tab-dataset.tab_dataset.field

The field module is part of the tab-dataset package.

It contains the classes Sfield and Nfield for Field entities.

For more information, see the user guide or the github repository.

  1# -*- coding: utf-8 -*-
  2"""
  3The `field` module is part of the `tab-dataset` package.
  4
  5It contains the classes `Sfield` and `Nfield` for Field entities.
  6
  7For more information, see the 
  8[user guide](https://loco-philippe.github.io/tab-dataset/docs/user_guide.html) 
  9or the [github repository](https://github.com/loco-philippe/tab-dataset).
 10"""
 11from copy import copy
 12import json
 13
 14from json_ntv.ntv import Ntv, NtvSingle
 15from json_ntv.ntv_util import NtvJsonEncoder
 16
 17from tab_dataset.field_interface import FieldInterface
 18from tab_dataset.cfield import Cfield, FieldError, Cutil
 19
 20DEFAULTINDEX = '$default'
 21
 22
 23class Sfield(FieldInterface, Cfield):
 24    '''
 25    `Sfield` is a child class of `Cfield` where internal value can be different
 26    from external value (list is converted in tuple and dict in json-object).
 27
 28    Attributes are the same as Cfield class
 29
 30    The methods defined in this class are :
 31
 32    *constructor (@classmethod)*
 33
 34    - `Cfield.bol`
 35    - `Cfield.from_ntv`
 36    - `Cfield.ntv`
 37    - `Cfield.like`
 38    - `Sfield.merging`
 39
 40    *conversion (@staticmethod)*
 41
 42    - `Sfield.l_to_i`
 43    - `Sfield.s_to_i`
 44    - `Sfield.l_to_e`
 45    - `Sfield.s_to_e`
 46    - `Sfield.i_to_n`
 47    - `Sfield.n_to_i`
 48    - `Sfield.i_to_name`
 49    - `Cfield.ntv_to_val` (@classmethod)
 50    
 51    *dynamic value (getters @property)*
 52
 53    - `Sfield.val`
 54    - `Sfield.cod`
 55    - `Cfield.hashf`
 56    - `Cfield.to_analysis`
 57    - `Cfield.values`
 58    - `Cfield.codec`
 59    - `Cfield.infos`
 60    - `Cfield.keys`
 61    
 62    *add - update methods*
 63
 64    - `Sfield.append`
 65    - `Sfield.setcodecvalue`
 66    - `Sfield.setcodeclist`
 67    - `Sfield.setlistvalue`
 68    - `Sfield.setvalue`
 69    - `Cfield.add`
 70    - `Cfield.setname`
 71    - `Cfield.set_keys`
 72    - `Cfield.set_codec`
 73    - `Cfield.setkeys`
 74
 75    *transform methods*
 76
 77    - `Cfield.coupling`
 78    - `Cfield.extendkeys`
 79    - `Cfield.full`
 80    - `Cfield.reindex`
 81    - `Cfield.reorder`
 82    - `Cfield.sort`
 83    - `Cfield.tocoupled`
 84    - `Cfield.tostdcodec`
 85    
 86    *getters methods*
 87
 88    - `Sfield.isvalue`
 89    - `Sfield.keytoval`
 90    - `Sfield.loc`
 91    - `Sfield.recordfromvalue`
 92    - `Sfield.valtokey`
 93    - `Cfield.couplinginfos`
 94    - `Cfield.derkeys`
 95    - `Cfield.getduplicates`
 96    - `Cfield.iscrossed`
 97    - `Cfield.iscoupled`
 98    - `Cfield.isderived`
 99    - `Cfield.islinked`
100    - `Cfield.iskeysfromderkeys`
101    - `Cfield.recordfromkeys`
102
103    *export methods (`observation.field_interface.SfieldInterface`)*
104
105    - `Sfield.json`
106    - `Sfield.to_obj`
107    - `Sfield.to_dict_obj`
108    - `Sfield.to_numpy`
109    - `Sfield.to_pandas`
110    - `Sfield.vlist`
111    - `Sfield.v_name`
112    - `Sfield.vSimple`
113    '''
114
115    def __init__(self, codec=None, name=None, keys=None,
116                 lendefault=0, reindex=False, fast=False):
117        '''
118        Sfield constructor.
119
120        *Parameters*
121
122        - **codec** :  list (default None) - external different values of index (see data model)
123        - **keys** :  list (default None)  - key value of index (see data model)
124        - **name** : string (default None) - name of index (see data model)
125        - **lendefault** : integer (default 0) - default len if no keys is defined
126        - **reindex** : boolean (default True) - if True, default codec is apply
127        - **fast**: boolean (default False) - if True, codec is created without conversion'''
128        if isinstance(codec, Sfield):
129            Cfield.__init__(self, copy(codec._codec),
130                            copy(codec.name), copy(codec._keys))
131            return
132        if codec is None:
133            codec = []
134        if not isinstance(codec, list):
135            codec = [codec]
136        leng = lendefault
137        if codec and len(codec) > 0 and not leng:
138            leng = len(codec)
139        if not keys is None:
140            leng = len(keys)
141        if not name:
142            name = DEFAULTINDEX
143        if not (keys is None or isinstance(keys, list)):
144            raise FieldError("keys not list")
145        if keys is None and leng == 0:
146            keys = []
147        elif keys is None:
148            keys = [(i*len(codec))//leng for i in range(leng)]
149        if codec == []:
150            keysset = Cutil.tocodec(keys)
151            codec = self.l_to_i(keysset, fast=True)
152        codec = self.l_to_i(codec, fast=fast)
153        Cfield.__init__(self, codec, name, keys, reindex=reindex)
154
155    def __setitem__(self, ind, item):
156        ''' modify values item'''
157        if isinstance(ind, slice):
158            start, stop, step = ind.start or 0, ind.stop or len(self), ind.step or 1
159            idxt = list(iter(range(start, stop, step)))
160            if len(idxt) != len(item):
161                raise FieldError("item length not consistent")
162            self.setlistvalue(item, idxt, extern=True)
163        elif ind < 0 or ind >= len(self):
164            raise FieldError("out of bounds")
165        else: 
166            self.setvalue(ind, item, extern=True)
167
168    def __str__(self):
169        '''return json string format'''
170        return str({self.name: self.l_to_e(self.values)})
171        # return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
172
173    @classmethod
174    def merging(cls, listidx, name=None):
175        '''Create a new Field with values are tuples of listidx Field values
176
177        *Parameters*
178
179        - **listidx** : list of Field to be merged.
180        - **name** : string (default : None) - Name of the new Field
181
182        *Returns* : new Field'''
183        if not name:
184            name = str(list({idx.name for idx in listidx}))
185        values = Cutil.transpose([idx.values for idx in listidx])
186        return cls.ntv({name: values})
187
188    @staticmethod
189    def l_to_i(lis, fast=False):
190        ''' converting a list of external values to a list of internal values'''
191        if fast:
192            return lis
193        return [Sfield.s_to_i(val, fast) for val in lis]
194
195    @staticmethod
196    def s_to_i(val, fast=False):
197        '''converting an external value to an internal value'''
198        if fast:
199            return val
200        if val is None or isinstance(val, bool):
201            return json.dumps(val)
202        if isinstance(val, list):
203            return Cutil.tupled(val)
204        if isinstance(val, dict):
205            return json.dumps(val, cls=NtvJsonEncoder)
206        return val
207
208    @staticmethod
209    def n_to_i(ntv_lis, fast=False):
210        ''' converting a NtvList value to an internal value'''
211        if isinstance(ntv_lis, list) and len(ntv_lis) == 0:
212            return []
213        if isinstance(ntv_lis, list) and ntv_lis[0].__class__.__name__ in ('NtvSingle', 'NtvList'):
214            # return [Sfield.n_to_i(ntv.val, fast) for ntv in ntv_lis]
215            return [Sfield.n_to_i(ntv.to_obj(), fast) for ntv in ntv_lis]
216        return Sfield.s_to_i(ntv_lis, fast)
217
218    @staticmethod
219    def l_to_e(lis, fast=False):
220        ''' converting a list of internal values to a list of external values'''
221        if fast:
222            return lis
223        return [Sfield.s_to_e(val) for val in lis]
224
225    @staticmethod
226    def s_to_e(val, fast=False):
227        '''converting an internal value to an external value'''
228        if fast:
229            return val
230        if val in ('null', 'false', 'true'):
231            return json.loads(val)
232        # if val is None or isinstance(val, bool):
233        #    return json.dumps(val)
234        if isinstance(val, tuple):
235            return Cutil.listed(val)
236        if isinstance(val, str) and len(val) > 0 and val[0] == '{':
237            return json.loads(val)
238        return val
239
240    @staticmethod
241    def i_to_n(val):
242        ''' converting an internal value to a NTV value'''
243        return Ntv.obj(Sfield.s_to_e(val))
244
245    @staticmethod
246    def l_to_n(lis):
247        ''' converting a list of internal values to a list of NTV value'''
248        #return Ntv.obj(Sfield.l_to_e(lis))
249        return [Sfield.i_to_n(val) for val in lis]
250
251    @staticmethod
252    def i_to_name(val):
253        ''' return the name of the internal value'''
254        return ''
255
256    @property
257    def cod(self):
258        '''return codec conversion to json value '''
259        return self.l_to_e(self._codec)
260
261    @property
262    def val(self):
263        '''return values conversion to string '''
264        return [self.s_to_e(self._codec[key]) for key in self._keys]
265
266    def append(self, value, unique=True):
267        '''add a new value
268
269        *Parameters*
270
271        - **value** : new object value
272        - **unique** :  boolean (default True) - If False, duplication codec if value is present
273
274        *Returns* : key of value '''
275        return super().append(self.s_to_i(value), unique)
276
277    def isvalue(self, value, extern=True):
278        ''' return True if value is in index values
279
280        *Parameters*
281
282        - **value** : value to check
283        - **extern** : if True, compare value to external representation of self.value,
284        else, internal'''
285        if extern:
286            return value in self.val
287        return super().isvalue(value)
288
289    def keytoval(self, key, extern=True):
290        ''' return the value of a key
291
292        *Parameters*
293
294        - **key** : key to convert into values
295        - **extern** : if True, return string representation else, internal value
296
297        *Returns*
298
299        - **int** : first key finded (None else)'''
300        if extern:
301            return self.s_to_e(super().keytoval(key))
302        return super().keytoval(key)
303
304    def loc(self, value, extern=True):
305        '''return a list of record number with value
306
307        *Parameters*
308
309        - **value** : value to check
310        - **extern** : if True, compare value to external representation of self.value,
311        else, internal
312
313        *Returns*
314
315        - **list of int** : list of record number finded (None else)'''
316        return self.recordfromvalue(value, extern=extern)
317
318    def recordfromvalue(self, value, extern=True):
319        '''return a list of record number with value
320
321        *Parameters*
322
323        - **value** : value to check
324        - **extern** : if True, compare value to external representation of self.value,
325        else, internal
326
327        *Returns*
328
329        - **list of int** : list of record number finded (None else)'''
330
331        if extern:
332            return super().recordfromvalue(self.s_to_i(value))
333        return super().recordfromvalue(value)
334
335    def setcodecvalue(self, oldvalue, newvalue, extern=True):
336        '''update all the oldvalue by newvalue
337
338        *Parameters*
339
340        - **oldvalue** : list of values to replace
341        - **newvalue** : list of new value to apply
342        - **extern** : if True, the newvalue has external representation, else internal
343
344        *Returns* : int - last codec rank updated (-1 if None)'''
345        if extern:
346            # ,nameonly, valueonly)
347            return super().setcodecvalue(self.s_to_i(oldvalue), self.s_to_i(newvalue))
348        return super().setcodecvalue(oldvalue, newvalue)  # , nameonly, valueonly)
349
350    def setcodeclist(self, listcodec, extern=True):
351        '''update codec with listcodec values
352
353        *Parameters*
354
355        - **listcodec** : list of new codec values to apply
356        - **extern** : if True, the newvalue has external representation, else internal
357
358        *Returns* : int - last codec rank updated (-1 if None)'''
359        if extern:
360            super().setcodeclist(self.l_to_i(listcodec))  # , nameonly, valueonly)
361        super().setcodeclist(listcodec)  # , nameonly, valueonly)
362
363    def setvalue(self, ind, value, extern=True):
364        '''update a value at the rank ind (and update codec and keys)
365
366        *Parameters*
367
368        - **ind** : rank of the value
369        - **value** : new value
370        - **extern** : if True, the value has external representation, else internal
371
372        *Returns* : None'''
373        if extern:
374            super().setvalue(ind, self.s_to_i(value))
375        else:
376            super().setvalue(ind, value)
377
378    def setlistvalue(self, listvalue, listind=None, extern=True):
379        '''update the values (and update codec and keys)
380
381        *Parameters*
382
383        - **listvalue** : list - list of new values
384        - **listind** : list(default None) - list of index
385        - **extern** : if True, the value has external representation, else internal
386
387        *Returns* : None'''
388        if extern:
389            super().setlistvalue(self.l_to_i(listvalue), listind=listind)
390        else:
391            super().setlistvalue(listvalue, listind=listind)
392
393    def valtokey(self, value, extern=True):
394        '''convert a value to a key
395
396        *Parameters*
397
398        - **value** : value to convert
399        - **extern** : if True, the value has external representation, else internal
400
401        *Returns*
402
403        - **int** : first key finded (None else)'''
404        if extern:
405            return super().valtokey(self.s_to_i(value))
406        return super().valtokey(value)
407
408
409
410class Nfield(Sfield):
411    ''' Nfield is a child class of SField where values are NTV objects
412
413    The methods defined in this class are conversion methods:
414
415    *converting external value to internal value:*
416
417    - `Nfield.l_to_i`
418    - `Nfield.s_to_i`
419
420    *converting internal value to external value:*
421
422    - `Nfield.l_to_e`
423    - `Nfield.s_to_e`
424
425    *converting internal value / NTV value:*
426
427    - `Nfield.i_to_n`
428    - `Nfield.n_to_i`
429
430    *extract the name of the value:*
431
432    - `Nfield.i_to_name`
433    '''
434
435    def __str__(self):
436        '''return json string format'''
437        return str(self.to_ntv(modecodec='full'))
438        # return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
439
440    @staticmethod
441    def l_to_i(lis, fast=False):
442        ''' converting a list of external values to a list of internal values
443
444        *Parameters*
445
446        - **fast**: boolean (default False) - list is created with a list of json values
447        without control'''
448        if fast:
449            return [NtvSingle(val, fast=True) for val in lis]
450        return [Ntv.from_obj(val) for val in lis]
451
452    @staticmethod
453    def s_to_i(val, fast=False):
454        '''converting an external value to an internal value
455
456        *Parameters*
457
458        - **fast**: boolean (default False) - list is created with a list of json values
459        without control'''
460        if fast:
461            return NtvSingle(val, fast=True)
462        return Ntv.from_obj(val)
463
464    @staticmethod
465    def n_to_i(ntv_lis, fast=False):
466        ''' converting a NTV value to an internal value'''
467        return ntv_lis
468
469    @staticmethod
470    def l_to_e(lis, fast=False):
471        ''' converting a list of internal values to a list of external values'''
472        return [ntv.to_obj() for ntv in lis]
473
474    @staticmethod
475    def s_to_e(val, fast=False):
476        '''converting an internal value to an external value'''
477        return val.to_obj()
478
479    @staticmethod
480    def i_to_n(val):
481        ''' converting an internal value to a NTV value'''
482        return val
483
484    @staticmethod
485    def l_to_n(lis):
486        ''' converting a list of internal values to a list of NTV value'''
487        #return Ntv.obj(Nfield.l_to_e(lis))
488        return [Sfield.i_to_n(val) for val in lis]
489    
490    @staticmethod
491    def i_to_name(val):
492        ''' return the name of the internal value'''
493        return val.name
class Sfield(tab_dataset.field_interface.FieldInterface, tab_dataset.cfield.Cfield):
 24class Sfield(FieldInterface, Cfield):
 25    '''
 26    `Sfield` is a child class of `Cfield` where internal value can be different
 27    from external value (list is converted in tuple and dict in json-object).
 28
 29    Attributes are the same as Cfield class
 30
 31    The methods defined in this class are :
 32
 33    *constructor (@classmethod)*
 34
 35    - `Cfield.bol`
 36    - `Cfield.from_ntv`
 37    - `Cfield.ntv`
 38    - `Cfield.like`
 39    - `Sfield.merging`
 40
 41    *conversion (@staticmethod)*
 42
 43    - `Sfield.l_to_i`
 44    - `Sfield.s_to_i`
 45    - `Sfield.l_to_e`
 46    - `Sfield.s_to_e`
 47    - `Sfield.i_to_n`
 48    - `Sfield.n_to_i`
 49    - `Sfield.i_to_name`
 50    - `Cfield.ntv_to_val` (@classmethod)
 51    
 52    *dynamic value (getters @property)*
 53
 54    - `Sfield.val`
 55    - `Sfield.cod`
 56    - `Cfield.hashf`
 57    - `Cfield.to_analysis`
 58    - `Cfield.values`
 59    - `Cfield.codec`
 60    - `Cfield.infos`
 61    - `Cfield.keys`
 62    
 63    *add - update methods*
 64
 65    - `Sfield.append`
 66    - `Sfield.setcodecvalue`
 67    - `Sfield.setcodeclist`
 68    - `Sfield.setlistvalue`
 69    - `Sfield.setvalue`
 70    - `Cfield.add`
 71    - `Cfield.setname`
 72    - `Cfield.set_keys`
 73    - `Cfield.set_codec`
 74    - `Cfield.setkeys`
 75
 76    *transform methods*
 77
 78    - `Cfield.coupling`
 79    - `Cfield.extendkeys`
 80    - `Cfield.full`
 81    - `Cfield.reindex`
 82    - `Cfield.reorder`
 83    - `Cfield.sort`
 84    - `Cfield.tocoupled`
 85    - `Cfield.tostdcodec`
 86    
 87    *getters methods*
 88
 89    - `Sfield.isvalue`
 90    - `Sfield.keytoval`
 91    - `Sfield.loc`
 92    - `Sfield.recordfromvalue`
 93    - `Sfield.valtokey`
 94    - `Cfield.couplinginfos`
 95    - `Cfield.derkeys`
 96    - `Cfield.getduplicates`
 97    - `Cfield.iscrossed`
 98    - `Cfield.iscoupled`
 99    - `Cfield.isderived`
100    - `Cfield.islinked`
101    - `Cfield.iskeysfromderkeys`
102    - `Cfield.recordfromkeys`
103
104    *export methods (`observation.field_interface.SfieldInterface`)*
105
106    - `Sfield.json`
107    - `Sfield.to_obj`
108    - `Sfield.to_dict_obj`
109    - `Sfield.to_numpy`
110    - `Sfield.to_pandas`
111    - `Sfield.vlist`
112    - `Sfield.v_name`
113    - `Sfield.vSimple`
114    '''
115
116    def __init__(self, codec=None, name=None, keys=None,
117                 lendefault=0, reindex=False, fast=False):
118        '''
119        Sfield constructor.
120
121        *Parameters*
122
123        - **codec** :  list (default None) - external different values of index (see data model)
124        - **keys** :  list (default None)  - key value of index (see data model)
125        - **name** : string (default None) - name of index (see data model)
126        - **lendefault** : integer (default 0) - default len if no keys is defined
127        - **reindex** : boolean (default True) - if True, default codec is apply
128        - **fast**: boolean (default False) - if True, codec is created without conversion'''
129        if isinstance(codec, Sfield):
130            Cfield.__init__(self, copy(codec._codec),
131                            copy(codec.name), copy(codec._keys))
132            return
133        if codec is None:
134            codec = []
135        if not isinstance(codec, list):
136            codec = [codec]
137        leng = lendefault
138        if codec and len(codec) > 0 and not leng:
139            leng = len(codec)
140        if not keys is None:
141            leng = len(keys)
142        if not name:
143            name = DEFAULTINDEX
144        if not (keys is None or isinstance(keys, list)):
145            raise FieldError("keys not list")
146        if keys is None and leng == 0:
147            keys = []
148        elif keys is None:
149            keys = [(i*len(codec))//leng for i in range(leng)]
150        if codec == []:
151            keysset = Cutil.tocodec(keys)
152            codec = self.l_to_i(keysset, fast=True)
153        codec = self.l_to_i(codec, fast=fast)
154        Cfield.__init__(self, codec, name, keys, reindex=reindex)
155
156    def __setitem__(self, ind, item):
157        ''' modify values item'''
158        if isinstance(ind, slice):
159            start, stop, step = ind.start or 0, ind.stop or len(self), ind.step or 1
160            idxt = list(iter(range(start, stop, step)))
161            if len(idxt) != len(item):
162                raise FieldError("item length not consistent")
163            self.setlistvalue(item, idxt, extern=True)
164        elif ind < 0 or ind >= len(self):
165            raise FieldError("out of bounds")
166        else: 
167            self.setvalue(ind, item, extern=True)
168
169    def __str__(self):
170        '''return json string format'''
171        return str({self.name: self.l_to_e(self.values)})
172        # return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
173
174    @classmethod
175    def merging(cls, listidx, name=None):
176        '''Create a new Field with values are tuples of listidx Field values
177
178        *Parameters*
179
180        - **listidx** : list of Field to be merged.
181        - **name** : string (default : None) - Name of the new Field
182
183        *Returns* : new Field'''
184        if not name:
185            name = str(list({idx.name for idx in listidx}))
186        values = Cutil.transpose([idx.values for idx in listidx])
187        return cls.ntv({name: values})
188
189    @staticmethod
190    def l_to_i(lis, fast=False):
191        ''' converting a list of external values to a list of internal values'''
192        if fast:
193            return lis
194        return [Sfield.s_to_i(val, fast) for val in lis]
195
196    @staticmethod
197    def s_to_i(val, fast=False):
198        '''converting an external value to an internal value'''
199        if fast:
200            return val
201        if val is None or isinstance(val, bool):
202            return json.dumps(val)
203        if isinstance(val, list):
204            return Cutil.tupled(val)
205        if isinstance(val, dict):
206            return json.dumps(val, cls=NtvJsonEncoder)
207        return val
208
209    @staticmethod
210    def n_to_i(ntv_lis, fast=False):
211        ''' converting a NtvList value to an internal value'''
212        if isinstance(ntv_lis, list) and len(ntv_lis) == 0:
213            return []
214        if isinstance(ntv_lis, list) and ntv_lis[0].__class__.__name__ in ('NtvSingle', 'NtvList'):
215            # return [Sfield.n_to_i(ntv.val, fast) for ntv in ntv_lis]
216            return [Sfield.n_to_i(ntv.to_obj(), fast) for ntv in ntv_lis]
217        return Sfield.s_to_i(ntv_lis, fast)
218
219    @staticmethod
220    def l_to_e(lis, fast=False):
221        ''' converting a list of internal values to a list of external values'''
222        if fast:
223            return lis
224        return [Sfield.s_to_e(val) for val in lis]
225
226    @staticmethod
227    def s_to_e(val, fast=False):
228        '''converting an internal value to an external value'''
229        if fast:
230            return val
231        if val in ('null', 'false', 'true'):
232            return json.loads(val)
233        # if val is None or isinstance(val, bool):
234        #    return json.dumps(val)
235        if isinstance(val, tuple):
236            return Cutil.listed(val)
237        if isinstance(val, str) and len(val) > 0 and val[0] == '{':
238            return json.loads(val)
239        return val
240
241    @staticmethod
242    def i_to_n(val):
243        ''' converting an internal value to a NTV value'''
244        return Ntv.obj(Sfield.s_to_e(val))
245
246    @staticmethod
247    def l_to_n(lis):
248        ''' converting a list of internal values to a list of NTV value'''
249        #return Ntv.obj(Sfield.l_to_e(lis))
250        return [Sfield.i_to_n(val) for val in lis]
251
252    @staticmethod
253    def i_to_name(val):
254        ''' return the name of the internal value'''
255        return ''
256
257    @property
258    def cod(self):
259        '''return codec conversion to json value '''
260        return self.l_to_e(self._codec)
261
262    @property
263    def val(self):
264        '''return values conversion to string '''
265        return [self.s_to_e(self._codec[key]) for key in self._keys]
266
267    def append(self, value, unique=True):
268        '''add a new value
269
270        *Parameters*
271
272        - **value** : new object value
273        - **unique** :  boolean (default True) - If False, duplication codec if value is present
274
275        *Returns* : key of value '''
276        return super().append(self.s_to_i(value), unique)
277
278    def isvalue(self, value, extern=True):
279        ''' return True if value is in index values
280
281        *Parameters*
282
283        - **value** : value to check
284        - **extern** : if True, compare value to external representation of self.value,
285        else, internal'''
286        if extern:
287            return value in self.val
288        return super().isvalue(value)
289
290    def keytoval(self, key, extern=True):
291        ''' return the value of a key
292
293        *Parameters*
294
295        - **key** : key to convert into values
296        - **extern** : if True, return string representation else, internal value
297
298        *Returns*
299
300        - **int** : first key finded (None else)'''
301        if extern:
302            return self.s_to_e(super().keytoval(key))
303        return super().keytoval(key)
304
305    def loc(self, value, extern=True):
306        '''return a list of record number with value
307
308        *Parameters*
309
310        - **value** : value to check
311        - **extern** : if True, compare value to external representation of self.value,
312        else, internal
313
314        *Returns*
315
316        - **list of int** : list of record number finded (None else)'''
317        return self.recordfromvalue(value, extern=extern)
318
319    def recordfromvalue(self, value, extern=True):
320        '''return a list of record number with value
321
322        *Parameters*
323
324        - **value** : value to check
325        - **extern** : if True, compare value to external representation of self.value,
326        else, internal
327
328        *Returns*
329
330        - **list of int** : list of record number finded (None else)'''
331
332        if extern:
333            return super().recordfromvalue(self.s_to_i(value))
334        return super().recordfromvalue(value)
335
336    def setcodecvalue(self, oldvalue, newvalue, extern=True):
337        '''update all the oldvalue by newvalue
338
339        *Parameters*
340
341        - **oldvalue** : list of values to replace
342        - **newvalue** : list of new value to apply
343        - **extern** : if True, the newvalue has external representation, else internal
344
345        *Returns* : int - last codec rank updated (-1 if None)'''
346        if extern:
347            # ,nameonly, valueonly)
348            return super().setcodecvalue(self.s_to_i(oldvalue), self.s_to_i(newvalue))
349        return super().setcodecvalue(oldvalue, newvalue)  # , nameonly, valueonly)
350
351    def setcodeclist(self, listcodec, extern=True):
352        '''update codec with listcodec values
353
354        *Parameters*
355
356        - **listcodec** : list of new codec values to apply
357        - **extern** : if True, the newvalue has external representation, else internal
358
359        *Returns* : int - last codec rank updated (-1 if None)'''
360        if extern:
361            super().setcodeclist(self.l_to_i(listcodec))  # , nameonly, valueonly)
362        super().setcodeclist(listcodec)  # , nameonly, valueonly)
363
364    def setvalue(self, ind, value, extern=True):
365        '''update a value at the rank ind (and update codec and keys)
366
367        *Parameters*
368
369        - **ind** : rank of the value
370        - **value** : new value
371        - **extern** : if True, the value has external representation, else internal
372
373        *Returns* : None'''
374        if extern:
375            super().setvalue(ind, self.s_to_i(value))
376        else:
377            super().setvalue(ind, value)
378
379    def setlistvalue(self, listvalue, listind=None, extern=True):
380        '''update the values (and update codec and keys)
381
382        *Parameters*
383
384        - **listvalue** : list - list of new values
385        - **listind** : list(default None) - list of index
386        - **extern** : if True, the value has external representation, else internal
387
388        *Returns* : None'''
389        if extern:
390            super().setlistvalue(self.l_to_i(listvalue), listind=listind)
391        else:
392            super().setlistvalue(listvalue, listind=listind)
393
394    def valtokey(self, value, extern=True):
395        '''convert a value to a key
396
397        *Parameters*
398
399        - **value** : value to convert
400        - **extern** : if True, the value has external representation, else internal
401
402        *Returns*
403
404        - **int** : first key finded (None else)'''
405        if extern:
406            return super().valtokey(self.s_to_i(value))
407        return super().valtokey(value)

Sfield is a child class of Cfield where internal value can be different from external value (list is converted in tuple and dict in json-object).

Attributes are the same as Cfield class

The methods defined in this class are :

constructor (@classmethod)

conversion (@staticmethod)

dynamic value (getters @property)

  • Sfield.val
  • Sfield.cod
  • Cfield.hashf
  • Cfield.to_analysis
  • Cfield.values
  • Cfield.codec
  • Cfield.infos
  • Cfield.keys

add - update methods

transform methods

  • Cfield.coupling
  • Cfield.extendkeys
  • Cfield.full
  • Cfield.reindex
  • Cfield.reorder
  • Cfield.sort
  • Cfield.tocoupled
  • Cfield.tostdcodec

getters methods

export methods (observation.field_interface.SfieldInterface)

Sfield( codec=None, name=None, keys=None, lendefault=0, reindex=False, fast=False)
116    def __init__(self, codec=None, name=None, keys=None,
117                 lendefault=0, reindex=False, fast=False):
118        '''
119        Sfield constructor.
120
121        *Parameters*
122
123        - **codec** :  list (default None) - external different values of index (see data model)
124        - **keys** :  list (default None)  - key value of index (see data model)
125        - **name** : string (default None) - name of index (see data model)
126        - **lendefault** : integer (default 0) - default len if no keys is defined
127        - **reindex** : boolean (default True) - if True, default codec is apply
128        - **fast**: boolean (default False) - if True, codec is created without conversion'''
129        if isinstance(codec, Sfield):
130            Cfield.__init__(self, copy(codec._codec),
131                            copy(codec.name), copy(codec._keys))
132            return
133        if codec is None:
134            codec = []
135        if not isinstance(codec, list):
136            codec = [codec]
137        leng = lendefault
138        if codec and len(codec) > 0 and not leng:
139            leng = len(codec)
140        if not keys is None:
141            leng = len(keys)
142        if not name:
143            name = DEFAULTINDEX
144        if not (keys is None or isinstance(keys, list)):
145            raise FieldError("keys not list")
146        if keys is None and leng == 0:
147            keys = []
148        elif keys is None:
149            keys = [(i*len(codec))//leng for i in range(leng)]
150        if codec == []:
151            keysset = Cutil.tocodec(keys)
152            codec = self.l_to_i(keysset, fast=True)
153        codec = self.l_to_i(codec, fast=fast)
154        Cfield.__init__(self, codec, name, keys, reindex=reindex)

Sfield constructor.

Parameters

  • codec : list (default None) - external different values of index (see data model)
  • keys : list (default None) - key value of index (see data model)
  • name : string (default None) - name of index (see data model)
  • lendefault : integer (default 0) - default len if no keys is defined
  • reindex : boolean (default True) - if True, default codec is apply
  • fast: boolean (default False) - if True, codec is created without conversion
@classmethod
def merging(cls, listidx, name=None):
174    @classmethod
175    def merging(cls, listidx, name=None):
176        '''Create a new Field with values are tuples of listidx Field values
177
178        *Parameters*
179
180        - **listidx** : list of Field to be merged.
181        - **name** : string (default : None) - Name of the new Field
182
183        *Returns* : new Field'''
184        if not name:
185            name = str(list({idx.name for idx in listidx}))
186        values = Cutil.transpose([idx.values for idx in listidx])
187        return cls.ntv({name: values})

Create a new Field with values are tuples of listidx Field values

Parameters

  • listidx : list of Field to be merged.
  • name : string (default : None) - Name of the new Field

Returns : new Field

@staticmethod
def l_to_i(lis, fast=False):
189    @staticmethod
190    def l_to_i(lis, fast=False):
191        ''' converting a list of external values to a list of internal values'''
192        if fast:
193            return lis
194        return [Sfield.s_to_i(val, fast) for val in lis]

converting a list of external values to a list of internal values

@staticmethod
def s_to_i(val, fast=False):
196    @staticmethod
197    def s_to_i(val, fast=False):
198        '''converting an external value to an internal value'''
199        if fast:
200            return val
201        if val is None or isinstance(val, bool):
202            return json.dumps(val)
203        if isinstance(val, list):
204            return Cutil.tupled(val)
205        if isinstance(val, dict):
206            return json.dumps(val, cls=NtvJsonEncoder)
207        return val

converting an external value to an internal value

@staticmethod
def n_to_i(ntv_lis, fast=False):
209    @staticmethod
210    def n_to_i(ntv_lis, fast=False):
211        ''' converting a NtvList value to an internal value'''
212        if isinstance(ntv_lis, list) and len(ntv_lis) == 0:
213            return []
214        if isinstance(ntv_lis, list) and ntv_lis[0].__class__.__name__ in ('NtvSingle', 'NtvList'):
215            # return [Sfield.n_to_i(ntv.val, fast) for ntv in ntv_lis]
216            return [Sfield.n_to_i(ntv.to_obj(), fast) for ntv in ntv_lis]
217        return Sfield.s_to_i(ntv_lis, fast)

converting a NtvList value to an internal value

@staticmethod
def l_to_e(lis, fast=False):
219    @staticmethod
220    def l_to_e(lis, fast=False):
221        ''' converting a list of internal values to a list of external values'''
222        if fast:
223            return lis
224        return [Sfield.s_to_e(val) for val in lis]

converting a list of internal values to a list of external values

@staticmethod
def s_to_e(val, fast=False):
226    @staticmethod
227    def s_to_e(val, fast=False):
228        '''converting an internal value to an external value'''
229        if fast:
230            return val
231        if val in ('null', 'false', 'true'):
232            return json.loads(val)
233        # if val is None or isinstance(val, bool):
234        #    return json.dumps(val)
235        if isinstance(val, tuple):
236            return Cutil.listed(val)
237        if isinstance(val, str) and len(val) > 0 and val[0] == '{':
238            return json.loads(val)
239        return val

converting an internal value to an external value

@staticmethod
def i_to_n(val):
241    @staticmethod
242    def i_to_n(val):
243        ''' converting an internal value to a NTV value'''
244        return Ntv.obj(Sfield.s_to_e(val))

converting an internal value to a NTV value

@staticmethod
def l_to_n(lis):
246    @staticmethod
247    def l_to_n(lis):
248        ''' converting a list of internal values to a list of NTV value'''
249        #return Ntv.obj(Sfield.l_to_e(lis))
250        return [Sfield.i_to_n(val) for val in lis]

converting a list of internal values to a list of NTV value

@staticmethod
def i_to_name(val):
252    @staticmethod
253    def i_to_name(val):
254        ''' return the name of the internal value'''
255        return ''

return the name of the internal value

cod

return codec conversion to json value

val

return values conversion to string

def append(self, value, unique=True):
267    def append(self, value, unique=True):
268        '''add a new value
269
270        *Parameters*
271
272        - **value** : new object value
273        - **unique** :  boolean (default True) - If False, duplication codec if value is present
274
275        *Returns* : key of value '''
276        return super().append(self.s_to_i(value), unique)

add a new value

Parameters

  • value : new object value
  • unique : boolean (default True) - If False, duplication codec if value is present

Returns : key of value

def isvalue(self, value, extern=True):
278    def isvalue(self, value, extern=True):
279        ''' return True if value is in index values
280
281        *Parameters*
282
283        - **value** : value to check
284        - **extern** : if True, compare value to external representation of self.value,
285        else, internal'''
286        if extern:
287            return value in self.val
288        return super().isvalue(value)

return True if value is in index values

Parameters

  • value : value to check
  • extern : if True, compare value to external representation of self.value, else, internal
def keytoval(self, key, extern=True):
290    def keytoval(self, key, extern=True):
291        ''' return the value of a key
292
293        *Parameters*
294
295        - **key** : key to convert into values
296        - **extern** : if True, return string representation else, internal value
297
298        *Returns*
299
300        - **int** : first key finded (None else)'''
301        if extern:
302            return self.s_to_e(super().keytoval(key))
303        return super().keytoval(key)

return the value of a key

Parameters

  • key : key to convert into values
  • extern : if True, return string representation else, internal value

Returns

  • int : first key finded (None else)
def loc(self, value, extern=True):
305    def loc(self, value, extern=True):
306        '''return a list of record number with value
307
308        *Parameters*
309
310        - **value** : value to check
311        - **extern** : if True, compare value to external representation of self.value,
312        else, internal
313
314        *Returns*
315
316        - **list of int** : list of record number finded (None else)'''
317        return self.recordfromvalue(value, extern=extern)

return a list of record number with value

Parameters

  • value : value to check
  • extern : if True, compare value to external representation of self.value, else, internal

Returns

  • list of int : list of record number finded (None else)
def recordfromvalue(self, value, extern=True):
319    def recordfromvalue(self, value, extern=True):
320        '''return a list of record number with value
321
322        *Parameters*
323
324        - **value** : value to check
325        - **extern** : if True, compare value to external representation of self.value,
326        else, internal
327
328        *Returns*
329
330        - **list of int** : list of record number finded (None else)'''
331
332        if extern:
333            return super().recordfromvalue(self.s_to_i(value))
334        return super().recordfromvalue(value)

return a list of record number with value

Parameters

  • value : value to check
  • extern : if True, compare value to external representation of self.value, else, internal

Returns

  • list of int : list of record number finded (None else)
def setcodecvalue(self, oldvalue, newvalue, extern=True):
336    def setcodecvalue(self, oldvalue, newvalue, extern=True):
337        '''update all the oldvalue by newvalue
338
339        *Parameters*
340
341        - **oldvalue** : list of values to replace
342        - **newvalue** : list of new value to apply
343        - **extern** : if True, the newvalue has external representation, else internal
344
345        *Returns* : int - last codec rank updated (-1 if None)'''
346        if extern:
347            # ,nameonly, valueonly)
348            return super().setcodecvalue(self.s_to_i(oldvalue), self.s_to_i(newvalue))
349        return super().setcodecvalue(oldvalue, newvalue)  # , nameonly, valueonly)

update all the oldvalue by newvalue

Parameters

  • oldvalue : list of values to replace
  • newvalue : list of new value to apply
  • extern : if True, the newvalue has external representation, else internal

Returns : int - last codec rank updated (-1 if None)

def setcodeclist(self, listcodec, extern=True):
351    def setcodeclist(self, listcodec, extern=True):
352        '''update codec with listcodec values
353
354        *Parameters*
355
356        - **listcodec** : list of new codec values to apply
357        - **extern** : if True, the newvalue has external representation, else internal
358
359        *Returns* : int - last codec rank updated (-1 if None)'''
360        if extern:
361            super().setcodeclist(self.l_to_i(listcodec))  # , nameonly, valueonly)
362        super().setcodeclist(listcodec)  # , nameonly, valueonly)

update codec with listcodec values

Parameters

  • listcodec : list of new codec values to apply
  • extern : if True, the newvalue has external representation, else internal

Returns : int - last codec rank updated (-1 if None)

def setvalue(self, ind, value, extern=True):
364    def setvalue(self, ind, value, extern=True):
365        '''update a value at the rank ind (and update codec and keys)
366
367        *Parameters*
368
369        - **ind** : rank of the value
370        - **value** : new value
371        - **extern** : if True, the value has external representation, else internal
372
373        *Returns* : None'''
374        if extern:
375            super().setvalue(ind, self.s_to_i(value))
376        else:
377            super().setvalue(ind, value)

update a value at the rank ind (and update codec and keys)

Parameters

  • ind : rank of the value
  • value : new value
  • extern : if True, the value has external representation, else internal

Returns : None

def setlistvalue(self, listvalue, listind=None, extern=True):
379    def setlistvalue(self, listvalue, listind=None, extern=True):
380        '''update the values (and update codec and keys)
381
382        *Parameters*
383
384        - **listvalue** : list - list of new values
385        - **listind** : list(default None) - list of index
386        - **extern** : if True, the value has external representation, else internal
387
388        *Returns* : None'''
389        if extern:
390            super().setlistvalue(self.l_to_i(listvalue), listind=listind)
391        else:
392            super().setlistvalue(listvalue, listind=listind)

update the values (and update codec and keys)

Parameters

  • listvalue : list - list of new values
  • listind : list(default None) - list of index
  • extern : if True, the value has external representation, else internal

Returns : None

def valtokey(self, value, extern=True):
394    def valtokey(self, value, extern=True):
395        '''convert a value to a key
396
397        *Parameters*
398
399        - **value** : value to convert
400        - **extern** : if True, the value has external representation, else internal
401
402        *Returns*
403
404        - **int** : first key finded (None else)'''
405        if extern:
406            return super().valtokey(self.s_to_i(value))
407        return super().valtokey(value)

convert a value to a key

Parameters

  • value : value to convert
  • extern : if True, the value has external representation, else internal

Returns

  • int : first key finded (None else)
Inherited Members
tab_dataset.field_interface.FieldInterface
to_numpy
to_ntv
to_pandas
vlist
v_name
tab_dataset.cfield.Cfield
hashf
to_analysis
codec
infos
keys
values
from_ntv
bol
like
ntv
ntv_to_val
check_relation
add
coupling
couplinginfos
derkeys
extendkeys
full
getduplicates
iscrossed
iscoupled
isderived
iskeysfromderkeys
islinked
recordfromkeys
reindex
reorder
set_keys
set_codec
setkeys
setname
sort
tocoupled
tostdcodec
class Nfield(Sfield):
411class Nfield(Sfield):
412    ''' Nfield is a child class of SField where values are NTV objects
413
414    The methods defined in this class are conversion methods:
415
416    *converting external value to internal value:*
417
418    - `Nfield.l_to_i`
419    - `Nfield.s_to_i`
420
421    *converting internal value to external value:*
422
423    - `Nfield.l_to_e`
424    - `Nfield.s_to_e`
425
426    *converting internal value / NTV value:*
427
428    - `Nfield.i_to_n`
429    - `Nfield.n_to_i`
430
431    *extract the name of the value:*
432
433    - `Nfield.i_to_name`
434    '''
435
436    def __str__(self):
437        '''return json string format'''
438        return str(self.to_ntv(modecodec='full'))
439        # return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
440
441    @staticmethod
442    def l_to_i(lis, fast=False):
443        ''' converting a list of external values to a list of internal values
444
445        *Parameters*
446
447        - **fast**: boolean (default False) - list is created with a list of json values
448        without control'''
449        if fast:
450            return [NtvSingle(val, fast=True) for val in lis]
451        return [Ntv.from_obj(val) for val in lis]
452
453    @staticmethod
454    def s_to_i(val, fast=False):
455        '''converting an external value to an internal value
456
457        *Parameters*
458
459        - **fast**: boolean (default False) - list is created with a list of json values
460        without control'''
461        if fast:
462            return NtvSingle(val, fast=True)
463        return Ntv.from_obj(val)
464
465    @staticmethod
466    def n_to_i(ntv_lis, fast=False):
467        ''' converting a NTV value to an internal value'''
468        return ntv_lis
469
470    @staticmethod
471    def l_to_e(lis, fast=False):
472        ''' converting a list of internal values to a list of external values'''
473        return [ntv.to_obj() for ntv in lis]
474
475    @staticmethod
476    def s_to_e(val, fast=False):
477        '''converting an internal value to an external value'''
478        return val.to_obj()
479
480    @staticmethod
481    def i_to_n(val):
482        ''' converting an internal value to a NTV value'''
483        return val
484
485    @staticmethod
486    def l_to_n(lis):
487        ''' converting a list of internal values to a list of NTV value'''
488        #return Ntv.obj(Nfield.l_to_e(lis))
489        return [Sfield.i_to_n(val) for val in lis]
490    
491    @staticmethod
492    def i_to_name(val):
493        ''' return the name of the internal value'''
494        return val.name

Nfield is a child class of SField where values are NTV objects

The methods defined in this class are conversion methods:

converting external value to internal value:

converting internal value to external value:

converting internal value / NTV value:

extract the name of the value:

@staticmethod
def l_to_i(lis, fast=False):
441    @staticmethod
442    def l_to_i(lis, fast=False):
443        ''' converting a list of external values to a list of internal values
444
445        *Parameters*
446
447        - **fast**: boolean (default False) - list is created with a list of json values
448        without control'''
449        if fast:
450            return [NtvSingle(val, fast=True) for val in lis]
451        return [Ntv.from_obj(val) for val in lis]

converting a list of external values to a list of internal values

Parameters

  • fast: boolean (default False) - list is created with a list of json values without control
@staticmethod
def s_to_i(val, fast=False):
453    @staticmethod
454    def s_to_i(val, fast=False):
455        '''converting an external value to an internal value
456
457        *Parameters*
458
459        - **fast**: boolean (default False) - list is created with a list of json values
460        without control'''
461        if fast:
462            return NtvSingle(val, fast=True)
463        return Ntv.from_obj(val)

converting an external value to an internal value

Parameters

  • fast: boolean (default False) - list is created with a list of json values without control
@staticmethod
def n_to_i(ntv_lis, fast=False):
465    @staticmethod
466    def n_to_i(ntv_lis, fast=False):
467        ''' converting a NTV value to an internal value'''
468        return ntv_lis

converting a NTV value to an internal value

@staticmethod
def l_to_e(lis, fast=False):
470    @staticmethod
471    def l_to_e(lis, fast=False):
472        ''' converting a list of internal values to a list of external values'''
473        return [ntv.to_obj() for ntv in lis]

converting a list of internal values to a list of external values

@staticmethod
def s_to_e(val, fast=False):
475    @staticmethod
476    def s_to_e(val, fast=False):
477        '''converting an internal value to an external value'''
478        return val.to_obj()

converting an internal value to an external value

@staticmethod
def i_to_n(val):
480    @staticmethod
481    def i_to_n(val):
482        ''' converting an internal value to a NTV value'''
483        return val

converting an internal value to a NTV value

@staticmethod
def l_to_n(lis):
485    @staticmethod
486    def l_to_n(lis):
487        ''' converting a list of internal values to a list of NTV value'''
488        #return Ntv.obj(Nfield.l_to_e(lis))
489        return [Sfield.i_to_n(val) for val in lis]

converting a list of internal values to a list of NTV value

@staticmethod
def i_to_name(val):
491    @staticmethod
492    def i_to_name(val):
493        ''' return the name of the internal value'''
494        return val.name

return the name of the internal value

Inherited Members
Sfield
Sfield
merging
cod
val
append
isvalue
keytoval
loc
recordfromvalue
setcodecvalue
setcodeclist
setvalue
setlistvalue
valtokey
tab_dataset.field_interface.FieldInterface
to_numpy
to_ntv
to_pandas
vlist
v_name
tab_dataset.cfield.Cfield
hashf
to_analysis
codec
infos
keys
values
from_ntv
bol
like
ntv
ntv_to_val
check_relation
add
coupling
couplinginfos
derkeys
extendkeys
full
getduplicates
iscrossed
iscoupled
isderived
iskeysfromderkeys
islinked
recordfromkeys
reindex
reorder
set_keys
set_codec
setkeys
setname
sort
tocoupled
tostdcodec