funcguard 0.1.3__tar.gz → 0.1.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.

Potentially problematic release.


This version of funcguard might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: funcguard
3
- Version: 0.1.3
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
@@ -22,6 +22,7 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
22
22
  - 函数执行失败自动重试
23
23
  - HTTP请求封装(支持自动重试)
24
24
  - 格式化打印工具(分隔线和块打印)
25
+ - 时间日志记录和耗时统计
25
26
 
26
27
  ## 安装/升级
27
28
 
@@ -37,7 +38,7 @@ pip install --upgrade funcguard
37
38
  使用`timeout_handler`函数可以控制函数的执行时间,防止函数运行时间过长:
38
39
 
39
40
  ```python
40
- from funcguard.core import timeout_handler
41
+ from funcguard import timeout_handler
41
42
 
42
43
  def long_running_function():
43
44
  # 模拟一个耗时操作
@@ -58,7 +59,7 @@ except TimeoutError as e:
58
59
  使用`retry_function`函数可以在函数执行失败时自动重试:
59
60
 
60
61
  ```python
61
- from funcguard.core import retry_function
62
+ from funcguard import retry_function
62
63
 
63
64
  def unstable_function():
64
65
  # 模拟一个可能失败的操作
@@ -80,7 +81,7 @@ except Exception as e:
80
81
  使用`send_request`函数发送HTTP请求,支持自动重试:
81
82
 
82
83
  ```python
83
- from funcguard.tools import send_request
84
+ from funcguard import send_request
84
85
 
85
86
  # 不使用重试
86
87
  response = send_request(
@@ -112,7 +113,7 @@ print(response)
112
113
  使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
113
114
 
114
115
  ```python
115
- from funcguard.printer import print_line, print_block
116
+ from funcguard import print_line, print_block
116
117
 
117
118
  # 打印分隔线
118
119
  print_line() # 默认使用40个'-'字符
