ezKit 3.0.63__tar.gz → 3.0.65__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.63
3
+ Version: 3.0.65
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -10,6 +10,7 @@ from decimal import Decimal
10
10
  from functools import wraps
11
11
  from ipaddress import IPv4Address, IPv6Address
12
12
  from itertools import islice
13
+ from operator import itemgetter
13
14
  from os import environ
14
15
  from pathlib import Path
15
16
  from secrets import choice
@@ -174,6 +175,10 @@ def check_path_type(path_object: str | Path, path_type: str) -> bool:
174
175
 
175
176
  _path_object: Path = Path(path_object) if not isinstance(path_object, Path) else path_object
176
177
 
178
+ # is_symlink 不跟随目标, 断开的符号链接也应被识别
179
+ # if path_type == "symlink":
180
+ # return _path_object.is_symlink()
181
+
177
182
  # 文件不存在的情况
178
183
  if not _path_object.exists():
179
184
  return False
@@ -400,6 +405,10 @@ def list_split(data: list, number: int, equally: bool = False) -> list | None:
400
405
 
401
406
  try:
402
407
 
408
+ # 检查 number 是否为正整数
409
+ if not isinstance(number, int) or isinstance(number, bool) or number < 1:
410
+ raise ValueError("number must be a positive integer")
411
+
403
412
  # 要将列表平均分成 n 个子列表
404
413
  if equally:
405
414
  it = iter(data)
@@ -425,6 +434,9 @@ def list_print_by_step(data: list, step: int, separator: str = " ") -> bool:
425
434
 
426
435
  try:
427
436
 
437
+ if not isinstance(step, int) or isinstance(step, bool) or step < 1:
438
+ raise ValueError("step must be a positive integer")
439
+
428
440
  # 打印
429
441
  for i, v in enumerate(data):
430
442
 
@@ -529,7 +541,7 @@ def parallelism(
529
541
  workers: int = 2,
530
542
  thread: bool = False,
531
543
  **kwargs,
532
- ) -> Any:
544
+ ) -> list | bool:
533
545
  """multi-processing or multi-threading"""
534
546
 
535
547
  # :param func: 处理函数
@@ -557,6 +569,14 @@ def parallelism(
557
569
 
558
570
  try:
559
571
 
572
+ # 确保数据不为空
573
+ if not data:
574
+ raise ValueError("data is empty")
575
+
576
+ # 检查 workers 是否为正整数
577
+ if not isinstance(workers, int) or isinstance(workers, bool) or workers < 1:
578
+ raise ValueError("workers must be a positive integer")
579
+
560
580
  # 确保并行数不超过数据量
561
581
  workers = min(len(data), workers)
562
582
  data_chunks = list_split(data, workers, equally=True) if workers > 1 else [data]
@@ -565,10 +585,10 @@ def parallelism(
565
585
  logger.info(f"{info} | data chunks error")
566
586
  return False
567
587
 
568
- # 执行多线程或多进程任务
588
+ # 在上下文管理器内获取结果, 确保任务异常由此处捕获
569
589
  pool = ThreadPoolExecutor if thread else ProcessPoolExecutor
570
590
  with pool(workers, **kwargs) as executor:
571
- return executor.map(func, data_chunks)
591
+ return list(executor.map(func, data_chunks))
572
592
 
573
593
  except Exception as e:
574
594
 
@@ -624,11 +644,15 @@ def shell(
624
644
 
625
645
  try:
626
646
 
627
- if not serialize:
647
+ options = dict(kwargs)
628
648
 
629
- return subprocess.run(args, **kwargs, check=False)
649
+ if not serialize:
650
+ return subprocess.run(args, check=options.pop("check", False), **options)
630
651
 
631
- result = subprocess.run(args, capture_output=True, text=True, check=False, **kwargs)
652
+ # 序列化模式强制使用文本输出, 同时避免重复关键字参数
653
+ options.pop("check", None)
654
+ options.update(capture_output=True, text=True)
655
+ result = subprocess.run(args, check=False, **options)
632
656
 
633
657
  return {
634
658
  "returncode": result.returncode,
@@ -702,3 +726,15 @@ def split_dict(data: dict, keys: list) -> tuple[dict[Any, Any], dict[Any, Any]]
702
726
  logger.error(e)
703
727
 
704
728
  return None
729
+
730
+
731
+ def sort_dicts(dicts: list[dict], key: str, *, reverse: bool = False) -> list[dict] | None:
732
+ """字典列表排序"""
733
+ try:
734
+ return sorted(dicts, key=itemgetter(key), reverse=reverse)
735
+ except Exception as e:
736
+ if DEBUG:
737
+ logger.exception(e)
738
+ else:
739
+ logger.error(e)
740
+ return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.63
3
+ Version: 3.0.65
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -4,7 +4,7 @@ from setuptools import find_packages, setup
4
4
 
5
5
  setup(
6
6
  name="ezKit",
7
- version="3.0.63",
7
+ version="3.0.65",
8
8
  author="septvean",
9
9
  author_email="septvean@gmail.com",
10
10
  description="Easy Kit",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes