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.

@@ -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 to_airs_item(item: Item)->Item:
7
- """ Takes an item and makes sure to map the namespace fields to AIRS Item. Keys can contain : as namespace seperator
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
- dictionary=item.model_dump(exclude_unset=True, by_alias=True, exclude_none=True)
16
- return Item(**__replaceKeys(dictionary, ":", "__"))
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
- """ create a dictionnary from the Item. Keys contain : as namespace seperator
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
- dictionary=item.model_dump(exclude_unset=True, by_alias=True, exclude_none=True)
29
- return __replaceKeys(dictionary, "__", ":")
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
- Args:
35
- item (Item): The item
39
+ def to_airs_dict(item: Item) -> dict:
40
+ """ create a dictionnary from the Item. Keys contain __ as namespace seperator
36
41
 
37
- Returns:
38
- dict: The dictionary. Keys contain __ as namespace seperator
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
- def to_airs_json(item: Item)->str:
44
- """ create a json from the Item. Keys contain __ as namespace seperator
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
- Returns:
50
- str: the json. Keys contain __ as namespace seperator
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
- def to_json(item: Item)->str:
55
- """ create a json from the Item. Keys contain : as namespace seperator
55
+ Args:
56
+ item (Item): The item
56
57
 
57
- Args:
58
- item (Item): The item
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 item_from_json_file(jsonfile)->Item:
66
- """ load a json file containing an Item
64
+ def to_json(item: Item) -> str:
65
+ """ create a json from the Item. Keys contain : as namespace seperator
67
66
 
68
- Args:
69
- jsonfile (file): the json file
67
+ Args:
68
+ item (Item): The item
70
69
 
71
- Returns:
72
- Item: _description_
73
- """
74
- return Item(**__replaceKeys(json.load(jsonfile), ":", "__"))
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
- Args:
80
- json_string (str): the Item as json
76
+ def item_from_json_file(jsonfile) -> Item:
77
+ """ load a json file containing an Item
81
78
 
82
- Returns:
83
- Item: the Item
84
- """
85
- return Item(**__replaceKeys(json.loads(json_string), ":", "__"))
79
+ Args:
80
+ jsonfile (file): the json file
86
81
 
87
- def item_from_dict(object:dict)->Item:
88
- """ load a dict Item
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
- Returns:
94
- Item: the Item
95
- """
96
- return Item(**__replaceKeys(object, ":", "__"))
97
-
98
- def __replaceKeys(o, before:str, after:str):
99
- if type(o) is dict:
100
- d={}
101
- for key in o:
102
- d[key.replace(before, after)]=__replaceKeys(o[key], before, after)
103
- return d
104
- if type(o) is list:
105
- return list(map(lambda elt:__replaceKeys(elt, before, after), o))
106
- else:
107
- return o
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