airsmodel 0.2.21__py3-none-any.whl → 0.4.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of airsmodel might be problematic. Click here for more details.
- airs/core/models/mapper.py +82 -68
- airs/core/models/model.py +296 -250
- {airsmodel-0.2.21.dist-info → airsmodel-0.4.3.dist-info}/METADATA +544 -94
- airsmodel-0.4.3.dist-info/RECORD +7 -0
- airsmodel-0.2.21.dist-info/RECORD +0 -7
- {airsmodel-0.2.21.dist-info → airsmodel-0.4.3.dist-info}/WHEEL +0 -0
- {airsmodel-0.2.21.dist-info → airsmodel-0.4.3.dist-info}/top_level.txt +0 -0
airs/core/models/mapper.py
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import json
|
|
2
|
+
import datetime
|
|
2
3
|
|
|
3
4
|
from airs.core.models.model import Item
|
|
4
5
|
|
|
5
6
|
|
|
6
|
-
def
|
|
7
|
-
|
|
7
|
+
def serialize_datetime(obj):
|
|
8
|
+
if isinstance(obj, datetime.datetime):
|
|
9
|
+
return int(obj.timestamp())
|
|
10
|
+
raise TypeError("Type not serializable")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def to_airs_item(item: Item) -> Item:
|
|
14
|
+
""" Takes an item and makes sure to map the namespace fields to AIRS Item. Keys can contain : as namespace seperator
|
|
8
15
|
|
|
9
16
|
Args:
|
|
10
17
|
item (Item): The item (with : namespace separator)
|
|
@@ -12,12 +19,12 @@ def to_airs_item(item: Item)->Item:
|
|
|
12
19
|
Returns:
|
|
13
20
|
item: The airs item
|
|
14
21
|
"""
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
dictionary = item.model_dump(exclude_unset=True, by_alias=True, exclude_none=True)
|
|
23
|
+
return Item(**__replaceKeys(dictionary, ":", "__"))
|
|
17
24
|
|
|
18
25
|
|
|
19
|
-
def to_dict(item: Item)->dict:
|
|
20
|
-
|
|
26
|
+
def to_dict(item: Item) -> dict:
|
|
27
|
+
""" create a dictionnary from the Item. Keys contain : as namespace seperator
|
|
21
28
|
|
|
22
29
|
Args:
|
|
23
30
|
item (Item): The item
|
|
@@ -25,83 +32,90 @@ def to_dict(item: Item)->dict:
|
|
|
25
32
|
Returns:
|
|
26
33
|
dict: The dictionary. Keys contain : as namespace seperator
|
|
27
34
|
"""
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
dictionary = item.model_dump(exclude_unset=True, by_alias=True, exclude_none=True)
|
|
36
|
+
return __replaceKeys(dictionary, "__", ":")
|
|
30
37
|
|
|
31
|
-
def to_airs_dict(item: Item)->dict:
|
|
32
|
-
""" create a dictionnary from the Item. Keys contain __ as namespace seperator
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
def to_airs_dict(item: Item) -> dict:
|
|
40
|
+
""" create a dictionnary from the Item. Keys contain __ as namespace seperator
|
|
36
41
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"""
|
|
40
|
-
dictionary=item.model_dump(exclude_unset=True, by_alias=False, exclude_none=True)
|
|
41
|
-
return dictionary
|
|
42
|
+
Args:
|
|
43
|
+
item (Item): The item
|
|
42
44
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
Returns:
|
|
46
|
+
dict: The dictionary. Keys contain __ as namespace seperator
|
|
47
|
+
"""
|
|
48
|
+
dictionary = item.model_dump(exclude_unset=True, by_alias=False, exclude_none=True)
|
|
49
|
+
return dictionary
|
|
45
50
|
|
|
46
|
-
Args:
|
|
47
|
-
item (Item): The item
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"""
|
|
52
|
-
return json.dumps(to_airs_dict(item), default=str, indent=2)
|
|
52
|
+
def to_airs_json(item: Item) -> str:
|
|
53
|
+
""" create a json from the Item. Keys contain __ as namespace seperator
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
Args:
|
|
56
|
+
item (Item): The item
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
Returns:
|
|
59
|
+
str: the json. Keys contain __ as namespace seperator
|
|
60
|
+
"""
|
|
61
|
+
return json.dumps(to_airs_dict(item), indent=2, default=serialize_datetime)
|
|
59
62
|
|
|
60
|
-
Returns:
|
|
61
|
-
str: the json. Keys contain : as namespace seperator
|
|
62
|
-
"""
|
|
63
|
-
return json.dumps(to_dict(item), default=str, indent=2)
|
|
64
63
|
|
|
65
|
-
def
|
|
66
|
-
|
|
64
|
+
def to_json(item: Item) -> str:
|
|
65
|
+
""" create a json from the Item. Keys contain : as namespace seperator
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
Args:
|
|
68
|
+
item (Item): The item
|
|
70
69
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
Returns:
|
|
71
|
+
str: the json. Keys contain : as namespace seperator
|
|
72
|
+
"""
|
|
73
|
+
return json.dumps(to_dict(item), indent=2, default=serialize_datetime)
|
|
75
74
|
|
|
76
|
-
def item_from_json(json_string:str)->Item:
|
|
77
|
-
""" load a json Item
|
|
78
75
|
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
def item_from_json_file(jsonfile) -> Item:
|
|
77
|
+
""" load a json file containing an Item
|
|
81
78
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"""
|
|
85
|
-
return Item(**__replaceKeys(json.loads(json_string), ":", "__"))
|
|
79
|
+
Args:
|
|
80
|
+
jsonfile (file): the json file
|
|
86
81
|
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
Returns:
|
|
83
|
+
Item: _description_
|
|
84
|
+
"""
|
|
85
|
+
return Item(**__replaceKeys(json.load(jsonfile), ":", "__"))
|
|
89
86
|
|
|
90
|
-
Args:
|
|
91
|
-
object (dict): the Item as dict
|
|
92
87
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
88
|
+
def item_from_json(json_string: str) -> Item:
|
|
89
|
+
""" load a json Item
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
json_string (str): the Item as json
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
Item: the Item
|
|
96
|
+
"""
|
|
97
|
+
return Item(**__replaceKeys(json.loads(json_string), ":", "__"))
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def item_from_dict(object: dict) -> Item:
|
|
101
|
+
""" load a dict Item
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
object (dict): the Item as dict
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
Item: the Item
|
|
108
|
+
"""
|
|
109
|
+
return Item(**__replaceKeys(object, ":", "__"))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def __replaceKeys(o, before: str, after: str):
|
|
113
|
+
if type(o) is dict:
|
|
114
|
+
d = {}
|
|
115
|
+
for key in o:
|
|
116
|
+
d[key.replace(before, after)] = __replaceKeys(o[key], before, after)
|
|
117
|
+
return d
|
|
118
|
+
if type(o) is list:
|
|
119
|
+
return list(map(lambda elt: __replaceKeys(elt, before, after), o))
|
|
120
|
+
else:
|
|
121
|
+
return o
|