funcguard 0.1.3__py3-none-any.whl → 0.1.4__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 funcguard might be problematic. Click here for more details.
- funcguard/__init__.py +3 -1
- funcguard/tools.py +82 -1
- {funcguard-0.1.3.dist-info → funcguard-0.1.4.dist-info}/METADATA +56 -5
- {funcguard-0.1.3.dist-info → funcguard-0.1.4.dist-info}/RECORD +7 -7
- {funcguard-0.1.3.dist-info → funcguard-0.1.4.dist-info}/LICENSE +0 -0
- {funcguard-0.1.3.dist-info → funcguard-0.1.4.dist-info}/WHEEL +0 -0
- {funcguard-0.1.3.dist-info → funcguard-0.1.4.dist-info}/top_level.txt +0 -0
funcguard/__init__.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from .core import timeout_handler, retry_function
|
|
2
|
-
from .tools import send_request
|
|
2
|
+
from .tools import send_request, time_log, time_diff
|
|
3
3
|
from .printer import print_block, print_line
|
|
4
4
|
|
|
5
5
|
__author__ = "ruocen"
|
|
@@ -9,6 +9,8 @@ __all__ = [
|
|
|
9
9
|
"timeout_handler",
|
|
10
10
|
"retry_function",
|
|
11
11
|
"send_request",
|
|
12
|
+
"time_log",
|
|
13
|
+
"time_diff",
|
|
12
14
|
"print_block",
|
|
13
15
|
"print_line",
|
|
14
16
|
]
|
funcguard/tools.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import requests
|
|
3
|
+
from datetime import datetime, timezone, timedelta
|
|
3
4
|
from typing import Optional, Dict, Any, Union
|
|
4
5
|
from .core import retry_function
|
|
5
6
|
|
|
@@ -63,4 +64,84 @@ def send_request(
|
|
|
63
64
|
return response
|
|
64
65
|
else:
|
|
65
66
|
result = response.text
|
|
66
|
-
return result
|
|
67
|
+
return result
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# 打印时间
|
|
71
|
+
def time_log(message, i = 0, max_num = 0, s_time = None) :
|
|
72
|
+
"""
|
|
73
|
+
打印带时间戳的日志信息,支持进度显示和预计完成时间
|
|
74
|
+
|
|
75
|
+
:param message: 日志消息
|
|
76
|
+
:param i: 当前进度(从0开始)
|
|
77
|
+
:param max_num: 总进度数量
|
|
78
|
+
:param s_time: 开始时间,用于计算预计完成时间
|
|
79
|
+
:return: None
|
|
80
|
+
"""
|
|
81
|
+
now = datetime.now( timezone( timedelta( hours = 8 ) ) )
|
|
82
|
+
time_log = "{:02d}:{:02d}:{:02d}".format( now.hour, now.minute, now.second )
|
|
83
|
+
if i < 2 :
|
|
84
|
+
print( time_log + " " + message )
|
|
85
|
+
else :
|
|
86
|
+
if max_num == 0 :
|
|
87
|
+
text = "{}".format( i )
|
|
88
|
+
else :
|
|
89
|
+
text = "{}/{}".format( i, max_num )
|
|
90
|
+
# 检查是否应该显示预计完成时间和剩余时间
|
|
91
|
+
if i % 10 == 0 and s_time is not None and i < max_num :
|
|
92
|
+
duration = now - s_time
|
|
93
|
+
ev_duration = duration / i # 每项平均耗时
|
|
94
|
+
remaining_items = max_num - i
|
|
95
|
+
time_left = ev_duration * remaining_items
|
|
96
|
+
end_time = now + time_left
|
|
97
|
+
end_time_str = end_time.strftime( "%Y-%m-%d %H:%M" )
|
|
98
|
+
remaining_time_str = str( timedelta( seconds = int( time_left.total_seconds() ) ) )
|
|
99
|
+
text = text + "({})etr {}".format( end_time_str, remaining_time_str )
|
|
100
|
+
print( time_log + " " + message + " " + text )
|
|
101
|
+
return
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# 计算持续时间
|
|
105
|
+
def time_diff(s_time = None, max_num = 0, language = "cn") :
|
|
106
|
+
"""
|
|
107
|
+
计算并打印任务执行时间统计信息
|
|
108
|
+
|
|
109
|
+
:param s_time: 开始时间
|
|
110
|
+
:param max_num: 任务数量
|
|
111
|
+
:param language: 语言选择("cn"中文,其他为英文)
|
|
112
|
+
:return: 如果s_time为None则返回当前时间,否则返回None
|
|
113
|
+
"""
|
|
114
|
+
# 获取当前时间并转换为北京时间
|
|
115
|
+
now = datetime.now( timezone( timedelta( hours = 8 ) ) )
|
|
116
|
+
|
|
117
|
+
if s_time is None :
|
|
118
|
+
return now
|
|
119
|
+
|
|
120
|
+
e_time = now
|
|
121
|
+
duration = e_time - s_time
|
|
122
|
+
hours = duration.seconds // 3600
|
|
123
|
+
duration_minutes = (duration.seconds % 3600) // 60
|
|
124
|
+
seconds = duration.seconds % 60
|
|
125
|
+
result = f"{hours:02d}:{duration_minutes:02d}:{seconds:02d}"
|
|
126
|
+
|
|
127
|
+
# 将时间差转化为分钟
|
|
128
|
+
minutes = round( duration.total_seconds() / 60 )
|
|
129
|
+
if max_num == 0 :
|
|
130
|
+
if language == "cn" :
|
|
131
|
+
print( "总耗时:{}".format( result ) )
|
|
132
|
+
else :
|
|
133
|
+
print( "Total time: {}".format( result ) )
|
|
134
|
+
else :
|
|
135
|
+
eve_minutes = round( minutes / max_num, 3 )
|
|
136
|
+
if language == "cn" :
|
|
137
|
+
print( "开始时间:{},结束时间:{}".format( s_time.strftime( "%Y-%m-%d %H:%M" ),
|
|
138
|
+
e_time.strftime( "%Y-%m-%d %H:%M" ) ) )
|
|
139
|
+
print( "总耗时:{},累计:{}分钟,数量;{},平均耗时:{}分钟".format( result, minutes, max_num, eve_minutes ) )
|
|
140
|
+
else :
|
|
141
|
+
print( "Start time:{},End time:{}".format( s_time.strftime( "%Y-%m-%d %H:%M" ),
|
|
142
|
+
e_time.strftime( "%Y-%m-%d %H:%M" ) ) )
|
|
143
|
+
print( "Total time: {},Total minutes: {},Number: {},Average time: {} minutes".format( result, minutes,
|
|
144
|
+
max_num,
|
|
145
|
+
eve_minutes ) )
|
|
146
|
+
|
|
147
|
+
return
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: funcguard
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: A funcguard for Python.
|
|
5
5
|
Home-page: https://github.com/tinycen/funcguard
|
|
6
6
|
Author: tinycen
|
|
@@ -23,6 +23,7 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
|
|
|
23
23
|
- 函数执行失败自动重试
|
|
24
24
|
- HTTP请求封装(支持自动重试)
|
|
25
25
|
- 格式化打印工具(分隔线和块打印)
|
|
26
|
+
- 时间日志记录和耗时统计
|
|
26
27
|
|
|
27
28
|
## 安装/升级
|
|
28
29
|
|
|
@@ -38,7 +39,7 @@ pip install --upgrade funcguard
|
|
|
38
39
|
使用`timeout_handler`函数可以控制函数的执行时间,防止函数运行时间过长:
|
|
39
40
|
|
|
40
41
|
```python
|
|
41
|
-
from funcguard
|
|
42
|
+
from funcguard import timeout_handler
|
|
42
43
|
|
|
43
44
|
def long_running_function():
|
|
44
45
|
# 模拟一个耗时操作
|
|
@@ -59,7 +60,7 @@ except TimeoutError as e:
|
|
|
59
60
|
使用`retry_function`函数可以在函数执行失败时自动重试:
|
|
60
61
|
|
|
61
62
|
```python
|
|
62
|
-
from funcguard
|
|
63
|
+
from funcguard import retry_function
|
|
63
64
|
|
|
64
65
|
def unstable_function():
|
|
65
66
|
# 模拟一个可能失败的操作
|
|
@@ -81,7 +82,7 @@ except Exception as e:
|
|
|
81
82
|
使用`send_request`函数发送HTTP请求,支持自动重试:
|
|
82
83
|
|
|
83
84
|
```python
|
|
84
|
-
from funcguard
|
|
85
|
+
from funcguard import send_request
|
|
85
86
|
|
|
86
87
|
# 不使用重试
|
|
87
88
|
response = send_request(
|
|
@@ -113,7 +114,7 @@ print(response)
|
|
|
113
114
|
使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
|
|
114
115
|
|
|
115
116
|
```python
|
|
116
|
-
from funcguard
|
|
117
|
+
from funcguard import print_line, print_block
|
|
117
118
|
|
|
118
119
|
# 打印分隔线
|
|
119
120
|
print_line() # 默认使用40个'-'字符
|
|
@@ -134,6 +135,37 @@ result = {
|
|
|
134
135
|
print_block("API响应", result)
|
|
135
136
|
```
|
|
136
137
|
|
|
138
|
+
### 时间日志记录
|
|
139
|
+
|
|
140
|
+
使用`time_log`和`time_diff`函数记录任务执行时间和统计信息:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from funcguard import time_log, time_diff
|
|
144
|
+
|
|
145
|
+
# 获取开始时间
|
|
146
|
+
start_time = time_diff()
|
|
147
|
+
|
|
148
|
+
# 记录任务开始
|
|
149
|
+
time_log("开始处理数据", 0, 100, start_time)
|
|
150
|
+
|
|
151
|
+
# 模拟处理过程
|
|
152
|
+
import time
|
|
153
|
+
for i in range(1, 101):
|
|
154
|
+
time.sleep(0.1) # 模拟处理时间
|
|
155
|
+
if i % 20 == 0:
|
|
156
|
+
time_log(f"处理进度", i, 100, start_time) # 显示进度和预计完成时间
|
|
157
|
+
|
|
158
|
+
# 记录任务完成并打印统计信息
|
|
159
|
+
time_log("数据处理完成", 100, 100, start_time)
|
|
160
|
+
time_diff(start_time, 100, "cn") # 中文显示统计信息
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
时间日志功能特点:
|
|
164
|
+
- 自动显示北京时间(UTC+8)
|
|
165
|
+
- 支持进度显示和预计完成时间计算
|
|
166
|
+
- 提供中英文双语统计信息
|
|
167
|
+
- 可显示总耗时、平均耗时等详细统计
|
|
168
|
+
|
|
137
169
|
## API文档
|
|
138
170
|
|
|
139
171
|
### funcguard.core
|
|
@@ -175,6 +207,25 @@ print_block("API响应", result)
|
|
|
175
207
|
- **返回值**: 根据return_type参数返回不同格式的响应数据
|
|
176
208
|
- **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
|
|
177
209
|
|
|
210
|
+
#### time_log(message, i=0, max_num=0, s_time=None)
|
|
211
|
+
|
|
212
|
+
- **参数**:
|
|
213
|
+
- `message`: 日志消息
|
|
214
|
+
- `i`: 当前进度(从0开始),默认为0
|
|
215
|
+
- `max_num`: 总进度数量,默认为0
|
|
216
|
+
- `s_time`: 开始时间,用于计算预计完成时间,默认为None
|
|
217
|
+
- **返回值**: 无
|
|
218
|
+
- **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
|
|
219
|
+
|
|
220
|
+
#### time_diff(s_time=None, max_num=0, language="cn")
|
|
221
|
+
|
|
222
|
+
- **参数**:
|
|
223
|
+
- `s_time`: 开始时间,默认为None
|
|
224
|
+
- `max_num`: 任务数量,默认为0
|
|
225
|
+
- `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
|
|
226
|
+
- **返回值**: 如果s_time为None则返回当前时间,否则返回None
|
|
227
|
+
- **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
|
|
228
|
+
|
|
178
229
|
### funcguard.printer
|
|
179
230
|
|
|
180
231
|
#### print_line(separator_char: str = "-", separator_length: int = 40) -> None
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
funcguard/__init__.py,sha256=
|
|
1
|
+
funcguard/__init__.py,sha256=iyM7STP-KSxUn7PUaBWEQcZus2XskiGTTweI4qjoETE,342
|
|
2
2
|
funcguard/core.py,sha256=-rFRkE-udxM0wxlcv9Qi_yIQBRdVrGgwza-LYQsVRLg,3632
|
|
3
3
|
funcguard/printer.py,sha256=c93x6Z-6a7nnead_Rk_f0Xe23kdVdSj4qaxsuBMKfd0,958
|
|
4
|
-
funcguard/tools.py,sha256=
|
|
4
|
+
funcguard/tools.py,sha256=S7t46KurOedvVKCVoFLW_CoDvLSC__lfVLUYBse5ABM,5433
|
|
5
5
|
tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
|
|
6
6
|
tests/run_test.py,sha256=-SLdUV7gDifLxuCCAlU8qLxYMx6KpK4cxfG4UYfUIgA,1005
|
|
7
7
|
tests/test_core.py,sha256=aZNbQK4eTnnkCI4c2txYZNTcYIUhSJvII5Dvn0vNJKo,3732
|
|
8
8
|
tests/test_tools.py,sha256=g9dK-WW1s5SIobscRuelufBfPrSgNLZU8d1AJteBpd0,5609
|
|
9
|
-
funcguard-0.1.
|
|
10
|
-
funcguard-0.1.
|
|
11
|
-
funcguard-0.1.
|
|
12
|
-
funcguard-0.1.
|
|
13
|
-
funcguard-0.1.
|
|
9
|
+
funcguard-0.1.4.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
|
|
10
|
+
funcguard-0.1.4.dist-info/METADATA,sha256=ayz3u_YGwUqH3ziy-awhbPMjWgFdPWYCxSQmTZJF-HI,7175
|
|
11
|
+
funcguard-0.1.4.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
12
|
+
funcguard-0.1.4.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
|
|
13
|
+
funcguard-0.1.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|