funcguard 0.1.3__tar.gz → 0.1.5__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,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: funcguard
3
- Version: 0.1.3
4
- Summary: A funcguard for Python.
3
+ Version: 0.1.5
4
+ Summary: FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。
5
5
  Home-page: https://github.com/tinycen/funcguard
6
6
  Author: tinycen
7
7
  Author-email: sky_ruocen@qq.com
@@ -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,61 @@ 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
+ # 记录任务开始(i从0开始)
148
+ time_log("开始处理数据", 0, 100, 0, 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, 0, start_time) # 显示进度和预计完成时间
156
+
157
+ # 记录任务完成并打印统计信息
158
+ time_log("数据处理完成", 100, 100, 0, start_time)
159
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
160
+ ```
161
+
162
+ 或者当i从1开始时:
163
+
164
+ ```python
165
+ from funcguard import time_log, time_diff
166
+
167
+ # 获取开始时间
168
+ start_time = time_diff()
169
+
170
+ # 记录任务开始(i从1开始)
171
+ time_log("开始处理数据", 1, 100, 1, start_time)
172
+
173
+ # 模拟处理过程
174
+ import time
175
+ for i in range(1, 101):
176
+ time.sleep(0.1) # 模拟处理时间
177
+ if i % 20 == 0:
178
+ time_log(f"处理进度", i, 100, 1, start_time) # 显示进度和预计完成时间
179
+
180
+ # 记录任务完成并打印统计信息
181
+ time_log("数据处理完成", 100, 100, 1, start_time)
182
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
183
+ ```
184
+
185
+ 时间日志功能特点:
186
+ - 自动显示北京时间(UTC+8)
187
+ - 支持进度显示和预计完成时间计算
188
+ - 提供中英文双语统计信息
189
+ - 可显示总耗时、平均耗时等详细统计
190
+ - 支持i从0或从1开始的计数方式
191
+
136
192
  ## API文档
137
193
 
138
194
  ### funcguard.core
@@ -174,6 +230,26 @@ print_block("API响应", result)
174
230
  - **返回值**: 根据return_type参数返回不同格式的响应数据
175
231
  - **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
176
232
 
233
+ #### time_log(message, i=0, max_num=0, start_from=0, s_time=None)
234
+
235
+ - **参数**:
236
+ - `message`: 日志消息
237
+ - `i`: 当前进度,默认为0
238
+ - `max_num`: 总进度数量,默认为0
239
+ - `start_from`: i是否从0开始,0表示从0开始,1表示从1开始,默认为0
240
+ - `s_time`: 开始时间,用于计算预计完成时间,默认为None
241
+ - **返回值**: 无
242
+ - **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
243
+
244
+ #### time_diff(s_time=None, max_num=0, language="cn")
245
+
246
+ - **参数**:
247
+ - `s_time`: 开始时间,默认为None
248
+ - `max_num`: 任务数量,默认为0
249
+ - `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
250
+ - **返回值**: 如果s_time为None则返回当前时间,否则返回None
251
+ - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
252
+
177
253
  ### funcguard.printer
178
254
 
179
255
  #### 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,61 @@ 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
+ # 记录任务开始(i从0开始)
134
+ time_log("开始处理数据", 0, 100, 0, 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, 0, start_time) # 显示进度和预计完成时间
142
+
143
+ # 记录任务完成并打印统计信息
144
+ time_log("数据处理完成", 100, 100, 0, start_time)
145
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
146
+ ```
147
+
148
+ 或者当i从1开始时:
149
+
150
+ ```python
151
+ from funcguard import time_log, time_diff
152
+
153
+ # 获取开始时间
154
+ start_time = time_diff()
155
+
156
+ # 记录任务开始(i从1开始)
157
+ time_log("开始处理数据", 1, 100, 1, start_time)
158
+
159
+ # 模拟处理过程
160
+ import time
161
+ for i in range(1, 101):
162
+ time.sleep(0.1) # 模拟处理时间
163
+ if i % 20 == 0:
164
+ time_log(f"处理进度", i, 100, 1, start_time) # 显示进度和预计完成时间
165
+
166
+ # 记录任务完成并打印统计信息
167
+ time_log("数据处理完成", 100, 100, 1, start_time)
168
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
169
+ ```
170
+
171
+ 时间日志功能特点:
172
+ - 自动显示北京时间(UTC+8)
173
+ - 支持进度显示和预计完成时间计算
174
+ - 提供中英文双语统计信息
175
+ - 可显示总耗时、平均耗时等详细统计
176
+ - 支持i从0或从1开始的计数方式
177
+
122
178
  ## API文档
123
179
 
124
180
  ### funcguard.core
@@ -160,6 +216,26 @@ print_block("API响应", result)
160
216
  - **返回值**: 根据return_type参数返回不同格式的响应数据
161
217
  - **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
162
218
 
219
+ #### time_log(message, i=0, max_num=0, start_from=0, s_time=None)
220
+
221
+ - **参数**:
222
+ - `message`: 日志消息
223
+ - `i`: 当前进度,默认为0
224
+ - `max_num`: 总进度数量,默认为0
225
+ - `start_from`: i是否从0开始,0表示从0开始,1表示从1开始,默认为0
226
+ - `s_time`: 开始时间,用于计算预计完成时间,默认为None
227
+ - **返回值**: 无
228
+ - **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
229
+
230
+ #### time_diff(s_time=None, max_num=0, language="cn")
231
+
232
+ - **参数**:
233
+ - `s_time`: 开始时间,默认为None
234
+ - `max_num`: 任务数量,默认为0
235
+ - `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
236
+ - **返回值**: 如果s_time为None则返回当前时间,否则返回None
237
+ - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
238
+
163
239
  ### funcguard.printer
