python.observation.fields

Created on Sun May 28 19:34:03 2023

@author: a lab in the Air

  1# -*- coding: utf-8 -*-
  2"""
  3Created on Sun May 28 19:34:03 2023
  4
  5@author: a lab in the Air
  6"""
  7from observation.field import Field
  8from json_ntv import Ntv, NtvSingle, NtvJsonEncoder
  9import json
 10    
 11class Nfield(Field):
 12    ''' Nfield is a child class of NtvField where values are NTV objects
 13
 14    The methods defined in this class are conversion methods:
 15    
 16    *converting external value to internal value:*
 17    
 18    - `Nfield.l_to_i`
 19    - `Nfield.s_to_i`
 20
 21    *converting internal value to external value:*
 22    
 23    - `Nfield.l_to_e`
 24    - `Nfield.s_to_e`
 25
 26    *converting internal value / NTV value:*
 27    
 28    - `Nfield.i_to_n`
 29    - `Nfield.n_to_i`
 30
 31    *extract the name of the value:*
 32
 33    - `Nfield.i_to_name`
 34    '''
 35    def __init__(self, codec=None, name=None, keys=None,
 36                 lendefault=0, reindex=False, fast=False):
 37        super().__init__(codec=codec, name=name, keys=keys,
 38                     lendefault=lendefault, reindex=reindex, fast=fast)
 39
 40    def __str__(self):
 41        '''return json string format'''
 42        return str(self.to_ntv(modecodec='full'))
 43        #return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
 44        
 45    @staticmethod
 46    def l_to_i(lis, fast=False):
 47        ''' converting a list of external values to a list of internal values
 48        
 49        *Parameters*
 50
 51        - **fast**: boolean (default False) - list is created with a list of json values 
 52        without control'''
 53        if fast:
 54            return [NtvSingle(val, fast=True) for val in lis]
 55        return [Ntv.from_obj(val) for val in lis]
 56
 57    @staticmethod
 58    def s_to_i(val, fast=False):
 59        '''converting an external value to an internal value
 60
 61        *Parameters*
 62
 63        - **fast**: boolean (default False) - list is created with a list of json values 
 64        without control'''
 65        if fast:
 66            return NtvSingle(val, fast=True)        
 67        return Ntv.from_obj(val)
 68
 69    @staticmethod
 70    def n_to_i(ntv, fast=False):
 71        ''' converting a NTV value to an internal value'''
 72        return ntv
 73
 74    @staticmethod
 75    def l_to_e(lis, fast=False):
 76        ''' converting a list of internal values to a list of external values'''
 77        return [ntv.to_obj() for ntv in lis]    
 78
 79    @staticmethod
 80    def s_to_e(val, fast=False):
 81        '''converting an internal value to an external value'''
 82        return val.to_obj()
 83
 84    @staticmethod
 85    def i_to_n(val):
 86        ''' converting an internal value to a NTV value'''
 87        return val
 88
 89    @staticmethod
 90    def i_to_name(val):
 91        ''' return the name of the internal value'''
 92        return val.name   
 93    
 94class Sfield(Field):
 95    ''' Sfield is a child class of NtvField where inner and outer values are same
 96
 97    The methods defined in this class are conversion methods:
 98    
 99    *converting external value to internal value:*
100    
101    - `Nfield.l_to_i`
102    - `Nfield.s_to_i`
103
104    *converting internal value to external value:*
105    
106    - `Nfield.l_to_e`
107    - `Nfield.s_to_e`
108
109    *converting internal value / NTV value:*
110    
111    - `Nfield.i_to_n`
112    - `Nfield.n_to_i`
113
114    *extract the name of the value:*
115
116    - `Nfield.i_to_name`
117    '''    
118    def __init__(self, codec=None, name=None, keys=None,
119                 lendefault=0, reindex=False, fast=False):
120        super().__init__(codec=codec, name=name, keys=keys,
121                     lendefault=lendefault, reindex=reindex, fast=fast)
122
123    def __str__(self):
124        '''return json string format'''
125        return str({self.name: self.l_to_e(self.values)})
126        #return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
127        
128    @staticmethod
129    def l_to_i(lis, fast=False):
130        ''' converting a list of external values to a list of internal values'''
131        if fast:
132            return lis
133        return [Sfield.s_to_i(val, fast) for val in lis]
134    
135    @staticmethod
136    def s_to_i(val, fast=False):
137        '''converting an external value to an internal value'''
138        if fast:
139            return val
140        if val is None or isinstance(val, bool):
141            return json.dumps(val)
142        #if val in ('null', 'false' 'true'):
143        #    return json.loads(val)
144        if isinstance(val, list):
145            return Sfield._tupled(val)
146        if isinstance(val, dict):
147            return json.dumps(val, cls=NtvJsonEncoder)
148        return val    
149    
150    @staticmethod
151    def n_to_i(ntv_lis, fast=False):
152        ''' converting a NtvList value to an internal value'''
153        if isinstance(ntv_lis, list) and len(ntv_lis) == 0:
154            return []
155        if isinstance(ntv_lis, list) and ntv_lis[0].__class__.__name__ in ('NtvSingle', 'NtvList'):
156            #return [Sfield.n_to_i(ntv.val, fast) for ntv in ntv_lis]
157            return [Sfield.n_to_i(ntv.to_obj(), fast) for ntv in ntv_lis]
158        return  Sfield.s_to_i(ntv_lis, fast)
159    
160    @staticmethod
161    def l_to_e(lis, fast=False):
162        ''' converting a list of internal values to a list of external values'''
163        if fast:
164            return lis
165        return [Sfield.s_to_e(val) for val in lis]
166    
167    @staticmethod
168    def s_to_e(val, fast=False):
169        '''converting an internal value to an external value'''
170        if fast:
171            return val
172        if val in ('null', 'false', 'true'):
173            return json.loads(val)
174        #if val is None or isinstance(val, bool):
175        #    return json.dumps(val)
176        if isinstance(val, tuple):
177            return Sfield._listed(val)
178        if isinstance(val, str) and len(val) > 0 and val[0] == '{':
179            return json.loads(val)
180        return val
181    
182    @staticmethod
183    def i_to_n(val):
184        ''' converting an internal value to a NTV value'''
185        return Ntv.obj(Sfield.s_to_e(val))
186
187    @staticmethod
188    def i_to_name(val):
189        ''' return the name of the internal value'''
190        return ''
191    
192    @staticmethod
193    def _tupled(lis):
194        '''transform a list of list in a tuple of tuple'''
195        return tuple([val if not isinstance(val, list) else Sfield._tupled(val) for val in lis])
196
197    @staticmethod
198    def _listed(lis):
199        '''transform a tuple of tuple in a list of list'''
200        return [val if not isinstance(val, tuple) else Sfield._listed(val) for val in lis]
class Nfield(observation.field.Field):
12class Nfield(Field):
13    ''' Nfield is a child class of NtvField where values are NTV objects
14
15    The methods defined in this class are conversion methods:
16    
17    *converting external value to internal value:*
18    
19    - `Nfield.l_to_i`
20    - `Nfield.s_to_i`
21
22    *converting internal value to external value:*
23    
24    - `Nfield.l_to_e`
25    - `Nfield.s_to_e`
26
27    *converting internal value / NTV value:*
28    
29    - `Nfield.i_to_n`
30    - `Nfield.n_to_i`
31
32    *extract the name of the value:*
33
34    - `Nfield.i_to_name`
35    '''
36    def __init__(self, codec=None, name=None, keys=None,
37                 lendefault=0, reindex=False, fast=False):
38        super().__init__(codec=codec, name=name, keys=keys,
39                     lendefault=lendefault, reindex=reindex, fast=fast)
40
41    def __str__(self):
42        '''return json string format'''
43        return str(self.to_ntv(modecodec='full'))
44        #return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
45        
46    @staticmethod
47    def l_to_i(lis, fast=False):
48        ''' converting a list of external values to a list of internal values
49        
50        *Parameters*
51
52        - **fast**: boolean (default False) - list is created with a list of json values 
53        without control'''
54        if fast:
55            return [NtvSingle(val, fast=True) for val in lis]
56        return [Ntv.from_obj(val) for val in lis]
57
58    @staticmethod
59    def s_to_i(val, fast=False):
60        '''converting an external value to an internal value
61
62        *Parameters*
63
64        - **fast**: boolean (default False) - list is created with a list of json values 
65        without control'''
66        if fast:
67            return NtvSingle(val, fast=True)        
68        return Ntv.from_obj(val)
69
70    @staticmethod
71    def n_to_i(ntv, fast=False):
72        ''' converting a NTV value to an internal value'''
73        return ntv
74
75    @staticmethod
76    def l_to_e(lis, fast=False):
77        ''' converting a list of internal values to a list of external values'''
78        return [ntv.to_obj() for ntv in lis]    
79
80    @staticmethod
81    def s_to_e(val, fast=False):
82        '''converting an internal value to an external value'''
83        return val.to_obj()
84
85    @staticmethod
86    def i_to_n(val):
87        ''' converting an internal value to a NTV value'''
88        return val
89
90    @staticmethod
91    def i_to_name(val):
92        ''' return the name of the internal value'''
93        return val.name   

