trd-utils 0.0.5__py3-none-any.whl → 0.0.6__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 trd-utils might be problematic. Click here for more details.
- trd_utils/__init__.py +1 -1
- trd_utils/types_helper/base_model.py +31 -19
- {trd_utils-0.0.5.dist-info → trd_utils-0.0.6.dist-info}/METADATA +1 -1
- {trd_utils-0.0.5.dist-info → trd_utils-0.0.6.dist-info}/RECORD +6 -6
- {trd_utils-0.0.5.dist-info → trd_utils-0.0.6.dist-info}/LICENSE +0 -0
- {trd_utils-0.0.5.dist-info → trd_utils-0.0.6.dist-info}/WHEEL +0 -0
trd_utils/__init__.py
CHANGED
|
@@ -3,7 +3,7 @@ from typing import (
|
|
|
3
3
|
Union,
|
|
4
4
|
get_type_hints,
|
|
5
5
|
Any,
|
|
6
|
-
get_args as get_type_args
|
|
6
|
+
get_args as get_type_args,
|
|
7
7
|
)
|
|
8
8
|
|
|
9
9
|
from trd_utils.html_utils.html_formats import camel_to_snake
|
|
@@ -19,6 +19,7 @@ ULTRA_LIST_ENABLED: bool = False
|
|
|
19
19
|
# attribute names are converted to snake_case.
|
|
20
20
|
SET_CAMEL_ATTR_NAMES = False
|
|
21
21
|
|
|
22
|
+
|
|
22
23
|
def get_my_field_types(cls):
|
|
23
24
|
type_hints = {}
|
|
24
25
|
for current_cls in cls.__class__.__mro__:
|
|
@@ -27,24 +28,31 @@ def get_my_field_types(cls):
|
|
|
27
28
|
type_hints.update(get_type_hints(current_cls))
|
|
28
29
|
return type_hints
|
|
29
30
|
|
|
31
|
+
|
|
30
32
|
def get_real_attr(cls, attr_name):
|
|
31
33
|
if cls is None:
|
|
32
34
|
return None
|
|
33
|
-
|
|
35
|
+
|
|
34
36
|
if isinstance(cls, dict):
|
|
35
37
|
return cls.get(attr_name, None)
|
|
36
38
|
|
|
37
39
|
if hasattr(cls, attr_name):
|
|
38
40
|
return getattr(cls, attr_name)
|
|
39
|
-
|
|
41
|
+
|
|
40
42
|
return None
|
|
41
43
|
|
|
44
|
+
|
|
45
|
+
def is_any_type(target_type: type) -> bool:
|
|
46
|
+
return target_type == Any or target_type is type(None)
|
|
47
|
+
|
|
48
|
+
|
|
42
49
|
class UltraList(list):
|
|
43
50
|
def __getattr__(self, attr):
|
|
44
51
|
if len(self) == 0:
|
|
45
52
|
return None
|
|
46
53
|
return UltraList([get_real_attr(item, attr) for item in self])
|
|
47
54
|
|
|
55
|
+
|
|
48
56
|
def convert_to_ultra_list(value: Any) -> UltraList:
|
|
49
57
|
if not value:
|
|
50
58
|
return UltraList()
|
|
@@ -61,7 +69,7 @@ def convert_to_ultra_list(value: Any) -> UltraList:
|
|
|
61
69
|
return tuple(convert_to_ultra_list(v) for v in value)
|
|
62
70
|
elif isinstance(value, set):
|
|
63
71
|
return {convert_to_ultra_list(v) for v in value}
|
|
64
|
-
|
|
72
|
+
|
|
65
73
|
for attr, attr_value in get_my_field_types(value).items():
|
|
66
74
|
if isinstance(attr_value, list):
|
|
67
75
|
setattr(value, attr, convert_to_ultra_list(getattr(value, attr)))
|
|
@@ -70,6 +78,7 @@ def convert_to_ultra_list(value: Any) -> UltraList:
|
|
|
70
78
|
except Exception:
|
|
71
79
|
return value
|
|
72
80
|
|
|
81
|
+
|
|
73
82
|
class BaseModel:
|
|
74
83
|
def __init__(self, **kwargs):
|
|
75
84
|
annotations = get_my_field_types(self)
|
|
@@ -83,17 +92,19 @@ class BaseModel:
|
|
|
83
92
|
# just ignore and continue
|
|
84
93
|
annotations[key] = Any
|
|
85
94
|
annotations[corrected_key] = Any
|
|
86
|
-
|
|
95
|
+
|
|
87
96
|
expected_type = annotations[corrected_key]
|
|
88
97
|
if hasattr(self, "_get_" + corrected_key + "_type"):
|
|
89
98
|
try:
|
|
90
|
-
overridden_type = getattr(self, "_get_" + corrected_key + "_type")(
|
|
99
|
+
overridden_type = getattr(self, "_get_" + corrected_key + "_type")(
|
|
100
|
+
kwargs
|
|
101
|
+
)
|
|
91
102
|
if overridden_type:
|
|
92
103
|
expected_type = overridden_type
|
|
93
104
|
except Exception:
|
|
94
105
|
pass
|
|
95
|
-
|
|
96
|
-
is_optional_type = getattr(expected_type,
|
|
106
|
+
|
|
107
|
+
is_optional_type = getattr(expected_type, "_name", None) == "Optional"
|
|
97
108
|
# maybe in the future we can have some other usages for is_optional_type
|
|
98
109
|
# variable or something like that.
|
|
99
110
|
if is_optional_type:
|
|
@@ -102,11 +113,11 @@ class BaseModel:
|
|
|
102
113
|
except Exception:
|
|
103
114
|
# something went wrong, just ignore and continue
|
|
104
115
|
expected_type = Any
|
|
105
|
-
|
|
116
|
+
|
|
106
117
|
# Handle nested models
|
|
107
118
|
if isinstance(value, dict) and issubclass(expected_type, BaseModel):
|
|
108
119
|
value = expected_type(**value)
|
|
109
|
-
|
|
120
|
+
|
|
110
121
|
elif isinstance(value, list):
|
|
111
122
|
type_args = get_type_args(expected_type)
|
|
112
123
|
if not type_args:
|
|
@@ -118,27 +129,29 @@ class BaseModel:
|
|
|
118
129
|
nested_type = type_args[0]
|
|
119
130
|
if issubclass(nested_type, BaseModel):
|
|
120
131
|
value = [nested_type(**item) for item in value]
|
|
121
|
-
|
|
132
|
+
|
|
122
133
|
if ULTRA_LIST_ENABLED and isinstance(value, list):
|
|
123
134
|
value = convert_to_ultra_list(value)
|
|
124
|
-
|
|
135
|
+
|
|
125
136
|
# Type checking
|
|
126
|
-
elif expected_type
|
|
137
|
+
elif not (is_any_type(expected_type) or isinstance(value, expected_type)):
|
|
127
138
|
try:
|
|
128
139
|
value = expected_type(value)
|
|
129
140
|
except Exception:
|
|
130
|
-
raise TypeError(
|
|
131
|
-
|
|
132
|
-
|
|
141
|
+
raise TypeError(
|
|
142
|
+
f"Field {corrected_key} must be of type {expected_type},"
|
|
143
|
+
+ f" but it's {type(value)}"
|
|
144
|
+
)
|
|
145
|
+
|
|
133
146
|
setattr(self, corrected_key, value)
|
|
134
147
|
if SET_CAMEL_ATTR_NAMES and key != corrected_key:
|
|
135
148
|
setattr(self, key, value)
|
|
136
|
-
|
|
149
|
+
|
|
137
150
|
# Check if all required fields are present
|
|
138
151
|
# for field in self.__annotations__:
|
|
139
152
|
# if not hasattr(self, field):
|
|
140
153
|
# raise ValueError(f"Missing required field: {field}")
|
|
141
|
-
|
|
154
|
+
|
|
142
155
|
@classmethod
|
|
143
156
|
def deserialize(cls, json_data: Union[str, dict]):
|
|
144
157
|
if isinstance(json_data, str):
|
|
@@ -146,4 +159,3 @@ class BaseModel:
|
|
|
146
159
|
else:
|
|
147
160
|
data = json_data
|
|
148
161
|
return cls(**data)
|
|
149
|
-
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
trd_utils/__init__.py,sha256=
|
|
1
|
+
trd_utils/__init__.py,sha256=BbjDqNvJRwxv1bgE42Omkkm7kDmKwwd0BpdQwilcoEE,24
|
|
2
2
|
trd_utils/cipher/__init__.py,sha256=V05KNuzQwCic-ihMVHlC8sENaJGc3I8MCb4pg4849X8,1765
|
|
3
3
|
trd_utils/common_utils/float_utils.py,sha256=W-jv7nzjl88xwGB6gsEXmDDhF6DseOrrVT2qx7OvyCo,266
|
|
4
4
|
trd_utils/exchanges/__init__.py,sha256=SQJt5cIXh305miWuDumkOLZHzqDUyOqSmlhTT9Xc9RY,180
|
|
@@ -16,8 +16,8 @@ trd_utils/tradingview/__init__.py,sha256=H0QYb-O5qvy7qC3yswtlcSWLmeBnaS6oJ3Jtjvm
|
|
|
16
16
|
trd_utils/tradingview/tradingview_client.py,sha256=g_eWYaCRQAL8Kvd-r6AnAdbH7Jha6C_GAyCuxh-RQUU,3917
|
|
17
17
|
trd_utils/tradingview/tradingview_types.py,sha256=z21MXPVdWHAduEl3gSeMIRhxtBN9yK-jPYHfZSMIbSA,6144
|
|
18
18
|
trd_utils/types_helper/__init__.py,sha256=SB9_5bQkuxV059AKC4cXYwsLPT3lM4LOu2m8LpMhZlQ,60
|
|
19
|
-
trd_utils/types_helper/base_model.py,sha256=
|
|
20
|
-
trd_utils-0.0.
|
|
21
|
-
trd_utils-0.0.
|
|
22
|
-
trd_utils-0.0.
|
|
23
|
-
trd_utils-0.0.
|
|
19
|
+
trd_utils/types_helper/base_model.py,sha256=xclJlC8BxaWzJ06Gb88lEYOFHel_pFeRz1EF_tQU04k,5698
|
|
20
|
+
trd_utils-0.0.6.dist-info/LICENSE,sha256=J1EP2xt87RjjmsTV1jTjHDQMLIM9FjdwEftTpw8hyv4,1067
|
|
21
|
+
trd_utils-0.0.6.dist-info/METADATA,sha256=_9LU-hyREa10cefKN1qa4YqPMq_U68UxQYTCq-ZNcbI,1094
|
|
22
|
+
trd_utils-0.0.6.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
23
|
+
trd_utils-0.0.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|