funcguard 0.1.8__tar.gz → 0.2.0__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.
Potentially problematic release.
This version of funcguard might be problematic. Click here for more details.
- {funcguard-0.1.8 → funcguard-0.2.0}/PKG-INFO +7 -7
- {funcguard-0.1.8 → funcguard-0.2.0}/README.md +6 -6
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard/__init__.py +4 -3
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard/printer.py +27 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard/time_utils.py +22 -10
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard/tools.py +0 -2
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard.egg-info/PKG-INFO +7 -7
- {funcguard-0.1.8 → funcguard-0.2.0}/setup.py +1 -1
- {funcguard-0.1.8 → funcguard-0.2.0}/LICENSE +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard/core.py +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard.egg-info/SOURCES.txt +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard.egg-info/dependency_links.txt +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard.egg-info/not-zip-safe +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard.egg-info/requires.txt +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/funcguard.egg-info/top_level.txt +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/setup.cfg +0 -0
- {funcguard-0.1.8 → funcguard-0.2.0}/tests/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: funcguard
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。
|
|
5
5
|
Home-page: https://github.com/tinycen/funcguard
|
|
6
6
|
Author: tinycen
|
|
@@ -181,10 +181,10 @@ for i in range(1, 101):
|
|
|
181
181
|
|
|
182
182
|
### 执行时间监控
|
|
183
183
|
|
|
184
|
-
使用`
|
|
184
|
+
使用`time_monitor`函数监控函数执行时间:
|
|
185
185
|
|
|
186
186
|
```python
|
|
187
|
-
from funcguard import
|
|
187
|
+
from funcguard import time_monitor
|
|
188
188
|
|
|
189
189
|
def some_function():
|
|
190
190
|
# 模拟一个耗时操作
|
|
@@ -193,14 +193,14 @@ def some_function():
|
|
|
193
193
|
return "操作完成"
|
|
194
194
|
|
|
195
195
|
# 模式1:总是打印执行时间
|
|
196
|
-
result =
|
|
196
|
+
result = time_monitor(
|
|
197
197
|
func=some_function,
|
|
198
198
|
print_mode=1
|
|
199
199
|
)
|
|
200
200
|
print(f"结果: {result}")
|
|
201
201
|
|
|
202
202
|
# 模式2:仅在超过阈值时打印警告
|
|
203
|
-
result =
|
|
203
|
+
result = time_monitor(
|
|
204
204
|
func=some_function,
|
|
205
205
|
warning_threshold=1.5, # 设置1.5秒的警告阈值
|
|
206
206
|
print_mode=2
|
|
@@ -208,7 +208,7 @@ result = monitor_execution_time(
|
|
|
208
208
|
print(f"结果: {result}")
|
|
209
209
|
|
|
210
210
|
# 模式0:不打印任何信息,仅返回结果和执行时间
|
|
211
|
-
result, duration =
|
|
211
|
+
result, duration = time_monitor(
|
|
212
212
|
func=some_function,
|
|
213
213
|
print_mode=0
|
|
214
214
|
)
|
|
@@ -293,7 +293,7 @@ print(f"结果: {result}, 耗时: {duration}秒")
|
|
|
293
293
|
- 否则返回None
|
|
294
294
|
- **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
|
|
295
295
|
|
|
296
|
-
####
|
|
296
|
+
#### time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)
|
|
297
297
|
|
|
298
298
|
- **参数**:
|
|
299
299
|
- `warning_threshold`: 警告阈值(秒),如果执行耗时超过此值则打印警告,默认为None
|
|
@@ -167,10 +167,10 @@ for i in range(1, 101):
|
|
|
167
167
|
|
|
168
168
|
### 执行时间监控
|
|
169
169
|
|
|
170
|
-
使用`
|
|
170
|
+
使用`time_monitor`函数监控函数执行时间:
|
|
171
171
|
|
|
172
172
|
```python
|
|
173
|
-
from funcguard import
|
|
173
|
+
from funcguard import time_monitor
|
|
174
174
|
|
|
175
175
|
def some_function():
|
|
176
176
|
# 模拟一个耗时操作
|
|
@@ -179,14 +179,14 @@ def some_function():
|
|
|
179
179
|
return "操作完成"
|
|
180
180
|
|
|
181
181
|
# 模式1:总是打印执行时间
|
|
182
|
-
result =
|
|
182
|
+
result = time_monitor(
|
|
183
183
|
func=some_function,
|
|
184
184
|
print_mode=1
|
|
185
185
|
)
|
|
186
186
|
print(f"结果: {result}")
|
|
187
187
|
|
|
188
188
|
# 模式2:仅在超过阈值时打印警告
|
|
189
|
-
result =
|
|
189
|
+
result = time_monitor(
|
|
190
190
|
func=some_function,
|
|
191
191
|
warning_threshold=1.5, # 设置1.5秒的警告阈值
|
|
192
192
|
print_mode=2
|
|
@@ -194,7 +194,7 @@ result = monitor_execution_time(
|
|
|
194
194
|
print(f"结果: {result}")
|
|
195
195
|
|
|
196
196
|
# 模式0:不打印任何信息,仅返回结果和执行时间
|
|
197
|
-
result, duration =
|
|
197
|
+
result, duration = time_monitor(
|
|
198
198
|
func=some_function,
|
|
199
199
|
print_mode=0
|
|
200
200
|
)
|
|
@@ -279,7 +279,7 @@ print(f"结果: {result}, 耗时: {duration}秒")
|
|
|
279
279
|
- 否则返回None
|
|
280
280
|
- **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
|
|
281
281
|
|
|
282
|
-
####
|
|
282
|
+
#### time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)
|
|
283
283
|
|
|
284
284
|
- **参数**:
|
|
285
285
|
- `warning_threshold`: 警告阈值(秒),如果执行耗时超过此值则打印警告,默认为None
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from .core import timeout_handler, retry_function
|
|
2
2
|
from .tools import send_request
|
|
3
|
-
from .time_utils import time_log, time_diff,
|
|
4
|
-
from .printer import print_block, print_line, print_title
|
|
3
|
+
from .time_utils import time_log, time_diff, time_monitor
|
|
4
|
+
from .printer import print_block, print_line, print_title, print_progress
|
|
5
5
|
|
|
6
6
|
__author__ = "ruocen"
|
|
7
7
|
|
|
@@ -12,8 +12,9 @@ __all__ = [
|
|
|
12
12
|
"send_request",
|
|
13
13
|
"time_log",
|
|
14
14
|
"time_diff",
|
|
15
|
-
"
|
|
15
|
+
"time_monitor",
|
|
16
16
|
"print_block",
|
|
17
17
|
"print_line",
|
|
18
18
|
"print_title",
|
|
19
|
+
"print_progress",
|
|
19
20
|
]
|
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
from typing import Any
|
|
2
2
|
|
|
3
|
+
# 打印进度条
|
|
4
|
+
def print_progress(idx: int, total: int, message: str = "") -> None:
|
|
5
|
+
"""
|
|
6
|
+
打印进度条,显示当前进度
|
|
7
|
+
|
|
8
|
+
设计原理:
|
|
9
|
+
- 进度条总长度固定为50个字符,这是终端显示的最佳长度
|
|
10
|
+
- 使用整除2(//2)将0-100%映射到0-50字符,确保平滑过渡
|
|
11
|
+
- 已完成部分用'█'表示,未完成部分用'-'表示
|
|
12
|
+
|
|
13
|
+
:param idx: 当前索引(从0开始)
|
|
14
|
+
:param total: 总数量
|
|
15
|
+
:param message: 额外消息,默认为空字符串
|
|
16
|
+
"""
|
|
17
|
+
# 计算当前进度百分比(0-100)
|
|
18
|
+
percent = int(idx / total * 100)
|
|
19
|
+
|
|
20
|
+
# 构建进度条:
|
|
21
|
+
bar = '█' * (percent // 2) + '-' * (50 - percent // 2)
|
|
22
|
+
|
|
23
|
+
# 打印进度条:
|
|
24
|
+
# - \r:回车符,回到行首覆盖之前的内容
|
|
25
|
+
# - end='':不换行,保持在同一行更新
|
|
26
|
+
# - flush=True:立即刷新输出,确保实时显示
|
|
27
|
+
print(f"\r进度: |{bar}| {percent}% ({idx}/{total}) {message}", end='', flush=True)
|
|
28
|
+
|
|
29
|
+
|
|
3
30
|
# 打印带等号的标题(如:=== 初始化分类器 ===)
|
|
4
31
|
def print_title(title: str, separator_char: str = "=", padding_length: int = 3) -> None:
|
|
5
32
|
"""
|
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
时间工具模块,提供时间计算、日志记录和执行时间监控功能
|
|
3
3
|
"""
|
|
4
4
|
from datetime import datetime, timezone, timedelta
|
|
5
|
-
from typing import Optional, Union
|
|
6
5
|
|
|
7
6
|
|
|
8
7
|
# 打印时间
|
|
9
|
-
def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
|
|
8
|
+
def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 , return_field = "progress_info") :
|
|
10
9
|
"""
|
|
11
10
|
打印带时间戳的日志信息,支持进度显示和预计完成时间
|
|
12
11
|
|
|
@@ -15,17 +14,20 @@ def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
|
|
|
15
14
|
:param max_num: 总进度数量
|
|
16
15
|
:param s_time: 开始时间,用于计算预计完成时间
|
|
17
16
|
:param start_from: i是否从0开始,0表示从0开始,1表示从1开始
|
|
18
|
-
:
|
|
17
|
+
:param return_field: 返回字段,支持以下:
|
|
18
|
+
"progress_info" 表示完整进度信息,"remaining_time" 表示剩余时间,"end_time" 表示预计完成时间
|
|
19
|
+
:return: 根据 return_field 参数返回不同的信息
|
|
19
20
|
"""
|
|
20
21
|
now = datetime.now( timezone( timedelta( hours = 8 ) ) )
|
|
21
|
-
|
|
22
|
+
time_str = "{:02d}:{:02d}:{:02d}".format( now.hour, now.minute, now.second )
|
|
23
|
+
progress_info = ""
|
|
22
24
|
if i < 2 or max_num < 2 :
|
|
23
|
-
print(
|
|
25
|
+
print( time_str + " " + message )
|
|
24
26
|
|
|
25
27
|
else :
|
|
26
28
|
# 根据start_from参数计算实际处理的项目数
|
|
27
29
|
process_item = i + 1 if start_from == 0 else i
|
|
28
|
-
|
|
30
|
+
progress_info = "{}/{}".format( process_item, max_num )
|
|
29
31
|
# 检查是否应该显示预计完成时间和剩余时间
|
|
30
32
|
if process_item % 10 == 0 and s_time is not None and process_item < max_num :
|
|
31
33
|
duration = now - s_time
|
|
@@ -35,9 +37,19 @@ def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
|
|
|
35
37
|
end_time = now + time_left
|
|
36
38
|
end_time_str = end_time.strftime( "%Y-%m-%d %H:%M" )
|
|
37
39
|
remaining_time_str = str( timedelta( seconds = int( time_left.total_seconds() ) ) )
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
|
|
41
|
+
# Estimated Time of Arrival(预计完成/到达时间)
|
|
42
|
+
if return_field == "end_time" :
|
|
43
|
+
return f"eta {end_time_str}"
|
|
44
|
+
|
|
45
|
+
# Estimated Time Remaining(预计剩余时间)
|
|
46
|
+
elif return_field == "remaining_time" :
|
|
47
|
+
return f"etr {remaining_time_str}"
|
|
48
|
+
|
|
49
|
+
progress_info = progress_info + "({})etr {}".format( end_time_str, remaining_time_str )
|
|
50
|
+
|
|
51
|
+
print( time_str + " " + message + " " + progress_info )
|
|
52
|
+
return progress_info
|
|
41
53
|
|
|
42
54
|
|
|
43
55
|
# 计算持续时间
|
|
@@ -97,7 +109,7 @@ def time_diff(s_time = None, max_num = 0, language = "cn", return_duration = 1)
|
|
|
97
109
|
|
|
98
110
|
|
|
99
111
|
# 监控程序的执行时间
|
|
100
|
-
def
|
|
112
|
+
def time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs):
|
|
101
113
|
"""
|
|
102
114
|
监控函数执行时间,并返回函数的执行结果和执行时间
|
|
103
115
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: funcguard
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。
|
|
5
5
|
Home-page: https://github.com/tinycen/funcguard
|
|
6
6
|
Author: tinycen
|
|
@@ -181,10 +181,10 @@ for i in range(1, 101):
|
|
|
181
181
|
|
|
182
182
|
### 执行时间监控
|
|
183
183
|
|
|
184
|
-
使用`
|
|
184
|
+
使用`time_monitor`函数监控函数执行时间:
|
|
185
185
|
|
|
186
186
|
```python
|
|
187
|
-
from funcguard import
|
|
187
|
+
from funcguard import time_monitor
|
|
188
188
|
|
|
189
189
|
def some_function():
|
|
190
190
|
# 模拟一个耗时操作
|
|
@@ -193,14 +193,14 @@ def some_function():
|
|
|
193
193
|
return "操作完成"
|
|
194
194
|
|
|
195
195
|
# 模式1:总是打印执行时间
|
|
196
|
-
result =
|
|
196
|
+
result = time_monitor(
|
|
197
197
|
func=some_function,
|
|
198
198
|
print_mode=1
|
|
199
199
|
)
|
|
200
200
|
print(f"结果: {result}")
|
|
201
201
|
|
|
202
202
|
# 模式2:仅在超过阈值时打印警告
|
|
203
|
-
result =
|
|
203
|
+
result = time_monitor(
|
|
204
204
|
func=some_function,
|
|
205
205
|
warning_threshold=1.5, # 设置1.5秒的警告阈值
|
|
206
206
|
print_mode=2
|
|
@@ -208,7 +208,7 @@ result = monitor_execution_time(
|
|
|
208
208
|
print(f"结果: {result}")
|
|
209
209
|
|
|
210
210
|
# 模式0:不打印任何信息,仅返回结果和执行时间
|
|
211
|
-
result, duration =
|
|
211
|
+
result, duration = time_monitor(
|
|
212
212
|
func=some_function,
|
|
213
213
|
print_mode=0
|
|
214
214
|
)
|
|
@@ -293,7 +293,7 @@ print(f"结果: {result}, 耗时: {duration}秒")
|
|
|
293
293
|
- 否则返回None
|
|
294
294
|
- **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
|
|
295
295
|
|
|
296
|
-
####
|
|
296
|
+
#### time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)
|
|
297
297
|
|
|
298
298
|
- **参数**:
|
|
299
299
|
- `warning_threshold`: 警告阈值(秒),如果执行耗时超过此值则打印警告,默认为None
|
|
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
|