dc-cmnlib 0.0.1__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.
- dc/__init__.py +0 -0
- dc/cmnlib/__init__.py +0 -0
- dc/cmnlib/basicdata/__init__.py +6 -0
- dc/cmnlib/basicdata/convert.py +391 -0
- dc/cmnlib/basicdata/data.py +586 -0
- dc/cmnlib/basicdata/object.py +506 -0
- dc/cmnlib/collection/collection.py +405 -0
- dc/cmnlib/database/__init__.py +0 -0
- dc/cmnlib/database/db.py +213 -0
- dc/cmnlib/database/db1.py +211 -0
- dc/cmnlib/database/db2.py +147 -0
- dc/cmnlib/definitions/dual.py +62 -0
- dc/cmnlib/definitions/result.py +128 -0
- dc/cmnlib/definitions/result_old.py +201 -0
- dc/cmnlib/io/__init__.py +7 -0
- dc/cmnlib/io/file/__init__.py +0 -0
- dc/cmnlib/io/file/file.py +540 -0
- dc/cmnlib/io/log/__init__.py +0 -0
- dc/cmnlib/io/log/log.py +50 -0
- dc/cmnlib/misc/__init__.py +0 -0
- dc/cmnlib/misc/action.py +292 -0
- dc/cmnlib/misc/serialize.py +94 -0
- dc/cmnlib/net/__init__.py +7 -0
- dc/cmnlib/net/net_utils.py +464 -0
- dc/cmnlib/sys/__init__.py +7 -0
- dc/cmnlib/sys/sys_utils.py +580 -0
- dc_cmnlib-0.0.1.dist-info/METADATA +9 -0
- dc_cmnlib-0.0.1.dist-info/RECORD +30 -0
- dc_cmnlib-0.0.1.dist-info/WHEEL +5 -0
- dc_cmnlib-0.0.1.dist-info/top_level.txt +1 -0
dc/__init__.py
ADDED
|
File without changes
|
dc/cmnlib/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
from dc.cmnlib.basicdata.data import DataHelper
|
|
2
|
+
from dc.cmnlib.basicdata.convert import ConvertHelper
|
|
3
|
+
from .object import ObjectHelper
|
|
4
|
+
from dc.cmnlib.misc.serialize import SerializeHelper
|
|
5
|
+
|
|
6
|
+
__all__ = ['DataHelper', 'ConvertHelper', 'ObjectHelper', 'SerializeHelper']
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"""数据转换帮助类
|
|
2
|
+
|
|
3
|
+
提供各种数据类型之间的转换功能,支持安全转换,避免异常。
|
|
4
|
+
所有方法均为静态方法,线程安全,适用于多线程环境。
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import xml.etree.ElementTree as ET
|
|
8
|
+
from typing import Any, List, Dict, Optional, Union
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ConvertHelper:
|
|
12
|
+
"""数据转换帮助类"""
|
|
13
|
+
|
|
14
|
+
@staticmethod
|
|
15
|
+
def to_str(value: Any, default: str = "") -> str:
|
|
16
|
+
"""安全转换为字符串
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
value: 任意类型的值
|
|
20
|
+
default: 默认值,转换失败时返回
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
str: 转换后的字符串
|
|
24
|
+
"""
|
|
25
|
+
if value is None:
|
|
26
|
+
return default
|
|
27
|
+
try:
|
|
28
|
+
return str(value)
|
|
29
|
+
except (TypeError, ValueError):
|
|
30
|
+
return default
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def to_int(value: Any, default: int = 0) -> int:
|
|
34
|
+
"""安全转换为整数
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
value: 任意类型的值
|
|
38
|
+
default: 默认值,转换失败时返回
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
int: 转换后的整数
|
|
42
|
+
"""
|
|
43
|
+
if value is None:
|
|
44
|
+
return default
|
|
45
|
+
try:
|
|
46
|
+
if isinstance(value, float):
|
|
47
|
+
return int(value)
|
|
48
|
+
return int(value)
|
|
49
|
+
except (TypeError, ValueError):
|
|
50
|
+
return default
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
def to_float(value: Any, default: float = 0.0) -> float:
|
|
54
|
+
"""安全转换为浮点数
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
value: 任意类型的值
|
|
58
|
+
default: 默认值,转换失败时返回
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
float: 转换后的浮点数
|
|
62
|
+
"""
|
|
63
|
+
if value is None:
|
|
64
|
+
return default
|
|
65
|
+
try:
|
|
66
|
+
return float(value)
|
|
67
|
+
except (TypeError, ValueError):
|
|
68
|
+
return default
|
|
69
|
+
|
|
70
|
+
@staticmethod
|
|
71
|
+
def to_bool(value: Any, default: bool = False) -> bool:
|
|
72
|
+
"""安全转换为布尔值
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
value: 任意类型的值
|
|
76
|
+
default: 默认值,转换失败时返回
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
bool: 转换后的布尔值
|
|
80
|
+
"""
|
|
81
|
+
if value is None:
|
|
82
|
+
return default
|
|
83
|
+
try:
|
|
84
|
+
if isinstance(value, bool):
|
|
85
|
+
return value
|
|
86
|
+
if isinstance(value, str):
|
|
87
|
+
lower = value.lower().strip()
|
|
88
|
+
return lower in ('true', 'yes', '1', 'on')
|
|
89
|
+
if isinstance(value, (int, float)):
|
|
90
|
+
return value != 0
|
|
91
|
+
return bool(value)
|
|
92
|
+
except (TypeError, ValueError):
|
|
93
|
+
return default
|
|
94
|
+
|
|
95
|
+
@staticmethod
|
|
96
|
+
def to_list(value: Any, default: List[Any] = None) -> List[Any]:
|
|
97
|
+
"""安全转换为列表
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
value: 任意类型的值
|
|
101
|
+
default: 默认值,转换失败时返回
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
List[Any]: 转换后的列表
|
|
105
|
+
"""
|
|
106
|
+
if default is None:
|
|
107
|
+
default = []
|
|
108
|
+
if value is None:
|
|
109
|
+
return default
|
|
110
|
+
try:
|
|
111
|
+
if isinstance(value, list):
|
|
112
|
+
return value
|
|
113
|
+
if isinstance(value, (tuple, set)):
|
|
114
|
+
return list(value)
|
|
115
|
+
if isinstance(value, str):
|
|
116
|
+
return [value]
|
|
117
|
+
return [value]
|
|
118
|
+
except (TypeError, ValueError):
|
|
119
|
+
return default
|
|
120
|
+
|
|
121
|
+
@staticmethod
|
|
122
|
+
def to_dict(value: Any, default: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
123
|
+
"""安全转换为字典
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
value: 任意类型的值
|
|
127
|
+
default: 默认值,转换失败时返回
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
Dict[str, Any]: 转换后的字典
|
|
131
|
+
"""
|
|
132
|
+
if default is None:
|
|
133
|
+
default = {}
|
|
134
|
+
if value is None:
|
|
135
|
+
return default
|
|
136
|
+
try:
|
|
137
|
+
if isinstance(value, dict):
|
|
138
|
+
return value
|
|
139
|
+
return default
|
|
140
|
+
except (TypeError, ValueError):
|
|
141
|
+
return default
|
|
142
|
+
|
|
143
|
+
@staticmethod
|
|
144
|
+
def json_to_dict(json_str: str, default: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
145
|
+
"""将 JSON 字符串转换为字典
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
json_str: JSON 字符串
|
|
149
|
+
default: 默认值,转换失败时返回
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
Dict[str, Any]: 转换后的字典
|
|
153
|
+
"""
|
|
154
|
+
if default is None:
|
|
155
|
+
default = {}
|
|
156
|
+
if not json_str or not isinstance(json_str, str):
|
|
157
|
+
return default
|
|
158
|
+
try:
|
|
159
|
+
return json.loads(json_str)
|
|
160
|
+
except (json.JSONDecodeError, TypeError, ValueError):
|
|
161
|
+
return default
|
|
162
|
+
|
|
163
|
+
@staticmethod
|
|
164
|
+
def dict_to_json(dictionary: Dict[str, Any], indent: int = 2) -> str:
|
|
165
|
+
"""将字典转换为 JSON 字符串
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
dictionary: 字典对象
|
|
169
|
+
indent: 缩进空格数,默认为 2
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
str: JSON 字符串
|
|
173
|
+
"""
|
|
174
|
+
if not dictionary or not isinstance(dictionary, dict):
|
|
175
|
+
return "{}"
|
|
176
|
+
try:
|
|
177
|
+
return json.dumps(dictionary, indent=indent, ensure_ascii=False)
|
|
178
|
+
except (TypeError, ValueError):
|
|
179
|
+
return "{}"
|
|
180
|
+
|
|
181
|
+
@staticmethod
|
|
182
|
+
def json_to_list(json_str: str, default: List[Any] = None) -> List[Any]:
|
|
183
|
+
"""将 JSON 字符串转换为列表
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
json_str: JSON 字符串
|
|
187
|
+
default: 默认值,转换失败时返回
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
List[Any]: 转换后的列表
|
|
191
|
+
"""
|
|
192
|
+
if default is None:
|
|
193
|
+
default = []
|
|
194
|
+
if not json_str or not isinstance(json_str, str):
|
|
195
|
+
return default
|
|
196
|
+
try:
|
|
197
|
+
result = json.loads(json_str)
|
|
198
|
+
if isinstance(result, list):
|
|
199
|
+
return result
|
|
200
|
+
return [result]
|
|
201
|
+
except (json.JSONDecodeError, TypeError, ValueError):
|
|
202
|
+
return default
|
|
203
|
+
|
|
204
|
+
@staticmethod
|
|
205
|
+
def list_to_json(lst: List[Any], indent: int = 2) -> str:
|
|
206
|
+
"""将列表转换为 JSON 字符串
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
lst: 列表对象
|
|
210
|
+
indent: 缩进空格数,默认为 2
|
|
211
|
+
|
|
212
|
+
Returns:
|
|
213
|
+
str: JSON 字符串
|
|
214
|
+
"""
|
|
215
|
+
if not lst or not isinstance(lst, list):
|
|
216
|
+
return "[]"
|
|
217
|
+
try:
|
|
218
|
+
return json.dumps(lst, indent=indent, ensure_ascii=False)
|
|
219
|
+
except (TypeError, ValueError):
|
|
220
|
+
return "[]"
|
|
221
|
+
|
|
222
|
+
@staticmethod
|
|
223
|
+
def xml_to_dict(xml_str: str, default: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
224
|
+
"""将 XML 字符串转换为字典
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
xml_str: XML 字符串
|
|
228
|
+
default: 默认值,转换失败时返回
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
Dict[str, Any]: 转换后的字典
|
|
232
|
+
"""
|
|
233
|
+
if default is None:
|
|
234
|
+
default = {}
|
|
235
|
+
if not xml_str or not isinstance(xml_str, str):
|
|
236
|
+
return default
|
|
237
|
+
try:
|
|
238
|
+
root = ET.fromstring(xml_str)
|
|
239
|
+
return ConvertHelper._xml_element_to_dict(root)
|
|
240
|
+
except (ET.ParseError, TypeError, ValueError):
|
|
241
|
+
return default
|
|
242
|
+
|
|
243
|
+
@staticmethod
|
|
244
|
+
def _xml_element_to_dict(element: ET.Element) -> Dict[str, Any]:
|
|
245
|
+
"""将 XML 元素转换为字典(内部方法)
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
element: XML 元素
|
|
249
|
+
|
|
250
|
+
Returns:
|
|
251
|
+
Dict[str, Any]: 转换后的字典
|
|
252
|
+
"""
|
|
253
|
+
result = {}
|
|
254
|
+
if element.text and element.text.strip():
|
|
255
|
+
if len(element) == 0:
|
|
256
|
+
return element.text.strip()
|
|
257
|
+
|
|
258
|
+
for child in element:
|
|
259
|
+
child_dict = ConvertHelper._xml_element_to_dict(child)
|
|
260
|
+
if child.tag in result:
|
|
261
|
+
if not isinstance(result[child.tag], list):
|
|
262
|
+
result[child.tag] = [result[child.tag]]
|
|
263
|
+
result[child.tag].append(child_dict)
|
|
264
|
+
else:
|
|
265
|
+
result[child.tag] = child_dict
|
|
266
|
+
|
|
267
|
+
result.update(element.attrib)
|
|
268
|
+
return result
|
|
269
|
+
|
|
270
|
+
@staticmethod
|
|
271
|
+
def dict_to_xml(dictionary: Dict[str, Any], root_tag: str = 'root') -> str:
|
|
272
|
+
"""将字典转换为 XML 字符串
|
|
273
|
+
|
|
274
|
+
Args:
|
|
275
|
+
dictionary: 字典对象
|
|
276
|
+
root_tag: 根节点标签名,默认为 'root'
|
|
277
|
+
|
|
278
|
+
Returns:
|
|
279
|
+
str: XML 字符串
|
|
280
|
+
"""
|
|
281
|
+
if not dictionary or not isinstance(dictionary, dict):
|
|
282
|
+
return f"<{root_tag}/>"
|
|
283
|
+
try:
|
|
284
|
+
root = ET.Element(root_tag)
|
|
285
|
+
ConvertHelper._dict_to_xml_element(root, dictionary)
|
|
286
|
+
return ET.tostring(root, encoding='unicode', method='xml')
|
|
287
|
+
except (TypeError, ValueError):
|
|
288
|
+
return f"<{root_tag}/>"
|
|
289
|
+
|
|
290
|
+
@staticmethod
|
|
291
|
+
def _dict_to_xml_element(parent: ET.Element, dictionary: Dict[str, Any]) -> None:
|
|
292
|
+
"""将字典转换为 XML 元素(内部方法)
|
|
293
|
+
|
|
294
|
+
Args:
|
|
295
|
+
parent: 父元素
|
|
296
|
+
dictionary: 字典对象
|
|
297
|
+
"""
|
|
298
|
+
for key, value in dictionary.items():
|
|
299
|
+
if isinstance(value, dict):
|
|
300
|
+
child = ET.SubElement(parent, key)
|
|
301
|
+
ConvertHelper._dict_to_xml_element(child, value)
|
|
302
|
+
elif isinstance(value, list):
|
|
303
|
+
for item in value:
|
|
304
|
+
child = ET.SubElement(parent, key)
|
|
305
|
+
if isinstance(item, dict):
|
|
306
|
+
ConvertHelper._dict_to_xml_element(child, item)
|
|
307
|
+
else:
|
|
308
|
+
child.text = str(item)
|
|
309
|
+
else:
|
|
310
|
+
child = ET.SubElement(parent, key)
|
|
311
|
+
child.text = str(value)
|
|
312
|
+
|
|
313
|
+
@staticmethod
|
|
314
|
+
def to_bytes(value: Any, encoding: str = 'utf-8', errors: str = 'replace') -> bytes:
|
|
315
|
+
"""安全转换为字节数组
|
|
316
|
+
|
|
317
|
+
Args:
|
|
318
|
+
value: 任意类型的值
|
|
319
|
+
encoding: 编码方式,默认为 'utf-8'
|
|
320
|
+
errors: 错误处理方式,默认为 'replace'
|
|
321
|
+
|
|
322
|
+
Returns:
|
|
323
|
+
bytes: 转换后的字节数组
|
|
324
|
+
"""
|
|
325
|
+
if value is None:
|
|
326
|
+
return b""
|
|
327
|
+
try:
|
|
328
|
+
if isinstance(value, bytes):
|
|
329
|
+
return value
|
|
330
|
+
if isinstance(value, str):
|
|
331
|
+
return value.encode(encoding, errors=errors)
|
|
332
|
+
return str(value).encode(encoding, errors=errors)
|
|
333
|
+
except (TypeError, ValueError):
|
|
334
|
+
return b""
|
|
335
|
+
|
|
336
|
+
@staticmethod
|
|
337
|
+
def bytes_to_str(value: bytes, encoding: str = 'utf-8', errors: str = 'replace') -> str:
|
|
338
|
+
"""将字节数组转换为字符串
|
|
339
|
+
|
|
340
|
+
Args:
|
|
341
|
+
value: 字节数组
|
|
342
|
+
encoding: 编码方式,默认为 'utf-8'
|
|
343
|
+
errors: 错误处理方式,默认为 'replace'
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
str: 转换后的字符串
|
|
347
|
+
"""
|
|
348
|
+
if value is None:
|
|
349
|
+
return ""
|
|
350
|
+
try:
|
|
351
|
+
if isinstance(value, str):
|
|
352
|
+
return value
|
|
353
|
+
return value.decode(encoding, errors=errors)
|
|
354
|
+
except (TypeError, ValueError):
|
|
355
|
+
return ""
|
|
356
|
+
|
|
357
|
+
@staticmethod
|
|
358
|
+
def str_to_hex(s: str, encoding: str = 'utf-8') -> str:
|
|
359
|
+
"""将字符串转换为十六进制
|
|
360
|
+
|
|
361
|
+
Args:
|
|
362
|
+
s: 字符串
|
|
363
|
+
encoding: 编码方式,默认为 'utf-8'
|
|
364
|
+
|
|
365
|
+
Returns:
|
|
366
|
+
str: 十六进制字符串
|
|
367
|
+
"""
|
|
368
|
+
if not s or not isinstance(s, str):
|
|
369
|
+
return ""
|
|
370
|
+
try:
|
|
371
|
+
return s.encode(encoding).hex()
|
|
372
|
+
except (TypeError, ValueError):
|
|
373
|
+
return ""
|
|
374
|
+
|
|
375
|
+
@staticmethod
|
|
376
|
+
def hex_to_str(hex_str: str, encoding: str = 'utf-8') -> str:
|
|
377
|
+
"""将十六进制字符串转换为普通字符串
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
hex_str: 十六进制字符串
|
|
381
|
+
encoding: 编码方式,默认为 'utf-8'
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
str: 转换后的字符串
|
|
385
|
+
"""
|
|
386
|
+
if not hex_str or not isinstance(hex_str, str):
|
|
387
|
+
return ""
|
|
388
|
+
try:
|
|
389
|
+
return bytes.fromhex(hex_str).decode(encoding)
|
|
390
|
+
except (TypeError, ValueError):
|
|
391
|
+
return ""
|