Nfield is a child class of NtvField 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:

Nfield( codec=None, name=None, keys=None, lendefault=0, reindex=False, fast=False)
36    def __init__(self, codec=None, name=None, keys=None,
37                 lendefault=0, reindex=False, fast=False):
38        super().__init__(codec=codec, name=name, keys=keys,
39                     lendefault=lendefault, reindex=reindex, fast=fast)

Field 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
@staticmethod
def l_to_i(lis, fast=False):
46    @staticmethod
47    def l_to_i(lis, fast=False):
48        ''' converting a list of external values to a list of internal values
49        
50        *Parameters*
51
52        - **fast**: boolean (default False) - list is created with a list of json values 
53        without control'''
54        if fast:
55            return [NtvSingle(val, fast=True) for val in lis]
56        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):
58    @staticmethod
59    def s_to_i(val, fast=False):
60        '''converting an external value to an internal value
61
62        *Parameters*
63
64        - **fast**: boolean (default False) - list is created with a list of json values 
65        without control'''
66        if fast:
67            return NtvSingle(val, fast=True)        
68        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, fast=False):
70    @staticmethod
71    def n_to_i(ntv, fast=False):
72        ''' converting a NTV value to an internal value'''
73        return ntv

converting a NTV value to an internal value

@staticmethod
def l_to_e(lis, fast=False):
75    @staticmethod
76    def l_to_e(lis, fast=False):
77        ''' converting a list of internal values to a list of external values'''
78        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):
80    @staticmethod
81    def s_to_e(val, fast=False):
82        '''converting an internal value to an external value'''
83        return val.to_obj()

