pyhdf5_handler.src.object_handler

  1from __future__ import annotations
  2
  3import numpy as np
  4import numbers
  5
  6
  7def generate_dict_structure(dictionary):
  8    """
  9    
 10    this function create a full dictionnary containing all the structure of an dictionnary in order to save it to an hdf5
 11
 12    Parameters
 13    ----------
 14    
 15    instance : python dictionary
 16        a custom dictionary.
 17
 18    Returns
 19    -------
 20    
 21    list or dict :
 22        A list or dictionary matching the structure of the python object.
 23    
 24    """
 25    key_data={}
 26    key_list = list()
 27    
 28    for attr,value in dictionary.items():
 29        
 30        try:
 31            if isinstance(value,dict):
 32                
 33                subkey_data=generate_dict_structure(value)
 34                if len(subkey_data)>0:
 35                    key_data.update({attr:subkey_data})
 36            
 37            elif isinstance(value, (list, tuple, np.ndarray, numbers.Number, str)):
 38                key_list.append(attr)
 39            
 40            elif type(value) == "method":
 41                key_list.append(attr)
 42            
 43            else:
 44                
 45                subkey_data = generate_object_structure(value)
 46                if len(subkey_data) > 0:
 47                    key_data.update({attr: subkey_data})
 48
 49        except:
 50            pass
 51    
 52    for attr, value in key_data.items():
 53        key_list.append({attr: value})
 54    
 55    return key_list
 56
 57
 58
 59def generate_object_structure(instance):
 60    """
 61    
 62    this function create a full dictionnary containing all the structure of an object in order to save it to an hdf5
 63
 64    Parameters
 65    ----------
 66    
 67    instance : object
 68        a custom python object.
 69
 70    Returns
 71    -------
 72    
 73    list or dict :
 74        A list or dictionary matching the structure of the python object.
 75    
 76    """
 77    key_data = {}
 78    key_list = list()
 79    return_list = False
 80    
 81    for attr in dir(instance):
 82        
 83        if not attr.startswith("_") and not attr in ["from_handle", "copy"]:
 84            
 85            try:
 86                value = getattr(instance, attr)
 87                
 88                if isinstance(value, (np.ndarray, list, tuple)):
 89
 90                    if isinstance(value, list):
 91                        value = np.array(value)
 92
 93                    if value.dtype == "object" or value.dtype.char == "U":
 94                        value = value.astype("S")
 95
 96                    key_list.append(attr)
 97                    return_list = True
 98                
 99                elif isinstance(value, dict):
100                    
101                    depp_key_data=generate_dict_structure(value)
102                    if len(depp_key_data) > 0:
103                        key_data.update({attr: depp_key_data})
104
105                elif isinstance(value, numbers.Number):
106                    key_list.append(attr)
107                    return_list = True
108
109                elif isinstance(value, str):
110                    key_list.append(attr)
111                    return_list = True
112
113                elif type(value) == "method":
114                    key_list.append(attr)
115                    return_list = True
116
117                else:
118                    
119                    depp_key_data = generate_object_structure(value)
120
121                    if len(depp_key_data) > 0:
122                        key_data.update({attr: depp_key_data})
123
124            except:
125                # raise ValueError("unable to parse attr", attr)
126                # print("unable to parse attr", attr, "skip it...")
127                pass
128
129    # print(key_data)
130    if return_list:
131        for attr, value in key_data.items():
132            key_list.append({attr: value})
133
134        return key_list
135
136    else:
137        return key_data
138
139
140def read_object_as_dict(instance, recursion_counter=0):
141    """
142    
143    create a dictionary from a custom python object
144
145    Parameters
146    ----------
147    
148    instance : object
149        an custom python object
150
151    Return
152    ------
153    
154    key_data: dict
155        an dictionary containing all keys and atributes of the object
156    
157    """
158    key_data = {}
159    # key_list = list()
160    # return_list = False
161    recursion_counter = 0
162    for attr in dir(instance):
163        #print(attr)
164        if not attr.startswith("_") and not attr in ["from_handle", "copy"]:
165            try:
166                value = getattr(instance, attr)
167                
168                if isinstance(value, (np.ndarray, list, tuple)):
169                    
170                    if isinstance(value, list):
171                        value = np.array(value).astype('U')
172
173                    if value.dtype == "object" or value.dtype.char == "U":
174                        value = value.astype("U")
175                    
176                    key_data.update({attr: value})
177                
178                elif isinstance(value, dict):
179                    key_data.update({attr: value})
180                
181                elif isinstance(value, numbers.Number):
182                    key_data.update({attr: value})
183
184                elif isinstance(value, str):
185                    key_data.update({attr: value})
186
187                elif type(value) == "method":
188                    next(attr)
189
190                else:
191                    
192                    depp_key_data = read_object_as_dict(
193                        value, recursion_counter=recursion_counter)
194                    
195                    recursion_counter = recursion_counter+1
196                    
197                    if len(depp_key_data) > 0:
198                        key_data.update({attr: depp_key_data})
199
200                    if recursion_counter > 100:
201                        print("recursion counter exxeed the limit of 100... return")
202                        return
203            except:
204                pass
205
206    return key_data
def generate_dict_structure(dictionary):
 9def generate_dict_structure(dictionary):
