ntv-numpy.ntv_numpy.ndtype
@author: Philippe@loco-labs.io
The ndtype
module is part of the ntv-numpy.ntv_numpy
package (specification document).
It contains the class Ndtype
for NTVtype with extension.
For more information, see the user guide or the github repository.
1# -*- coding: utf-8 -*- 2""" 3@author: Philippe@loco-labs.io 4 5The `ndtype` module is part of the `ntv-numpy.ntv_numpy` package ([specification document]( 6https://loco-philippe.github.io/ES/JSON%20semantic%20format%20(JSON-NTV).htm)). 7 8It contains the class `Ndtype` for NTVtype with extension. 9 10For more information, see the 11[user guide](https://loco-philippe.github.io/ntv-numpy/docs/user_guide.html) 12 or the [github repository](https://github.com/loco-philippe/ntv-numpy). 13""" 14import configparser 15from pathlib import Path 16import json 17from json_ntv import Datatype 18import ntv_numpy 19 20 21class Ndtype(Datatype): 22 ''' The Ndtype is a child class of Datatype with additional attributes. 23 24 *Additional attributes :* 25 - add_type: additional data added to the JSON ndarray 26 - dtype: data type of the np.ndarray 27 28 *Inherited attributes :* 29 - **name** : String - name of the Datatype 30 - **nspace** : Namespace - namespace associated 31 - **custom** : boolean - True if not referenced 32 - **validate** : function (default None) - validate function 33 - **typebase** : TypeBase - TypeBas of the Datatype 34 - **extension** : String (default None) - extension of the TypeBase 35 36 The methods defined in this class are : 37 - `read_ini` (static method) 38 ''' 39 @staticmethod 40 def read_ini(): 41 '''return a dict with config data read in ntv_numpy.ini''' 42 config = configparser.ConfigParser() 43 p_file = Path('ntv_numpy.ini') 44 config.read(Path(ntv_numpy.__file__).parent / p_file) 45 types = json.loads(config['data']['types']) 46 return {ntv_type: {'add_type': add_type, 'dtype': dtype} 47 for [ntv_type, add_type, dtype] in types} 48 49 def __init__(self, full_name, module=False, force=False, validate=None): 50 '''NdType constructor. 51 52 *Parameters* 53 54 - **full_name** : String - absolut name of the Datatype 55 - **module** : boolean (default False) - if True search data in the 56 local .ini file, else in the distant repository 57 - **force** : boolean (default False) - if True, no Namespace control 58 - **validate** : function (default None) - validate function to include''' 59 super().__init__(full_name, module=module, force=force, validate=validate) 60 np_type = NP_TYPES.get(self.base_name) 61 self.dtype = np_type['dtype'] if np_type else None 62 self.add_type = np_type['add_type'] if np_type else None 63 64 65NP_TYPES = Ndtype.read_ini() 66NP_NTYPE = {val['dtype']: key for key, val in NP_TYPES.items()}
class
Ndtype(json_ntv.namespace.Datatype):
22class Ndtype(Datatype): 23 ''' The Ndtype is a child class of Datatype with additional attributes. 24 25 *Additional attributes :* 26 - add_type: additional data added to the JSON ndarray 27 - dtype: data type of the np.ndarray 28 29 *Inherited attributes :* 30 - **name** : String - name of the Datatype 31 - **nspace** : Namespace - namespace associated 32 - **custom** : boolean - True if not referenced 33 - **validate** : function (default None) - validate function 34 - **typebase** : TypeBase - TypeBas of the Datatype 35 - **extension** : String (default None) - extension of the TypeBase 36 37 The methods defined in this class are : 38 - `read_ini` (static method) 39 ''' 40 @staticmethod 41 def read_ini(): 42 '''return a dict with config data read in ntv_numpy.ini''' 43 config = configparser.ConfigParser() 44 p_file = Path('ntv_numpy.ini') 45 config.read(Path(ntv_numpy.__file__).parent / p_file) 46 types = json.loads(config['data']['types']) 47 return {ntv_type: {'add_type': add_type, 'dtype': dtype} 48 for [ntv_type, add_type, dtype] in types} 49 50 def __init__(self, full_name, module=False, force=False, validate=None): 51 '''NdType constructor. 52 53 *Parameters* 54 55 - **full_name** : String - absolut name of the Datatype 56 - **module** : boolean (default False) - if True search data in the 57 local .ini file, else in the distant repository 58 - **force** : boolean (default False) - if True, no Namespace control 59 - **validate** : function (default None) - validate function to include''' 60 super().__init__(full_name, module=module, force=force, validate=validate) 61 np_type = NP_TYPES.get(self.base_name) 62 self.dtype = np_type['dtype'] if np_type else None 63 self.add_type = np_type['add_type'] if np_type else None
The Ndtype is a child class of Datatype with additional attributes.
Additional attributes :
- add_type: additional data added to the JSON ndarray
- dtype: data type of the np.ndarray
Inherited attributes :
- name : String - name of the Datatype
- nspace : Namespace - namespace associated
- custom : boolean - True if not referenced
- validate : function (default None) - validate function
- typebase : TypeBase - TypeBas of the Datatype
- extension : String (default None) - extension of the TypeBase
The methods defined in this class are :
read_ini
(static method)
Ndtype(full_name, module=False, force=False, validate=None)
50 def __init__(self, full_name, module=False, force=False, validate=None): 51 '''NdType constructor. 52 53 *Parameters* 54 55 - **full_name** : String - absolut name of the Datatype 56 - **module** : boolean (default False) - if True search data in the 57 local .ini file, else in the distant repository 58 - **force** : boolean (default False) - if True, no Namespace control 59 - **validate** : function (default None) - validate function to include''' 60 super().__init__(full_name, module=module, force=force, validate=validate) 61 np_type = NP_TYPES.get(self.base_name) 62 self.dtype = np_type['dtype'] if np_type else None 63 self.add_type = np_type['add_type'] if np_type else None
NdType constructor.
Parameters
- full_name : String - absolut name of the Datatype
- module : boolean (default False) - if True search data in the local .ini file, else in the distant repository
- force : boolean (default False) - if True, no Namespace control
- validate : function (default None) - validate function to include
@staticmethod
def
read_ini():
40 @staticmethod 41 def read_ini(): 42 '''return a dict with config data read in ntv_numpy.ini''' 43 config = configparser.ConfigParser() 44 p_file = Path('ntv_numpy.ini') 45 config.read(Path(ntv_numpy.__file__).parent / p_file) 46 types = json.loads(config['data']['types']) 47 return {ntv_type: {'add_type': add_type, 'dtype': dtype} 48 for [ntv_type, add_type, dtype] in types}
return a dict with config data read in ntv_numpy.ini
Inherited Members
- json_ntv.namespace.Datatype
- extension
- typebase
- base_name
- name
- gen_type
- json_type
- category
- nspace
- custom
- validate
- add
- json_ntv.namespace.TypeBase
- types
- long_name
- isin_namespace
- json_ntv.ntv_util.NtvUtil
- is_dictable
- from_obj_name
- decode_ntv_tab
- to_ntvpointer
NP_TYPES =
{'': {'add_type': 'json', 'dtype': None}, 'date': {'add_type': None, 'dtype': 'datetime64[D]'}, 'yearmonth': {'add_type': None, 'dtype': 'datetime64[M]'}, 'year': {'add_type': None, 'dtype': 'datetime64[Y]'}, 'string': {'add_type': None, 'dtype': 'str'}, 'datetime': {'add_type': None, 'dtype': 'datetime64[s]'}, 'timedelta': {'add_type': None, 'dtype': 'timedelta64[s]'}, 'float16': {'add_type': None, 'dtype': 'float16'}, 'float32': {'add_type': None, 'dtype': 'float32'}, 'float64': {'add_type': None, 'dtype': 'float64'}, 'float96': {'add_type': None, 'dtype': 'float96'}, 'float128': {'add_type': None, 'dtype': 'float128'}, 'uint8': {'add_type': None, 'dtype': 'uint8'}, 'uint16': {'add_type': None, 'dtype': 'uint16'}, 'uint32': {'add_type': None, 'dtype': 'uint32'}, 'uint64': {'add_type': None, 'dtype': 'uint64'}, 'int8': {'add_type': None, 'dtype': 'int8'}, 'int16': {'add_type': None, 'dtype': 'int16'}, 'int32': {'add_type': None, 'dtype': 'int32'}, 'int64': {'add_type': None, 'dtype': 'int64'}, 'boolean': {'add_type': None, 'dtype': 'bool'}, 'base16': {'add_type': None, 'dtype': 'bytes'}, 'time': {'add_type': None, 'dtype': 'object'}, 'array': {'add_type': None, 'dtype': 'object'}, 'object': {'add_type': None, 'dtype': 'object'}, 'null': {'add_type': None, 'dtype': 'object'}, 'decimal64': {'add_type': None, 'dtype': 'object'}, 'ndarray': {'add_type': None, 'dtype': 'object'}, 'narray': {'add_type': None, 'dtype': 'object'}, 'ntv': {'add_type': None, 'dtype': 'object'}, 'point': {'add_type': None, 'dtype': 'object'}, 'line': {'add_type': None, 'dtype': 'object'}, 'polygon': {'add_type': None, 'dtype': 'object'}, 'field': {'add_type': None, 'dtype': 'object'}, 'tab': {'add_type': None, 'dtype': 'object'}, 'json': {'add_type': 'json', 'dtype': 'object'}, 'number': {'add_type': 'number', 'dtype': None}, 'float': {'add_type': 'float', 'dtype': 'float'}, 'int': {'add_type': 'int', 'dtype': 'int'}, 'month': {'add_type': 'month', 'dtype': 'int'}, 'day': {'add_type': 'day', 'dtype': 'int'}, 'wday': {'add_type': 'wday', 'dtype': 'int'}, 'yday': {'add_type': 'yday', 'dtype': 'int'}, 'week': {'add_type': 'week', 'dtype': 'int'}, 'hour': {'add_type': 'hour', 'dtype': 'int'}, 'minute': {'add_type': 'minute', 'dtype': 'int'}, 'second': {'add_type': 'second', 'dtype': 'int'}, 'binary': {'add_type': 'binary', 'dtype': 'str'}, 'base32': {'add_type': 'base32', 'dtype': 'str'}, 'base64': {'add_type': 'base64', 'dtype': 'str'}, 'period': {'add_type': 'period', 'dtype': 'str'}, 'duration': {'add_type': 'duration', 'dtype': 'str'}, 'jpointer': {'add_type': 'jpointer', 'dtype': 'str'}, 'uri': {'add_type': 'uri', 'dtype': 'str'}, 'uriref': {'add_type': 'uriref', 'dtype': 'str'}, 'iri': {'add_type': 'iri', 'dtype': 'str'}, 'iriref': {'add_type': 'iriref', 'dtype': 'str'}, 'email': {'add_type': 'email', 'dtype': 'str'}, 'regex': {'add_type': 'regex', 'dtype': 'str'}, 'hostname': {'add_type': 'hostname', 'dtype': 'str'}, 'ipv4': {'add_type': 'ipv4', 'dtype': 'str'}, 'ipv6': {'add_type': 'ipv6', 'dtype': 'str'}, 'file': {'add_type': 'file', 'dtype': 'str'}, 'geojson': {'add_type': 'geojson', 'dtype': 'str'}, 'geometry': {'add_type': 'geometry', 'dtype': 'object'}, 'timearray': {'add_type': 'timearray', 'dtype': 'object'}}
NP_NTYPE =
{None: 'number', 'datetime64[D]': 'date', 'datetime64[M]': 'yearmonth', 'datetime64[Y]': 'year', 'str': 'geojson', 'datetime64[s]': 'datetime', 'timedelta64[s]': 'timedelta', 'float16': 'float16', 'float32': 'float32', 'float64': 'float64', 'float96': 'float96', 'float128': 'float128', 'uint8': 'uint8', 'uint16': 'uint16', 'uint32': 'uint32', 'uint64': 'uint64', 'int8': 'int8', 'int16': 'int16', 'int32': 'int32', 'int64': 'int64', 'bool': 'boolean', 'bytes': 'base16', 'object': 'timearray', 'float': 'float', 'int': 'second'}