converting an internal value to an external value

@staticmethod
def i_to_n(val):
85    @staticmethod
86    def i_to_n(val):
87        ''' converting an internal value to a NTV value'''
88        return val

converting an internal value to a NTV value

@staticmethod
def i_to_name(val):
90    @staticmethod
91    def i_to_name(val):
92        ''' return the name of the internal value'''
93        return val.name   

return the name of the internal value

Inherited Members
observation.field.Field
name
bol
from_parent
ntv
from_ntv
merging
add
cod
codec
infos
keys
values
val
observation.field_structure.FieldStructure
append
coupling
couplinginfos
derkeys
extendkeys
full
getduplicates
iscrossed
iscoupled
isderived
iskeysfromderkeys
islinked
isvalue
keytoval
keysfromderkeys
loc
recordfromvalue
recordfromkeys
reindex
reorder
setcodecvalue
setcodeclist
set_keys
set_codec
setkeys
setname
setvalue
setlistvalue
sort
tocoupled
tostdcodec
valtokey
observation.field_interface.FieldInterface
decode_ntv
encode_coef
keysfromcoef
to_dict_obj
to_numpy
to_ntv
to_pandas
vlist
vName
class Sfield(observation.field.Field):
 95class Sfield(Field):
 96    ''' Sfield is a child class of NtvField where inner and outer values are same
 97
 98    The methods defined in this class are conversion methods:
 99    
100    *converting external value to internal value:*
101    
102    - `Nfield.l_to_i`
103    - `Nfield.s_to_i`
104
105    *converting internal value to external value:*
106    
107    - `Nfield.l_to_e`
108    - `Nfield.s_to_e`
109
110    *converting internal value / NTV value:*
111    
112    - `Nfield.i_to_n`
113    - `Nfield.n_to_i`
114
115    *extract the name of the value:*
116
117    - `Nfield.i_to_name`
118    '''    
119    def __init__(self, codec=None, name=None, keys=None,
120                 lendefault=0, reindex=False, fast=False):
121        super().__init__(codec=codec, name=name, keys=keys,
122                     lendefault=lendefault, reindex=reindex, fast=fast)
123
124    def __str__(self):
125        '''return json string format'''
126        return str({self.name: self.l_to_e(self.values)})
127        #return '    ' + self.to_obj(encoded=True, modecodec='full', untyped=False) + '\n'
128        
129    @staticmethod
130    def l_to_i(lis, fast=False):
131        ''' converting a list of external values to a list of internal values'''
132        if fast:
133            return lis
134        return [Sfield.s_to_i(val, fast) for val in lis]
135    
136    @staticmethod
137    def s_to_i(val, fast=False):
138        '''converting an external value to an internal value'''
139        if fast:
140            return val
141        if val is None or isinstance(val, bool):
142            return json.dumps(val)
143        #if val in ('null', 'false' 'true'):
144        #    return json.loads(val)
145        if isinstance(val, list):
146            return Sfield._tupled(val)
147        if isinstance(val, dict):
148            return json.dumps(val, cls=NtvJsonEncoder)
149        return val    
150    
151    @staticmethod
152    def n_to_i(ntv_lis, fast=False):
153        ''' converting a NtvList value to an internal value'''
154        if isinstance(ntv_lis, list) and len(ntv_lis) == 0:
155            return []
156        if isinstance(ntv_lis, list) and ntv_lis[0].__class__.__name__ in ('NtvSingle', 'NtvList'):
157            #return [Sfield.n_to_i(ntv.val, fast) for ntv in ntv_lis]
158            return [Sfield.n_to_i(ntv.to_obj(), fast) for ntv in ntv_lis]
159        return  Sfield.s_to_i(ntv_lis, fast)
160    
161    @staticmethod
162    def l_to_e(lis, fast=False):
163        ''' converting a list of internal values to a list of external values'''
164        if fast:
165            return lis
166        return [Sfield.s_to_e(val) for val in lis]
167    
168    @staticmethod
169    def s_to_e(val, fast=False):
170        '''converting an internal value to an external value'''
171        if fast:
172            return val
173        if val in ('null', 'false', 'true'):
174            return json.loads(val)
175        #if val is None or isinstance(val, bool):
176        #    return json.dumps(val)
177        if isinstance(val, tuple):
178            return Sfield._listed(val)
179        if isinstance(val, str) and len(val) > 0 and val[0] == '{':
180            return json.loads(val)
181        return val
182    
183    @staticmethod
184    def i_to_n(val):
185        ''' converting an internal value to a NTV value'''
186        return Ntv.obj(Sfield.s_to_e(val))
187
188    @staticmethod
189    def i_to_name(val):
190        ''' return the name of the internal value'''
191        return ''
192    
193    @staticmethod
194    def _tupled(lis):
195        '''transform a list of list in a tuple of tuple'''
196        return tuple([val if not isinstance(val, list) else Sfield._tupled(val) for val in lis])
197
198    @staticmethod
199    def _listed(lis):
200        '''transform a tuple of tuple in a list of list'''
201        return [val if not isinstance(val, tuple) else Sfield._listed(val) for val in lis]