@@ -133,6 +134,37 @@ result = {
133
134
  print_block("API响应", result)
134
135
  ```
135
136
 
137
+ ### 时间日志记录
138
+
139
+ 使用`time_log`和`time_diff`函数记录任务执行时间和统计信息:
140
+
141
+ ```python
142
+ from funcguard import time_log, time_diff
143
+
144
+ # 获取开始时间
145
+ start_time = time_diff()
146
+
147
+ # 记录任务开始
148
+ time_log("开始处理数据", 0, 100, start_time)
149
+
150
+ # 模拟处理过程
151
+ import time
152
+ for i in range(1, 101):
153
+ time.sleep(0.1) # 模拟处理时间
154
+ if i % 20 == 0:
155
+ time_log(f"处理进度", i, 100, start_time) # 显示进度和预计完成时间
156
+
157
+ # 记录任务完成并打印统计信息
158
+ time_log("数据处理完成", 100, 100, start_time)
159
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
160
+ ```
161
+
162
+ 时间日志功能特点:
163
+ - 自动显示北京时间(UTC+8)
164
+ - 支持进度显示和预计完成时间计算
165
+ - 提供中英文双语统计信息
166
+ - 可显示总耗时、平均耗时等详细统计
167
+
136
168
  ## API文档
137
169
 
138
170
  ### funcguard.core
@@ -174,6 +206,25 @@ print_block("API响应", result)
174
206
  - **返回值**: 根据return_type参数返回不同格式的响应数据
175
207
  - **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
176
208
 
209
+ #### time_log(message, i=0, max_num=0, s_time=None)
210
+
211
+ - **参数**:
212
+ - `message`: 日志消息
213
+ - `i`: 当前进度(从0开始),默认为0
214
+ - `max_num`: 总进度数量,默认为0
215
+ - `s_time`: 开始时间,用于计算预计完成时间,默认为None
216
+ - **返回值**: 无
217
+ - **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
218
+
219
+ #### time_diff(s_time=None, max_num=0, language="cn")
220
+
221
+ - **参数**:
222
+ - `s_time`: 开始时间,默认为None
223
+ - `max_num`: 任务数量,默认为0
224
+ - `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
225
+ - **返回值**: 如果s_time为None则返回当前时间,否则返回None
226
+ - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
227
+
177
228
  ### funcguard.printer
178
229
 
179
230
  #### print_line(separator_char: str = "-", separator_length: int = 40) -> None
@@ -8,6 +8,7 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
8
8
  - 函数执行失败自动重试
9
9
  - HTTP请求封装(支持自动重试)
10
10
  - 格式化打印工具(分隔线和块打印)
11
+ - 时间日志记录和耗时统计
11
12
 
12
13
  ## 安装/升级
13
14
 
@@ -23,7 +24,7 @@ pip install --upgrade funcguard
23
24
  使用`timeout_handler`函数可以控制函数的执行时间,防止函数运行时间过长:
24
25
 
25
26
  ```python
26
- from funcguard.core import timeout_handler
27
+ from funcguard import timeout_handler
27
28
 
28
29
  def long_running_function():
29
30
  # 模拟一个耗时操作
@@ -44,7 +45,7 @@ except TimeoutError as e:
44
45
  使用`retry_function`函数可以在函数执行失败时自动重试:
45
46
 
46
47
  ```python
47
- from funcguard.core import retry_function
48
+ from funcguard import retry_function
48
49
 
49
50
  def unstable_function():
50
51
  # 模拟一个可能失败的操作
@@ -66,7 +67,7 @@ except Exception as e:
66
67
  使用`send_request`函数发送HTTP请求,支持自动重试:
67
68
 
68
69
  ```python
69
- from funcguard.tools import send_request
70
+ from funcguard import send_request
70
71
 
71
72
  # 不使用重试
72
73
  response = send_request(
@@ -98,7 +99,7 @@ print(response)
98
99
  使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
99
100
 
100
101
  ```python
101
- from funcguard.printer import print_line, print_block
102
+ from funcguard import print_line, print_block
102
103
 
103
104
  # 打印分隔线
104
105
  print_line() # 默认使用40个'-'字符
@@ -119,6 +120,37 @@ result = {
119
120
  print_block("API响应", result)
120
121
  ```
121
122
 
123
+ ### 时间日志记录
124
+
125
+ 使用`time_log`和`time_diff`函数记录任务执行时间和统计信息:
126
+
127
+ ```python
128
+ from funcguard import time_log, time_diff
129
+
130
+ # 获取开始时间
131
+ start_time = time_diff()
132
+
133
+ # 记录任务开始
134
+ time_log("开始处理数据", 0, 100, start_time)
135
+
136
+ # 模拟处理过程
137
+ import time
138
+ for i in range(1, 101):
139
+ time.sleep(0.1) # 模拟处理时间
140
+ if i % 20 == 0:
141
+ time_log(f"处理进度", i, 100, start_time) # 显示进度和预计完成时间
142
+
143
+ # 记录任务完成并打印统计信息
144
+ time_log("数据处理完成", 100, 100, start_time)
145
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
146
+ ```
147
+
148
+ 时间日志功能特点:
149
+ - 自动显示北京时间(UTC+8)
150
+ - 支持进度显示和预计完成时间计算
151
+ - 提供中英文双语统计信息
152
+ - 可显示总耗时、平均耗时等详细统计
153
+
122
154
  ## API文档
123
155
 
124
156
  ### funcguard.core
@@ -160,6 +192,25 @@ print_block("API响应", result)
160
192
  - **返回值**: 根据return_type参数返回不同格式的响应数据
161
193
  - **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
162
194
 
195
+ #### time_log(message, i=0, max_num=0, s_time=None)
196
+
197
+ - **参数**:
198
+ - `message`: 日志消息
199
+ - `i`: 当前进度(从0开始),默认为0
200
+ - `max_num`: 总进度数量,默认为0
201
+ - `s_time`: 开始时间,用于计算预计完成时间,默认为None
202
+ - **返回值**: 无
203
+ - **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
204
+
205
+ #### time_diff(s_time=None, max_num=0, language="cn")
206
+
207
+ - **参数**:
208
+ - `s_time`: 开始时间,默认为None
209
+ - `max_num`: 任务数量,默认为0
210
+ - `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
211
+ - **返回值**: 如果s_time为None则返回当前时间,否则返回None
212
+ - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
213
+
163
214
  ### funcguard.printer
164
215
 
165
216
  #### print_line(separator_char: str = "-", separator_length: int = 40) -> None
@@ -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
  ]
@@ -0,0 +1,147 @@
1
+ import json
2
+ import requests
3
+ from datetime import datetime, timezone, timedelta
4
+ from typing import Optional, Dict, Any, Union
5
+ from .core import retry_function
6
+
7
+
8
+ # 发起请求
9
+ def send_request(
10
+ method: str,
11
+ url: str,
12
+ headers: Dict[str, str],
13
+ data: Optional[Any] = None,
14
+ return_type: str = "json",
15
+ timeout: int = 60,
16
+ auto_retry: Optional[Dict[str, Any]] = None,
17
+ ) -> Union[Dict, str, requests.Response]:
18
+ """
19
+ 发送HTTP请求的通用函数
20
+
21
+ :param method: HTTP方法(GET, POST等)
22
+ :param url: 请求URL
23
+ :param headers: 请求头
24
+ :param data: 请求数据
25
+ :param return_type: 返回类型(json, text, response)
26
+ :param timeout: 请求超时时间
27
+ :param auto_retry: 自动重试配置,格式为:
28
+ {"task_name": "任务名称", "max_retries": 最大重试次数, "execute_timeout": 执行超时时间}
29
+ :return: 请求结果
30
+ """
31
+ if data is None:
32
+ payload = {}
33
+ else:
34
+ if (isinstance(data, dict) or isinstance(data, list)) and data != {}:
35
+ payload = json.dumps(data, ensure_ascii=False)
36
+ else:
37
+ payload = data
38
+ if auto_retry is None:
39
+ response = requests.request(
40
+ method, url, headers=headers, data=payload, timeout=timeout
41
+ )
42
+ else:
43
+ max_retries = auto_retry.get("max_retries", 5)
44
+ execute_timeout = auto_retry.get("execute_timeout", 90)
45
+ task_name = auto_retry.get("task_name", "")
46
+ response = retry_function(
47
+ requests.request,
48
+ max_retries,
49
+ execute_timeout,
50
+ task_name,
51
+ method,
52
+ url,
53
+ headers=headers,
54
+ data=payload,
55
+ timeout=timeout,
56
+ )
57
+
58
+ if response is None:
59
+ raise ValueError("请求返回的响应为None")
60
+
61
+ if return_type == "json":
62
+ result = response.json()
63
+ elif return_type == "response":
64
+ return response
65
+ else:
66
+ result = response.text
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
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
@@ -22,6 +22,7 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
22
22
  - 函数执行失败自动重试
23
23
  - HTTP请求封装(支持自动重试)
24
24
  - 格式化打印工具(分隔线和块打印)
25
+ - 时间日志记录和耗时统计
25
26
 
26
27
  ## 安装/升级
27
28
 
@@ -37,7 +38,7 @@ pip install --upgrade funcguard
37
38
  使用`timeout_handler`函数可以控制函数的执行时间,防止函数运行时间过长:
38
39
 
39
40
  ```python
40
- from funcguard.core import timeout_handler
41
+ from funcguard import timeout_handler
41
42
 
42
43
  def long_running_function():
43
44
  # 模拟一个耗时操作
@@ -58,7 +59,7 @@ except TimeoutError as e:
58
59
  使用`retry_function`函数可以在函数执行失败时自动重试:
59
60
 
60
61
  ```python
61
- from funcguard.core import retry_function
62
+ from funcguard import retry_function
62
63
 
63
64
  def unstable_function():
64
65
  # 模拟一个可能失败的操作
@@ -80,7 +81,7 @@ except Exception as e:
80
81
  使用`send_request`函数发送HTTP请求,支持自动重试:
81
82
 
82
83
  ```python
83
- from funcguard.tools import send_request
84
+ from funcguard import send_request
84
85
 
85
86
  # 不使用重试
86
87
  response = send_request(
@@ -112,7 +113,7 @@ print(response)
112
113
  使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
113
114
 
114
115
  ```python
115
- from funcguard.printer import print_line, print_block
116
+ from funcguard import print_line, print_block
116
117
 
117
118
  # 打印分隔线
118
119
  print_line() # 默认使用40个'-'字符
@@ -133,6 +134,37 @@ result = {
133
134
  print_block("API响应", result)
134
135
  ```
135
136
 
137
+ ### 时间日志记录
138
+
139
+ 使用`time_log`和`time_diff`函数记录任务执行时间和统计信息:
140
+
141
+ ```python
142
+ from funcguard import time_log, time_diff
143
+
144
+ # 获取开始时间
145
+ start_time = time_diff()
146
+
147
+ # 记录任务开始
148
+ time_log("开始处理数据", 0, 100, start_time)
149
+
150
+ # 模拟处理过程
151
+ import time
152
+ for i in range(1, 101):
153
+ time.sleep(0.1) # 模拟处理时间
154
+ if i % 20 == 0:
155
+ time_log(f"处理进度", i, 100, start_time) # 显示进度和预计完成时间
156
+
157
+ # 记录任务完成并打印统计信息
158
+ time_log("数据处理完成", 100, 100, start_time)
159
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
160
+ ```
161
+
162
+ 时间日志功能特点:
163
+ - 自动显示北京时间(UTC+8)
164
+ - 支持进度显示和预计完成时间计算
165
+ - 提供中英文双语统计信息
166
+ - 可显示总耗时、平均耗时等详细统计
167
+
136
168
  ## API文档
137
169
 
138
170
  ### funcguard.core
@@ -174,6 +206,25 @@ print_block("API响应", result)
174
206
  - **返回值**: 根据return_type参数返回不同格式的响应数据
175
207
  - **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
176
208
 
209
+ #### time_log(message, i=0, max_num=0, s_time=None)
210
+
211
+ - **参数**:
212
+ - `message`: 日志消息
213
+ - `i`: 当前进度(从0开始),默认为0
214
+ - `max_num`: 总进度数量,默认为0
215
+ - `s_time`: 开始时间,用于计算预计完成时间,默认为None
216
+ - **返回值**: 无
217
+ - **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
218
+
219
+ #### time_diff(s_time=None, max_num=0, language="cn")
220
+
221
+ - **参数**:
222
+ - `s_time`: 开始时间,默认为None
223
+ - `max_num`: 任务数量,默认为0
224
+ - `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
225
+ - **返回值**: 如果s_time为None则返回当前时间,否则返回None
226
+ - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
227
+
177
228
  ### funcguard.printer
178
229
 
179
230
  #### print_line(separator_char: str = "-", separator_length: int = 40) -> None
@@ -9,7 +9,7 @@ except FileNotFoundError:
9
9
 
10
10
  setup(
11
11
  name='funcguard',
12
- version='0.1.3',
12
+ version='0.1.4',
13
13
  packages=find_packages(),
14
14
  install_requires=[
15
15
  'requests',
@@ -1,66 +0,0 @@
1
- import json
2
- import requests
3
- from typing import Optional, Dict, Any, Union
4
- from .core import retry_function
5
-
6
-
7
- # 发起请求
8
- def send_request(
9
- method: str,
10
- url: str,
11
- headers: Dict[str, str],
12
- data: Optional[Any] = None,
13
- return_type: str = "json",
14
- timeout: int = 60,
15
- auto_retry: Optional[Dict[str, Any]] = None,
16
- ) -> Union[Dict, str, requests.Response]:
17
- """
18
- 发送HTTP请求的通用函数
19
-
20
- :param method: HTTP方法(GET, POST等)
21
- :param url: 请求URL
22
- :param headers: 请求头
23
- :param data: 请求数据
24
- :param return_type: 返回类型(json, text, response)
25
- :param timeout: 请求超时时间
26
- :param auto_retry: 自动重试配置,格式为:
27
- {"task_name": "任务名称", "max_retries": 最大重试次数, "execute_timeout": 执行超时时间}
28
- :return: 请求结果
29
- """
30
- if data is None:
31
- payload = {}
32
- else:
33
- if (isinstance(data, dict) or isinstance(data, list)) and data != {}:
34
- payload = json.dumps(data, ensure_ascii=False)
35
- else:
36
- payload = data
37
- if auto_retry is None:
38
- response = requests.request(
39
- method, url, headers=headers, data=payload, timeout=timeout
40
- )
41
- else:
42
- max_retries = auto_retry.get("max_retries", 5)
43
- execute_timeout = auto_retry.get("execute_timeout", 90)
44
- task_name = auto_retry.get("task_name", "")
45
- response = retry_function(
46
- requests.request,
47
- max_retries,
48
- execute_timeout,
49
- task_name,
50
- method,
51
- url,
52
- headers=headers,
53
- data=payload,
54
- timeout=timeout,
55
- )
56
-
57
- if response is None:
58
- raise ValueError("请求返回的响应为None")
59
-
60
- if return_type == "json":
61
- result = response.json()
62
- elif return_type == "response":
63
- return response
64
- else:
65
- result = response.text
66
- return result
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes