dc-cmnutils 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.
Potentially problematic release.
This version of dc-cmnutils might be problematic. Click here for more details.
- dc/__init__.py +0 -0
- dc/cmnutils/__init__.py +0 -0
- dc/cmnutils/basicdata/__init__.py +0 -0
- dc/cmnutils/basicdata/basic.py +0 -0
- dc/cmnutils/basicdata/basicdata.py +586 -0
- dc/cmnutils/basicdata/convert.py +189 -0
- dc/cmnutils/basicdata/convert_basic.py +391 -0
- dc/cmnutils/basicdata/json.py +94 -0
- dc/cmnutils/basicdata/object.py +814 -0
- dc/cmnutils/collection/__init__.py +0 -0
- dc/cmnutils/collection/collection.py +757 -0
- dc/cmnutils/database/__init__.py +0 -0
- dc/cmnutils/database/database_access.py +213 -0
- dc/cmnutils/database/database_accessor.py +287 -0
- dc/cmnutils/database/db_operater.py +283 -0
- dc/cmnutils/database/dbutil.py +389 -0
- dc/cmnutils/definitions/__init__.py +0 -0
- dc/cmnutils/definitions/definitions.py +91 -0
- dc/cmnutils/definitions/result.py +201 -0
- dc/cmnutils/io/__init__.py +0 -0
- dc/cmnutils/io/file/__init__.py +0 -0
- dc/cmnutils/io/file/cfg/__init__.py +0 -0
- dc/cmnutils/io/file/cfg/config.py +262 -0
- dc/cmnutils/io/file/file.py +214 -0
- dc/cmnutils/io/file/file_utils.py +605 -0
- dc/cmnutils/io/log/__init__.py +0 -0
- dc/cmnutils/io/log/log.py +43 -0
- dc/cmnutils/io/log/log_utils.py +50 -0
- dc/cmnutils/misc/__init__.py +0 -0
- dc/cmnutils/net/__init__.py +0 -0
- dc/cmnutils/net/email.py +185 -0
- dc/cmnutils/net/net.py +251 -0
- dc/cmnutils/net/net_utils.py +468 -0
- dc/cmnutils/sys/__init__.py +0 -0
- dc/cmnutils/sys/env/__init__.py +0 -0
- dc/cmnutils/sys/env/linuxenv.py +384 -0
- dc/cmnutils/sys/env/windowsenv.py +235 -0
- dc/cmnutils/sys/envutil.py +442 -0
- dc/cmnutils/sys/processutil.py +447 -0
- dc/cmnutils/sys/sys.py +640 -0
- dc/cmnutils/sys/sys_utils.py +580 -0
- dc/cmnutils/sys/threadutil.py +315 -0
- dc_cmnutils-0.0.1.dist-info/METADATA +36 -0
- dc_cmnutils-0.0.1.dist-info/RECORD +47 -0
- dc_cmnutils-0.0.1.dist-info/WHEEL +5 -0
- dc_cmnutils-0.0.1.dist-info/licenses/LICENSE.TXT +19 -0
- dc_cmnutils-0.0.1.dist-info/top_level.txt +1 -0
dc/__init__.py
ADDED
|
File without changes
|
dc/cmnutils/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
"""基础数据操作帮助类
|
|
2
|
+
|
|
3
|
+
提供字符串、数字、列表、字典等基础数据类型的常用操作封装。
|
|
4
|
+
所有方法均为静态方法,线程安全,适用于多线程环境。
|
|
5
|
+
"""
|
|
6
|
+
import re
|
|
7
|
+
import hashlib
|
|
8
|
+
import random
|
|
9
|
+
import string
|
|
10
|
+
from datetime import datetime
|
|
11
|
+
from typing import Any, List, Dict, Optional, Union
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BasicDataUtils:
|
|
15
|
+
"""基础数据操作帮助类"""
|
|
16
|
+
|
|
17
|
+
@staticmethod
|
|
18
|
+
def is_empty(value: Any) -> bool:
|
|
19
|
+
"""判断值是否为空
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
value: 任意类型的值
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
bool: 如果值为空返回 True,否则返回 False
|
|
26
|
+
"""
|
|
27
|
+
if value is None:
|
|
28
|
+
return True
|
|
29
|
+
if isinstance(value, str):
|
|
30
|
+
return value.strip() == ""
|
|
31
|
+
if isinstance(value, (list, tuple, dict, set)):
|
|
32
|
+
return len(value) == 0
|
|
33
|
+
return False
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def is_not_empty(value: Any) -> bool:
|
|
37
|
+
"""判断值是否不为空
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
value: 任意类型的值
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
bool: 如果值不为空返回 True,否则返回 False
|
|
44
|
+
"""
|
|
45
|
+
return not BasicDataUtils.is_empty(value)
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def safe_get(dictionary: Dict[str, Any], key: str, default: Any = None) -> Any:
|
|
49
|
+
"""安全获取字典值,避免 KeyError
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
dictionary: 字典对象
|
|
53
|
+
key: 键名
|
|
54
|
+
default: 默认值,当键不存在时返回
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Any: 字典中对应键的值,或默认值
|
|
58
|
+
"""
|
|
59
|
+
if dictionary is None or not isinstance(dictionary, dict):
|
|
60
|
+
return default
|
|
61
|
+
return dictionary.get(key, default)
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def safe_index(lst: List[Any], index: int, default: Any = None) -> Any:
|
|
65
|
+
"""安全获取列表元素,避免 IndexError
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
lst: 列表对象
|
|
69
|
+
index: 索引位置
|
|
70
|
+
default: 默认值,当索引超出范围时返回
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
Any: 列表中对应索引的元素,或默认值
|
|
74
|
+
"""
|
|
75
|
+
if lst is None or not isinstance(lst, list):
|
|
76
|
+
return default
|
|
77
|
+
try:
|
|
78
|
+
return lst[index]
|
|
79
|
+
except IndexError:
|
|
80
|
+
return default
|
|
81
|
+
|
|
82
|
+
@staticmethod
|
|
83
|
+
def remove_duplicates(lst: List[Any]) -> List[Any]:
|
|
84
|
+
"""移除列表中的重复元素,保持原有顺序
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
lst: 列表对象
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
List[Any]: 去重后的列表
|
|
91
|
+
"""
|
|
92
|
+
if lst is None:
|
|
93
|
+
return []
|
|
94
|
+
seen = set()
|
|
95
|
+
result = []
|
|
96
|
+
for item in lst:
|
|
97
|
+
if item not in seen:
|
|
98
|
+
seen.add(item)
|
|
99
|
+
result.append(item)
|
|
100
|
+
return result
|
|
101
|
+
|
|
102
|
+
@staticmethod
|
|
103
|
+
def merge_dicts(*dicts: Dict[str, Any]) -> Dict[str, Any]:
|
|
104
|
+
"""合并多个字典,后面的字典会覆盖前面的相同键
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
*dicts: 多个字典对象
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
Dict[str, Any]: 合并后的字典
|
|
111
|
+
"""
|
|
112
|
+
result = {}
|
|
113
|
+
for d in dicts:
|
|
114
|
+
if isinstance(d, dict):
|
|
115
|
+
result.update(d)
|
|
116
|
+
return result
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def split_string(s: str, separator: str = ',', strip: bool = True) -> List[str]:
|
|
120
|
+
"""分割字符串为列表
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
s: 要分割的字符串
|
|
124
|
+
separator: 分隔符,默认为逗号
|
|
125
|
+
strip: 是否去除每个元素的首尾空格,默认为 True
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
List[str]: 分割后的字符串列表
|
|
129
|
+
"""
|
|
130
|
+
if BasicDataUtils.is_empty(s):
|
|
131
|
+
return []
|
|
132
|
+
parts = s.split(separator)
|
|
133
|
+
if strip:
|
|
134
|
+
return [p.strip() for p in parts if p.strip()]
|
|
135
|
+
return parts
|
|
136
|
+
|
|
137
|
+
@staticmethod
|
|
138
|
+
def join_strings(lst: List[str], separator: str = ',') -> str:
|
|
139
|
+
"""将字符串列表连接为一个字符串
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
lst: 字符串列表
|
|
143
|
+
separator: 连接符,默认为逗号
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
str: 连接后的字符串
|
|
147
|
+
"""
|
|
148
|
+
if lst is None or not isinstance(lst, list):
|
|
149
|
+
return ""
|
|
150
|
+
return separator.join(str(item) for item in lst)
|
|
151
|
+
|
|
152
|
+
@staticmethod
|
|
153
|
+
def truncate_string(s: str, max_length: int, suffix: str = '...') -> str:
|
|
154
|
+
"""截断字符串,超过最大长度时添加后缀
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
s: 原始字符串
|
|
158
|
+
max_length: 最大长度
|
|
159
|
+
suffix: 后缀,默认为 '...'
|
|
160
|
+
|
|
161
|
+
Returns:
|
|
162
|
+
str: 截断后的字符串
|
|
163
|
+
"""
|
|
164
|
+
if BasicDataUtils.is_empty(s):
|
|
165
|
+
return ""
|
|
166
|
+
if len(s) <= max_length:
|
|
167
|
+
return s
|
|
168
|
+
return s[:max_length - len(suffix)] + suffix
|
|
169
|
+
|
|
170
|
+
@staticmethod
|
|
171
|
+
def generate_random_string(length: int = 16, chars: str = None) -> str:
|
|
172
|
+
"""生成随机字符串
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
length: 字符串长度,默认为 16
|
|
176
|
+
chars: 可选的字符集,默认为字母和数字
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
str: 随机生成的字符串
|
|
180
|
+
"""
|
|
181
|
+
if chars is None:
|
|
182
|
+
chars = string.ascii_letters + string.digits
|
|
183
|
+
return ''.join(random.choice(chars) for _ in range(length))
|
|
184
|
+
|
|
185
|
+
@staticmethod
|
|
186
|
+
def generate_random_number(min_value: int = 0, max_value: int = 100) -> int:
|
|
187
|
+
"""生成随机整数
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
min_value: 最小值,默认为 0
|
|
191
|
+
max_value: 最大值,默认为 100
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
int: 随机整数
|
|
195
|
+
"""
|
|
196
|
+
return random.randint(min_value, max_value)
|
|
197
|
+
|
|
198
|
+
@staticmethod
|
|
199
|
+
def calculate_hash(data: Any, algorithm: str = 'md5') -> str:
|
|
200
|
+
"""计算数据的哈希值
|
|
201
|
+
|
|
202
|
+
Args:
|
|
203
|
+
data: 要计算哈希的数据
|
|
204
|
+
algorithm: 哈希算法,支持 'md5', 'sha1', 'sha256', 'sha512'
|
|
205
|
+
|
|
206
|
+
Returns:
|
|
207
|
+
str: 十六进制哈希值
|
|
208
|
+
"""
|
|
209
|
+
if data is None:
|
|
210
|
+
return ""
|
|
211
|
+
|
|
212
|
+
if isinstance(data, str):
|
|
213
|
+
data = data.encode('utf-8')
|
|
214
|
+
elif isinstance(data, (int, float)):
|
|
215
|
+
data = str(data).encode('utf-8')
|
|
216
|
+
elif not isinstance(data, bytes):
|
|
217
|
+
data = str(data).encode('utf-8')
|
|
218
|
+
|
|
219
|
+
hash_obj = hashlib.new(algorithm)
|
|
220
|
+
hash_obj.update(data)
|
|
221
|
+
return hash_obj.hexdigest()
|
|
222
|
+
|
|
223
|
+
@staticmethod
|
|
224
|
+
def format_datetime(dt: datetime = None, format_str: str = '%Y-%m-%d %H:%M:%S') -> str:
|
|
225
|
+
"""格式化日期时间
|
|
226
|
+
|
|
227
|
+
Args:
|
|
228
|
+
dt: 日期时间对象,默认为当前时间
|
|
229
|
+
format_str: 格式化字符串,默认为 '%Y-%m-%d %H:%M:%S'
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
str: 格式化后的日期时间字符串
|
|
233
|
+
"""
|
|
234
|
+
if dt is None:
|
|
235
|
+
dt = datetime.now()
|
|
236
|
+
return dt.strftime(format_str)
|
|
237
|
+
|
|
238
|
+
@staticmethod
|
|
239
|
+
def is_valid_email(email: str) -> bool:
|
|
240
|
+
"""验证邮箱格式是否有效
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
email: 邮箱地址
|
|
244
|
+
|
|
245
|
+
Returns:
|
|
246
|
+
bool: 如果格式有效返回 True,否则返回 False
|
|
247
|
+
"""
|
|
248
|
+
if BasicDataUtils.is_empty(email):
|
|
249
|
+
return False
|
|
250
|
+
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
|
251
|
+
return re.match(pattern, email) is not None
|
|
252
|
+
|
|
253
|
+
@staticmethod
|
|
254
|
+
def is_valid_phone(phone: str) -> bool:
|
|
255
|
+
"""验证手机号格式是否有效(中国手机号)
|
|
256
|
+
|
|
257
|
+
Args:
|
|
258
|
+
phone: 手机号码
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
bool: 如果格式有效返回 True,否则返回 False
|
|
262
|
+
"""
|
|
263
|
+
if BasicDataUtils.is_empty(phone):
|
|
264
|
+
return False
|
|
265
|
+
pattern = r'^1[3-9]\d{9}$'
|
|
266
|
+
return re.match(pattern, phone) is not None
|
|
267
|
+
|
|
268
|
+
@staticmethod
|
|
269
|
+
def is_valid_url(url: str) -> bool:
|
|
270
|
+
"""验证 URL 格式是否有效
|
|
271
|
+
|
|
272
|
+
Args:
|
|
273
|
+
url: 网址
|
|
274
|
+
|
|
275
|
+
Returns:
|
|
276
|
+
bool: 如果格式有效返回 True,否则返回 False
|
|
277
|
+
"""
|
|
278
|
+
if BasicDataUtils.is_empty(url):
|
|
279
|
+
return False
|
|
280
|
+
pattern = r'^https?://[\w\.-]+(?:/[\w\.-]*)*$'
|
|
281
|
+
return re.match(pattern, url) is not None
|
|
282
|
+
|
|
283
|
+
@staticmethod
|
|
284
|
+
def clamp(value: Union[int, float], min_value: Union[int, float], max_value: Union[int, float]) -> Union[int, float]:
|
|
285
|
+
"""将值限制在指定范围内
|
|
286
|
+
|
|
287
|
+
Args:
|
|
288
|
+
value: 要限制的值
|
|
289
|
+
min_value: 最小值
|
|
290
|
+
max_value: 最大值
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
Union[int, float]: 限制后的值
|
|
294
|
+
"""
|
|
295
|
+
return max(min_value, min(value, max_value))
|
|
296
|
+
|
|
297
|
+
@staticmethod
|
|
298
|
+
def to_bool(value: Any) -> bool:
|
|
299
|
+
"""将值转换为布尔值
|
|
300
|
+
|
|
301
|
+
Args:
|
|
302
|
+
value: 任意类型的值
|
|
303
|
+
|
|
304
|
+
Returns:
|
|
305
|
+
bool: 转换后的布尔值
|
|
306
|
+
"""
|
|
307
|
+
if isinstance(value, bool):
|
|
308
|
+
return value
|
|
309
|
+
if isinstance(value, str):
|
|
310
|
+
lower = value.lower().strip()
|
|
311
|
+
return lower in ('true', 'yes', '1', 'on')
|
|
312
|
+
if isinstance(value, int):
|
|
313
|
+
return value != 0
|
|
314
|
+
if isinstance(value, float):
|
|
315
|
+
return value != 0.0
|
|
316
|
+
return bool(value)
|
|
317
|
+
|
|
318
|
+
@staticmethod
|
|
319
|
+
def trim(s: str) -> str:
|
|
320
|
+
"""去除字符串首尾空格
|
|
321
|
+
|
|
322
|
+
Args:
|
|
323
|
+
s: 字符串
|
|
324
|
+
|
|
325
|
+
Returns:
|
|
326
|
+
str: 去除首尾空格后的字符串
|
|
327
|
+
"""
|
|
328
|
+
if s is None:
|
|
329
|
+
return ""
|
|
330
|
+
return s.strip()
|
|
331
|
+
|
|
332
|
+
@staticmethod
|
|
333
|
+
def to_upper(s: str) -> str:
|
|
334
|
+
"""转换为大写
|
|
335
|
+
|
|
336
|
+
Args:
|
|
337
|
+
s: 字符串
|
|
338
|
+
|
|
339
|
+
Returns:
|
|
340
|
+
str: 大写字符串
|
|
341
|
+
"""
|
|
342
|
+
if s is None:
|
|
343
|
+
return ""
|
|
344
|
+
return s.upper()
|
|
345
|
+
|
|
346
|
+
@staticmethod
|
|
347
|
+
def to_lower(s: str) -> str:
|
|
348
|
+
"""转换为小写
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
s: 字符串
|
|
352
|
+
|
|
353
|
+
Returns:
|
|
354
|
+
str: 小写字符串
|
|
355
|
+
"""
|
|
356
|
+
if s is None:
|
|
357
|
+
return ""
|
|
358
|
+
return s.lower()
|
|
359
|
+
|
|
360
|
+
@staticmethod
|
|
361
|
+
def starts_with(s: str, prefix: str) -> bool:
|
|
362
|
+
"""判断字符串是否以指定前缀开头
|
|
363
|
+
|
|
364
|
+
Args:
|
|
365
|
+
s: 字符串
|
|
366
|
+
prefix: 前缀
|
|
367
|
+
|
|
368
|
+
Returns:
|
|
369
|
+
bool: 是否以指定前缀开头
|
|
370
|
+
"""
|
|
371
|
+
if s is None or prefix is None:
|
|
372
|
+
return False
|
|
373
|
+
return s.startswith(prefix)
|
|
374
|
+
|
|
375
|
+
@staticmethod
|
|
376
|
+
def ends_with(s: str, suffix: str) -> bool:
|
|
377
|
+
"""判断字符串是否以指定后缀结尾
|
|
378
|
+
|
|
379
|
+
Args:
|
|
380
|
+
s: 字符串
|
|
381
|
+
suffix: 后缀
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
bool: 是否以指定后缀结尾
|
|
385
|
+
"""
|
|
386
|
+
if s is None or suffix is None:
|
|
387
|
+
return False
|
|
388
|
+
return s.endswith(suffix)
|
|
389
|
+
|
|
390
|
+
@staticmethod
|
|
391
|
+
def contains(s: str, substring: str) -> bool:
|
|
392
|
+
"""判断字符串是否包含指定子串
|
|
393
|
+
|
|
394
|
+
Args:
|
|
395
|
+
s: 字符串
|
|
396
|
+
substring: 子串
|
|
397
|
+
|
|
398
|
+
Returns:
|
|
399
|
+
bool: 是否包含指定子串
|
|
400
|
+
"""
|
|
401
|
+
if s is None or substring is None:
|
|
402
|
+
return False
|
|
403
|
+
return substring in s
|
|
404
|
+
|
|
405
|
+
@staticmethod
|
|
406
|
+
def substring(s: str, start: int, length: int = None) -> str:
|
|
407
|
+
"""截取子串
|
|
408
|
+
|
|
409
|
+
Args:
|
|
410
|
+
s: 字符串
|
|
411
|
+
start: 起始位置
|
|
412
|
+
length: 截取长度,默认为到字符串末尾
|
|
413
|
+
|
|
414
|
+
Returns:
|
|
415
|
+
str: 截取的子串
|
|
416
|
+
"""
|
|
417
|
+
if s is None:
|
|
418
|
+
return ""
|
|
419
|
+
if length is None:
|
|
420
|
+
return s[start:]
|
|
421
|
+
return s[start:start + length]
|
|
422
|
+
|
|
423
|
+
@staticmethod
|
|
424
|
+
def is_integer(value: Any) -> bool:
|
|
425
|
+
"""判断是否为整数
|
|
426
|
+
|
|
427
|
+
Args:
|
|
428
|
+
value: 任意值
|
|
429
|
+
|
|
430
|
+
Returns:
|
|
431
|
+
bool: 是否为整数
|
|
432
|
+
"""
|
|
433
|
+
return isinstance(value, int)
|
|
434
|
+
|
|
435
|
+
@staticmethod
|
|
436
|
+
def is_float(value: Any) -> bool:
|
|
437
|
+
"""判断是否为浮点数
|
|
438
|
+
|
|
439
|
+
Args:
|
|
440
|
+
value: 任意值
|
|
441
|
+
|
|
442
|
+
Returns:
|
|
443
|
+
bool: 是否为浮点数
|
|
444
|
+
"""
|
|
445
|
+
return isinstance(value, float)
|
|
446
|
+
|
|
447
|
+
@staticmethod
|
|
448
|
+
def in_range(value: Union[int, float], min_value: Union[int, float], max_value: Union[int, float]) -> bool:
|
|
449
|
+
"""判断值是否在指定范围内
|
|
450
|
+
|
|
451
|
+
Args:
|
|
452
|
+
value: 要判断的值
|
|
453
|
+
min_value: 最小值
|
|
454
|
+
max_value: 最大值
|
|
455
|
+
|
|
456
|
+
Returns:
|
|
457
|
+
bool: 是否在范围内
|
|
458
|
+
"""
|
|
459
|
+
return min_value <= value <= max_value
|
|
460
|
+
|
|
461
|
+
@staticmethod
|
|
462
|
+
def is_list(value: Any) -> bool:
|
|
463
|
+
"""判断是否为列表
|
|
464
|
+
|
|
465
|
+
Args:
|
|
466
|
+
value: 任意值
|
|
467
|
+
|
|
468
|
+
Returns:
|
|
469
|
+
bool: 是否为列表
|
|
470
|
+
"""
|
|
471
|
+
return isinstance(value, list)
|
|
472
|
+
|
|
473
|
+
@staticmethod
|
|
474
|
+
def get_first(lst: List[Any], default: Any = None) -> Any:
|
|
475
|
+
"""获取列表第一个元素
|
|
476
|
+
|
|
477
|
+
Args:
|
|
478
|
+
lst: 列表
|
|
479
|
+
default: 默认值
|
|
480
|
+
|
|
481
|
+
Returns:
|
|
482
|
+
Any: 第一个元素或默认值
|
|
483
|
+
"""
|
|
484
|
+
if lst is None or not isinstance(lst, list) or len(lst) == 0:
|
|
485
|
+
return default
|
|
486
|
+
return lst[0]
|
|
487
|
+
|
|
488
|
+
@staticmethod
|
|
489
|
+
def get_last(lst: List[Any], default: Any = None) -> Any:
|
|
490
|
+
"""获取列表最后一个元素
|
|
491
|
+
|
|
492
|
+
Args:
|
|
493
|
+
lst: 列表
|
|
494
|
+
default: 默认值
|
|
495
|
+
|
|
496
|
+
Returns:
|
|
497
|
+
Any: 最后一个元素或默认值
|
|
498
|
+
"""
|
|
499
|
+
if lst is None or not isinstance(lst, list) or len(lst) == 0:
|
|
500
|
+
return default
|
|
501
|
+
return lst[-1]
|
|
502
|
+
|
|
503
|
+
@staticmethod
|
|
504
|
+
def chunk(lst: List[Any], size: int) -> List[List[Any]]:
|
|
505
|
+
"""将列表分割为指定大小的块
|
|
506
|
+
|
|
507
|
+
Args:
|
|
508
|
+
lst: 列表
|
|
509
|
+
size: 块大小
|
|
510
|
+
|
|
511
|
+
Returns:
|
|
512
|
+
List[List[Any]]: 分割后的列表
|
|
513
|
+
"""
|
|
514
|
+
if lst is None or not isinstance(lst, list) or size <= 0:
|
|
515
|
+
return []
|
|
516
|
+
return [lst[i:i + size] for i in range(0, len(lst), size)]
|
|
517
|
+
|
|
518
|
+
@staticmethod
|
|
519
|
+
def is_dict(value: Any) -> bool:
|
|
520
|
+
"""判断是否为字典
|
|
521
|
+
|
|
522
|
+
Args:
|
|
523
|
+
value: 任意值
|
|
524
|
+
|
|
525
|
+
Returns:
|
|
526
|
+
bool: 是否为字典
|
|
527
|
+
"""
|
|
528
|
+
return isinstance(value, dict)
|
|
529
|
+
|
|
530
|
+
@staticmethod
|
|
531
|
+
def get_value(dictionary: Dict[str, Any], key: str, default: Any = None) -> Any:
|
|
532
|
+
"""获取字典值
|
|
533
|
+
|
|
534
|
+
Args:
|
|
535
|
+
dictionary: 字典
|
|
536
|
+
key: 键
|
|
537
|
+
default: 默认值
|
|
538
|
+
|
|
539
|
+
Returns:
|
|
540
|
+
Any: 对应的值或默认值
|
|
541
|
+
"""
|
|
542
|
+
if dictionary is None or not isinstance(dictionary, dict):
|
|
543
|
+
return default
|
|
544
|
+
return dictionary.get(key, default)
|
|
545
|
+
|
|
546
|
+
@staticmethod
|
|
547
|
+
def has_key(dictionary: Dict[str, Any], key: str) -> bool:
|
|
548
|
+
"""判断字典是否包含指定键
|
|
549
|
+
|
|
550
|
+
Args:
|
|
551
|
+
dictionary: 字典
|
|
552
|
+
key: 键
|
|
553
|
+
|
|
554
|
+
Returns:
|
|
555
|
+
bool: 是否包含指定键
|
|
556
|
+
"""
|
|
557
|
+
if dictionary is None or not isinstance(dictionary, dict) or key is None:
|
|
558
|
+
return False
|
|
559
|
+
return key in dictionary
|
|
560
|
+
|
|
561
|
+
@staticmethod
|
|
562
|
+
def is_collection(value: Any) -> bool:
|
|
563
|
+
"""判断是否为集合类型(列表、元组、集合),不包括字符串和字典
|
|
564
|
+
|
|
565
|
+
Args:
|
|
566
|
+
value: 任意值
|
|
567
|
+
|
|
568
|
+
Returns:
|
|
569
|
+
bool: 是否为集合类型
|
|
570
|
+
"""
|
|
571
|
+
return isinstance(value, (list, tuple, set))
|
|
572
|
+
|
|
573
|
+
@staticmethod
|
|
574
|
+
def count(lst: List[Any], value: Any) -> int:
|
|
575
|
+
"""统计列表中指定元素出现的次数
|
|
576
|
+
|
|
577
|
+
Args:
|
|
578
|
+
lst: 列表
|
|
579
|
+
value: 要统计的元素
|
|
580
|
+
|
|
581
|
+
Returns:
|
|
582
|
+
int: 出现次数
|
|
583
|
+
"""
|
|
584
|
+
if lst is None or not isinstance(lst, list):
|
|
585
|
+
return 0
|
|
586
|
+
return lst.count(value)
|