Sfield is a child class of NtvField where inner and outer values are same

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:

Sfield( codec=None, name=None, keys=None, lendefault=0, reindex=False, fast=False)
119    def __init__(self, codec=None, name=None, keys=None,
120                 lendefault=0, reindex=False, fast=False):
121        super().__init__(codec=codec, name=name, keys=keys,
122                     lendefault=lendefault, reindex=reindex, fast=fast)

Field 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
@staticmethod
def l_to_i(lis, fast=False):
129    @staticmethod
130    def l_to_i(lis, fast=False):
131        ''' converting a list of external values to a list of internal values'''
132        if fast:
133            return lis
134        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):
136    @staticmethod
137    def s_to_i(val, fast=False):
138        '''converting an external value to an internal value'''
139        if fast:
140            return val
141        if val is None or isinstance(val, bool):
142            return json.dumps(val)
143        #if val in ('null', 'false' 'true'):
144        #    return json.loads(val)
145        if isinstance(val, list):
146            return Sfield._tupled(val)
147        if isinstance(val, dict):
148            return json.dumps(val, cls=NtvJsonEncoder)
149        return val    

converting an external value to an internal value

@staticmethod
def n_to_i(ntv_lis, fast=False):
151    @staticmethod
152    def n_to_i(ntv_lis, fast=False):
153        ''' converting a NtvList value to an internal value'''
154        if isinstance(ntv_lis, list) and len(ntv_lis) == 0:
155            return []
156        if isinstance(ntv_lis, list) and ntv_lis[0].__class__.__name__ in ('NtvSingle', 'NtvList'):
157            #return [Sfield.n_to_i(ntv.val, fast) for ntv in ntv_lis]
158            return [Sfield.n_to_i(ntv.to_obj(), fast) for ntv in ntv_lis]
159        return  Sfield.s_to_i(ntv_lis, fast)