164
240
 
165
241
  #### 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,148 @@
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, start_from = 0, s_time = None,) :
72
+ """
73
+ 打印带时间戳的日志信息,支持进度显示和预计完成时间
74
+
75
+ :param message: 日志消息
76
+ :param i: 当前进度
77
+ :param max_num: 总进度数量
78
+ :param start_from: i是否从0开始,0表示从0开始,1表示从1开始
79
+ :param s_time: 开始时间,用于计算预计完成时间
80
+ :return: None
81
+ """
82
+ now = datetime.now( timezone( timedelta( hours = 8 ) ) )
83
+ time_log = "{:02d}:{:02d}:{:02d}".format( now.hour, now.minute, now.second )
84
+ if i < 2 or max_num < 2 :
85
+ print( time_log + " " + message )
86
+
87
+ else :
88
+ # 根据start_from参数计算实际处理的项目数
89
+ process_item = i + 1 if start_from == 0 else i
90
+ text = "{}/{}".format( process_item, max_num )
91
+ # 检查是否应该显示预计完成时间和剩余时间
92
+ if process_item % 10 == 0 and s_time is not None and process_item < max_num :
93
+ duration = now - s_time
94
+ ev_duration = duration / process_item # 每项平均耗时
95
+ remaining_items = max_num - process_item
96
+ time_left = ev_duration * remaining_items
97
+ end_time = now + time_left
98
+ end_time_str = end_time.strftime( "%Y-%m-%d %H:%M" )
99
+ remaining_time_str = str( timedelta( seconds = int( time_left.total_seconds() ) ) )
100
+ text = text + "({})etr {}".format( end_time_str, remaining_time_str )
101
+ print( time_log + " " + message + " " + text )
102
+ return
103
+
104
+
105
+ # 计算持续时间
106
+ def time_diff(s_time = None, max_num = 0, language = "cn") :
107
+ """
108
+ 计算并打印任务执行时间统计信息
109
+
110
+ :param s_time: 开始时间
111
+ :param max_num: 任务数量
112
+ :param language: 语言选择("cn"中文,其他为英文)
113
+ :return: 如果s_time为None则返回当前时间,否则返回None
114
+ """
115
+ # 获取当前时间并转换为北京时间
116
+ now = datetime.now( timezone( timedelta( hours = 8 ) ) )
117
+
118
+ if s_time is None :
119
+ return now
120
+
121
+ e_time = now
122
+ duration = e_time - s_time
123
+ hours = duration.seconds // 3600
124
+ duration_minutes = (duration.seconds % 3600) // 60
125
+ seconds = duration.seconds % 60
126
+ result = f"{hours:02d}:{duration_minutes:02d}:{seconds:02d}"
127
+
128
+ # 将时间差转化为分钟
129
+ minutes = round( duration.total_seconds() / 60 )
130
+ if max_num == 0 :
131
+ if language == "cn" :
132
+ print( "总耗时:{}".format( result ) )
133
+ else :
134
+ print( "Total time: {}".format( result ) )
135
+ else :
136
+ eve_minutes = round( minutes / max_num, 3 )
137
+ if language == "cn" :
138
+ print( "开始时间:{},结束时间:{}".format( s_time.strftime( "%Y-%m-%d %H:%M" ),
139
+ e_time.strftime( "%Y-%m-%d %H:%M" ) ) )
140
+ print( "总耗时:{},累计:{}分钟,数量;{},平均耗时:{}分钟".format( result, minutes, max_num, eve_minutes ) )
141
+ else :
142
+ print( "Start time:{},End time:{}".format( s_time.strftime( "%Y-%m-%d %H:%M" ),
143
+ e_time.strftime( "%Y-%m-%d %H:%M" ) ) )
144
+ print( "Total time: {},Total minutes: {},Number: {},Average time: {} minutes".format( result, minutes,
145
+ max_num,
146
+ eve_minutes ) )
147
+
148
+ return
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: funcguard
3
- Version: 0.1.3
4
- Summary: A funcguard for Python.
3
+ Version: 0.1.5
4
+ Summary: FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。
5
5
  Home-page: https://github.com/tinycen/funcguard
