ezKit 3.0.2__tar.gz → 3.0.4__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.2
3
+ Version: 3.0.4
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -3,6 +3,9 @@
3
3
  Command:
4
4
 
5
5
  ```sh
6
+ # Clear cache
7
+ pip cache purge
8
+
6
9
  # Install or Update
7
10
  pip install -i https://pypi.org/simple --trusted-host pypi.org -U ezKit
8
11
  ```
@@ -1,8 +1,11 @@
1
1
  """Utils Library"""
2
2
 
3
+ import asyncio
4
+ import time
5
+ from functools import wraps
3
6
  from os import environ
4
7
  from pathlib import Path
5
- from typing import Any
8
+ from typing import Any, Callable, Coroutine, ParamSpec, TypeVar
6
9
 
7
10
  from loguru import logger
8
11
 
@@ -59,6 +62,93 @@ def isTrue(target: object, typeClass: Any) -> bool:
59
62
  # --------------------------------------------------------------------------------------------------
60
63
 
61
64
 
65
+ P = ParamSpec("P")
66
+ R = TypeVar("R")
67
+
68
+
69
+ def retry_decorator(times: int = 3, delay: float = 1.0):
70
+ """重试装饰器
71
+
72
+ Args:
73
+ times: 最大重试次数
74
+ delay: 每次重试之间的等待时间(秒)
75
+ """
76
+
77
+ def decorator(func: Callable[P, R]) -> Callable[P, R | bool]:
78
+
79
+ @wraps(func)
80
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R | bool:
81
+
82
+ max_times = max(1, times)
83
+
84
+ for attempt in range(max_times):
85
+
86
+ try:
87
+
88
+ return func(*args, **kwargs)
89
+
90
+ except Exception as e:
91
+
92
+ if DEBUG:
93
+ logger.exception(e)
94
+ else:
95
+ logger.error(e)
96
+
97
+ if attempt < (max_times - 1):
98
+ time.sleep(delay)
99
+ else:
100
+ return False
101
+
102
+ return False
103
+
104
+ return wrapper
105
+
106
+ return decorator
107
+
108
+
109
+ def async_retry_decorator(times: int = 3, delay: float = 1.0):
110
+ """异步重试装饰器
111
+
112
+ Args:
113
+ times: 最大重试次数
114
+ delay: 每次重试之间的等待时间(秒)
115
+ """
116
+
117
+ def decorator(func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, Coroutine[Any, Any, R | bool]]:
118
+
119
+ @wraps(func)
120
+ async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R | bool:
121
+
122
+ max_times = max(1, times)
123
+
124
+ for attempt in range(max_times):
125
+
126
+ try:
127
+
128
+ return await func(*args, **kwargs)
129
+
130
+ except Exception as e:
131
+
132
+ if DEBUG:
133
+ logger.exception(e)
134
+ else:
135
+ logger.error(e)
136
+
137
+ if attempt < (max_times - 1):
138
+ await asyncio.sleep(delay)
139
+ else:
140
+ return False
141
+
142
+ return False
143
+
144
+ return wrapper
145
+
146
+ return decorator
147
+
148
+
149
+ # --------------------------------------------------------------------------------------------------
150
+
151
+
62
152
  def check_path_type(path_object: str | Path, path_type: str) -> bool:
63
153
  """检查路径类型"""
64
154
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.2
3
+ Version: 3.0.4
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.2",
7
+ version="3.0.4",
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