trd-utils 0.0.24__py3-none-any.whl → 0.0.26__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 CHANGED
@@ -1,3 +1,3 @@
1
1
 
2
- __version__ = "0.0.24"
2
+ __version__ = "0.0.26"
3
3
 
@@ -47,6 +47,14 @@ def get_real_attr(cls, attr_name):
47
47
  return None
48
48
 
49
49
 
50
+ def is_base_model_type(expected_type: type) -> bool:
51
+ return (
52
+ expected_type is not None
53
+ and expected_type != Any
54
+ and issubclass(expected_type, BaseModel)
55
+ )
56
+
57
+
50
58
  def is_any_type(target_type: type) -> bool:
51
59
  return target_type == Any or target_type is type(None)
52
60
 
@@ -92,13 +100,29 @@ def value_to_normal_obj(value, omit_none: bool = False):
92
100
  result[inner_key] = normalized_value
93
101
 
94
102
  return result
95
-
103
+
96
104
  if isinstance(value, datetime.datetime):
97
105
  return dt_to_ts(value)
98
106
 
99
107
  raise TypeError(f"unsupported type provided: {type(value)}")
100
108
 
101
109
 
110
+ def convert_to_expected_type(
111
+ expected_type: type,
112
+ value: Any,
113
+ default_value=None,
114
+ ):
115
+ try:
116
+ return expected_type(value)
117
+ except Exception:
118
+ if value == "":
119
+ try:
120
+ return expected_type()
121
+ except Exception:
122
+ return default_value
123
+ return default_value
124
+
125
+
102
126
  def generic_obj_to_value(
103
127
  expected_type: type,
104
128
  expected_type_args: tuple[type],
@@ -133,7 +157,7 @@ def generic_obj_to_value(
133
157
  )
134
158
  return result
135
159
 
136
- if isinstance(value, dict) and issubclass(expected_type, BaseModel):
160
+ if isinstance(value, dict) and is_base_model_type(expected_type=expected_type):
137
161
  if len(expected_type_args) > 1:
138
162
  raise ValueError(
139
163
  "unsupported operation: at this time we cannot have"
@@ -144,7 +168,10 @@ def generic_obj_to_value(
144
168
  if not expected_type_args:
145
169
  if value is None or isinstance(value, expected_type):
146
170
  return value
147
- return expected_type(value)
171
+ return convert_to_expected_type(
172
+ expected_type=expected_type,
173
+ value=value,
174
+ )
148
175
 
149
176
  raise TypeError(f"unsupported type: {type(value)}")
150
177
 
@@ -231,7 +258,9 @@ class BaseModel:
231
258
  )
232
259
 
233
260
  # Handle nested models
234
- elif isinstance(value, dict) and issubclass(expected_type, BaseModel):
261
+ elif isinstance(value, dict) and is_base_model_type(
262
+ expected_type=expected_type
263
+ ):
235
264
  value = expected_type(**value)
236
265
 
237
266
  elif isinstance(value, list):
@@ -266,7 +295,10 @@ class BaseModel:
266
295
  # Type checking
267
296
  elif not (is_any_type(expected_type) or isinstance(value, expected_type)):
268
297
  try:
269
- value = expected_type(value)
298
+ value = convert_to_expected_type(
299
+ expected_type=expected_type,
300
+ value=value,
301
+ )
270
302
  except Exception:
271
303
  raise TypeError(
272
304
  f"Field {corrected_key} must be of type {expected_type},"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: trd_utils
3
- Version: 0.0.24
3
+ Version: 0.0.26
4
4
  Summary: Common Basic Utils for Python3. By ALiwoto.
5
5
  Keywords: utils,trd_utils,basic-utils,common-utils
6
6
  Author: ALiwoto
@@ -1,4 +1,4 @@
1
- trd_utils/__init__.py,sha256=BdPJxY5Id-AZ9wkd-avrl6Mcm-AJ8wfeCAWD6kCw4vk,25
1
+ trd_utils/__init__.py,sha256=Wy_DfSBoQg4JGnSX4I-RPMxCXnRcYWQW07O41KIS8LM,25
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/common_utils/wallet_utils.py,sha256=OX9q2fymP0VfIWTRIRBP8W33cfyjLXimxMgPOsZe-3g,727
@@ -28,8 +28,8 @@ trd_utils/tradingview/__init__.py,sha256=H0QYb-O5qvy7qC3yswtlcSWLmeBnaS6oJ3Jtjvm
28
28
  trd_utils/tradingview/tradingview_client.py,sha256=g_eWYaCRQAL8Kvd-r6AnAdbH7Jha6C_GAyCuxh-RQUU,3917
29
29
  trd_utils/tradingview/tradingview_types.py,sha256=z21MXPVdWHAduEl3gSeMIRhxtBN9yK-jPYHfZSMIbSA,6144
30
30
  trd_utils/types_helper/__init__.py,sha256=lLbUiW1jUV1gjzTMFLthwkvF0hwauH-F_J2JZq--1U0,67
31
- trd_utils/types_helper/base_model.py,sha256=EEVEfIpXc5_HhQMAfWXan1i7WFmj7MZvCuwnw3zBgtI,11029
32
- trd_utils-0.0.24.dist-info/LICENSE,sha256=J1EP2xt87RjjmsTV1jTjHDQMLIM9FjdwEftTpw8hyv4,1067
33
- trd_utils-0.0.24.dist-info/METADATA,sha256=tvZ8_oljtkI3o1ZYl0YHhClobu3GHp4TA6UUnlWyNf4,1095
34
- trd_utils-0.0.24.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
35
- trd_utils-0.0.24.dist-info/RECORD,,
31
+ trd_utils/types_helper/base_model.py,sha256=Q2KK0r4UXP9PlWeIl6nxdAeCGB5InU5IkTNGAfJasfM,11808
32
+ trd_utils-0.0.26.dist-info/LICENSE,sha256=J1EP2xt87RjjmsTV1jTjHDQMLIM9FjdwEftTpw8hyv4,1067
33
+ trd_utils-0.0.26.dist-info/METADATA,sha256=CorynlnCD_2-PAjDTKSRYOdkt61sShVt-KqZlSQxSKo,1095
34
+ trd_utils-0.0.26.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
35
+ trd_utils-0.0.26.dist-info/RECORD,,