6
6
  Author: tinycen
7
7
  Author-email: sky_ruocen@qq.com
@@ -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,61 @@ 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
+ # 记录任务开始(i从0开始)
148
+ time_log("开始处理数据", 0, 100, 0, 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, 0, start_time) # 显示进度和预计完成时间
156
+
157
+ # 记录任务完成并打印统计信息
158
+ time_log("数据处理完成", 100, 100, 0, start_time)
159
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
160
+ ```
161
+
162
+ 或者当i从1开始时:
163
+
164
+ ```python
165
+ from funcguard import time_log, time_diff
166
+
167
+ # 获取开始时间
168
+ start_time = time_diff()
169
+
170
+ # 记录任务开始(i从1开始)
171
+ time_log("开始处理数据", 1, 100, 1, start_time)
172
+
173
+ # 模拟处理过程
174
+ import time
175
+ for i in range(1, 101):
176
+ time.sleep(0.1) # 模拟处理时间
177
+ if i % 20 == 0:
178
+ time_log(f"处理进度", i, 100, 1, start_time) # 显示进度和预计完成时间
179
+
180
+ # 记录任务完成并打印统计信息
181
+ time_log("数据处理完成", 100, 100, 1, start_time)
182
+ time_diff(start_time, 100, "cn") # 中文显示统计信息
183
+ ```
184
+
185
+ 时间日志功能特点:
186
+ - 自动显示北京时间(UTC+8)
187
+ - 支持进度显示和预计完成时间计算
188
+ - 提供中英文双语统计信息
189
+ - 可显示总耗时、平均耗时等详细统计
190
+ - 支持i从0或从1开始的计数方式
191
+
136
192
  ## API文档
137
193
 
138
194
  ### funcguard.core
@@ -174,6 +230,26 @@ print_block("API响应", result)
174
230
  - **返回值**: 根据return_type参数返回不同格式的响应数据
175
231
  - **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
176
232
 
233
+ #### time_log(message, i=0, max_num=0, start_from=0, s_time=None)
234
+
235
+ - **参数**:
236
+ - `message`: 日志消息
237
+ - `i`: 当前进度,默认为0
238
+ - `max_num`: 总进度数量,默认为0
239
+ - `start_from`: i是否从0开始,0表示从0开始,1表示从1开始,默认为0
240
+ - `s_time`: 开始时间,用于计算预计完成时间,默认为None
241
+ - **返回值**: 无
242
+ - **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
243
+
244
+ #### time_diff(s_time=None, max_num=0, language="cn")
245
+
246
+ - **参数**:
247
+ - `s_time`: 开始时间,默认为None
248
+ - `max_num`: 任务数量,默认为0
249
+ - `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
250
+ - **返回值**: 如果s_time为None则返回当前时间,否则返回None
251
+ - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
252
+
177
253
  ### funcguard.printer
178
254
 
179
255
  #### print_line(separator_char: str = "-", separator_length: int = 40) -> None
@@ -9,14 +9,14 @@ except FileNotFoundError:
9
9
 
10
10
  setup(
11
11
  name='funcguard',
12
- version='0.1.3',
12
+ version='0.1.5',
13
13
  packages=find_packages(),
14
14
  install_requires=[
15
15
  'requests',
16
16
  ],
17
17
  author='tinycen',
18
18
  author_email='sky_ruocen@qq.com',
19
- description='A funcguard for Python.',
19
+ description='FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。',
20
20
  long_description=long_description,
21
21
  long_description_content_type='text/markdown',
22
22
  url='https://github.com/tinycen/funcguard',
@@ -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