converting a NtvList value to an internal value

@staticmethod
def l_to_e(lis, fast=False):
161    @staticmethod
162    def l_to_e(lis, fast=False):
163        ''' converting a list of internal values to a list of external values'''
164        if fast:
165            return lis
166        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):
168    @staticmethod
169    def s_to_e(val, fast=False):
170        '''converting an internal value to an external value'''
171        if fast:
172            return val
173        if val in ('null', 'false', 'true'):
174            return json.loads(val)
175        #if val is None or isinstance(val, bool):
176        #    return json.dumps(val)
177        if isinstance(val, tuple):
178            return Sfield._listed(val)
179        if isinstance(val, str) and len(val) > 0 and val[0] == '{':
180            return json.loads(val)
181        return val

converting an internal value to an external value

@staticmethod
def i_to_n(val):
183    @staticmethod
184    def i_to_n(val):
185        ''' converting an internal value to a NTV value'''
186        return Ntv.obj(Sfield.s_to_e(val))

converting an internal value to a NTV value

@staticmethod
def i_to_name(val):
188    @staticmethod
189    def i_to_name(val):
190        ''' return the name of the internal value'''
191        return ''

return the name of the internal value

Inherited Members
observation.field.Field
name
bol
from_parent
ntv
from_ntv
merging
add
cod
codec
infos
keys
values
val
observation.field_structure.FieldStructure
append
coupling
couplinginfos
derkeys
extendkeys
full
getduplicates
iscrossed
iscoupled
isderived
iskeysfromderkeys
islinked
isvalue
keytoval
keysfromderkeys
loc
recordfromvalue
recordfromkeys
reindex
reorder
setcodecvalue
setcodeclist
set_keys
set_codec
setkeys
setname
setvalue
setlistvalue
sort
tocoupled
tostdcodec
valtokey
observation.field_interface.FieldInterface
decode_ntv
encode_coef
keysfromcoef
to_dict_obj
to_numpy
to_ntv
to_pandas
vlist
vName