10    """
11    
12    this function create a full dictionnary containing all the structure of an dictionnary in order to save it to an hdf5
13
14    Parameters
15    ----------
16    
17    instance : python dictionary
18        a custom dictionary.
19
20    Returns
21    -------
22    
23    list or dict :
24        A list or dictionary matching the structure of the python object.
25    
26    """
27    key_data={}
28    key_list = list()
29    
30    for attr,value in dictionary.items():
31        
32        try:
33            if isinstance(value,dict):
34                
35                subkey_data=generate_dict_structure(value)
36                if len(subkey_data)>0:
37                    key_data.update({attr:subkey_data})
38            
39            elif isinstance(value, (list, tuple, np.ndarray, numbers.Number, str)):
40                key_list.append(attr)
41            
42            elif type(value) == "method":
43                key_list.append(attr)
44            
45            else:
46                
47                subkey_data = generate_object_structure(value)
48                if len(subkey_data) > 0:
49                    key_data.update({attr: subkey_data})
50
51        except:
52            pass
53    
54    for attr, value in key_data.items():
55        key_list.append({attr: value})
56    
57    return key_list

this function create a full dictionnary containing all the structure of an dictionnary in order to save it to an hdf5

Parameters

instance : python dictionary a custom dictionary.

Returns

list or dict : A list or dictionary matching the structure of the python object.

def generate_object_structure(instance):
 61def generate_object_structure(instance):
 62    """
 63    
 64    this function create a full dictionnary containing all the structure of an object in order to save it to an hdf5
 65
 66    Parameters
 67    ----------
 68    
 69    instance : object
 70        a custom python object.
 71
 72    Returns
 73    -------
 74    
 75    list or dict :
 76        A list or dictionary matching the structure of the python object.
 77    
 78    """
 79    key_data = {}
 80    key_list = list()
 81    return_list = False
 82    
 83    for attr in dir(instance):
 84        
 85        if not attr.startswith("_") and not attr in ["from_handle", "copy"]:
 86            
 87            try:
 88                value = getattr(instance, attr)
 89                
 90                if isinstance(value, (np.ndarray, list, tuple)):
 91
 92                    if isinstance(value, list):
 93                        value = np.array(value)
 94
 95                    if value.dtype == "object" or value.dtype.char == "U":
 96                        value = value.astype("S")
 97
 98                    key_list.append(attr)
 99                    return_list = True
100                
101                elif isinstance(value, dict):
102                    
103                    depp_key_data=generate_dict_structure(value)
104                    if len(depp_key_data) > 0:
105                        key_data.update({attr: depp_key_data})
106
107                elif isinstance(value, numbers.Number):
108                    key_list.append(attr)
109                    return_list = True
110
111                elif isinstance(value, str):
112                    key_list.append(attr)
113                    return_list = True
114
115                elif type(value) == "method":
116                    key_list.append(attr)
117                    return_list = True
118
119                else:
120                    
121                    depp_key_data = generate_object_structure(value)
122
123                    if len(depp_key_data) > 0:
124                        key_data.update({attr: depp_key_data})
125
126            except:
127                # raise ValueError("unable to parse attr", attr)
128                # print("unable to parse attr", attr, "skip it...")
129                pass
130
131    # print(key_data)
132    if return_list:
133        for attr, value in key_data.items():
134            key_list.append({attr: value})
135
136        return key_list
137
138    else:
139        return key_data

this function create a full dictionnary containing all the structure of an object in order to save it to an hdf5

Parameters

instance : object a custom python object.

Returns

list or dict : A list or dictionary matching the structure of the python object.

def read_object_as_dict(instance, recursion_counter=0):
142def read_object_as_dict(instance, recursion_counter=0):
143    """
144    
145    create a dictionary from a custom python object
146
147    Parameters
148    ----------
149    
150    instance : object
151        an custom python object
152
153    Return
154    ------
155    
156    key_data: dict
157        an dictionary containing all keys and atributes of the object
158    
159    """
160    key_data = {}
161    # key_list = list()
162    # return_list = False
163    recursion_counter = 0
164    for attr in dir(instance):
165        #print(attr)
166        if not attr.startswith("_") and not attr in ["from_handle", "copy"]:
167            try:
168                value = getattr(instance, attr)
169                
170                if isinstance(value, (np.ndarray, list, tuple)):
171                    
172                    if isinstance(value, list):
173                        value = np.array(value).astype('U')
174
175                    if value.dtype == "object" or value.dtype.char == "U":
176                        value = value.astype("U")
177                    
178                    key_data.update({attr: value})
179                
180                elif isinstance(value, dict):
181                    key_data.update({attr: value})
182                
183                elif isinstance(value, numbers.Number):
184                    key_data.update({attr: value})
185
186                elif isinstance(value, str):
187                    key_data.update({attr: value})
188
189                elif type(value) == "method":
190                    next(attr)
191
192                else:
193                    
194                    depp_key_data = read_object_as_dict(
195                        value, recursion_counter=recursion_counter)
196                    
197                    recursion_counter = recursion_counter+1
198                    
199                    if len(depp_key_data) > 0:
200                        key_data.update({attr: depp_key_data})
201
202                    if recursion_counter > 100:
203                        print("recursion counter exxeed the limit of 100... return")
204                        return
205            except:
206                pass
207
208    return key_data

create a dictionary from a custom python object

Parameters

instance : object an custom python object

Return

key_data: dict an dictionary containing all keys and atributes of the object