funcguard 0.1.1__py3-none-any.whl → 0.1.3__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 +8 -1
- funcguard/core.py +8 -1
- funcguard/printer.py +32 -0
- funcguard/tools.py +44 -24
- {funcguard-0.1.1.dist-info → funcguard-0.1.3.dist-info}/METADATA +48 -5
- funcguard-0.1.3.dist-info/RECORD +13 -0
- tests/test_tools.py +17 -8
- funcguard-0.1.1.dist-info/RECORD +0 -12
- {funcguard-0.1.1.dist-info → funcguard-0.1.3.dist-info}/LICENSE +0 -0
- {funcguard-0.1.1.dist-info → funcguard-0.1.3.dist-info}/WHEEL +0 -0
- {funcguard-0.1.1.dist-info → funcguard-0.1.3.dist-info}/top_level.txt +0 -0
funcguard/__init__.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
from .core import timeout_handler, retry_function
|
|
2
2
|
from .tools import send_request
|
|
3
|
+
from .printer import print_block, print_line
|
|
3
4
|
|
|
4
5
|
__author__ = "ruocen"
|
|
5
6
|
|
|
6
7
|
# 暴露主要接口
|
|
7
|
-
__all__ = [
|
|
8
|
+
__all__ = [
|
|
9
|
+
"timeout_handler",
|
|
10
|
+
"retry_function",
|
|
11
|
+
"send_request",
|
|
12
|
+
"print_block",
|
|
13
|
+
"print_line",
|
|
14
|
+
]
|
funcguard/core.py
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
class FuncguardTimeoutError(Exception):
|
|
2
|
+
"""
|
|
3
|
+
funcguard库专用的超时异常类。
|
|
4
|
+
|
|
5
|
+
为了避免与concurrent.futures.TimeoutError和Python内置TimeoutError的命名冲突,
|
|
6
|
+
特定义此异常类来明确表示这是funcguard库抛出的函数执行超时异常。
|
|
7
|
+
这样用户可以清晰地区分异常来源,并进行针对性的异常处理。
|
|
8
|
+
"""
|
|
2
9
|
pass
|
|
3
10
|
|
|
4
11
|
import time
|
|
@@ -55,7 +62,7 @@ def retry_function( func , max_retries = 5 , execute_timeout = 90 , task_name =
|
|
|
55
62
|
result = timeout_handler( func , args = args , kwargs = kwargs , execution_timeout = current_timeout )
|
|
56
63
|
return result # 如果调用成功,则返回结果
|
|
57
64
|
|
|
58
|
-
except
|
|
65
|
+
except Exception as e :
|
|
59
66
|
last_exception = e
|
|
60
67
|
retry_count += 1
|
|
61
68
|
print( e )
|
funcguard/printer.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
# 打印分隔线
|
|
4
|
+
def print_line(separator_char: str = "-", separator_length: int = 40) -> None:
|
|
5
|
+
"""
|
|
6
|
+
打印分隔线,用于分隔不同的打印块
|
|
7
|
+
|
|
8
|
+
:param separator_char: 分隔符字符,默认为'-'
|
|
9
|
+
:param separator_length: 分隔符长度,默认为40
|
|
10
|
+
"""
|
|
11
|
+
separator = separator_char * separator_length
|
|
12
|
+
print(separator)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# 块打印
|
|
16
|
+
def print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None:
|
|
17
|
+
"""
|
|
18
|
+
使用分隔符打印标题和内容,便于查看
|
|
19
|
+
|
|
20
|
+
:param title: 标题
|
|
21
|
+
:param content: 打印的内容
|
|
22
|
+
:param separator_char: 分隔符字符,默认为'-'
|
|
23
|
+
:param separator_length: 分隔符长度,默认为40
|
|
24
|
+
"""
|
|
25
|
+
print_line(separator_char, separator_length)
|
|
26
|
+
|
|
27
|
+
if title:
|
|
28
|
+
print(f"{title} :")
|
|
29
|
+
print(content)
|
|
30
|
+
|
|
31
|
+
print_line(separator_char, separator_length)
|
|
32
|
+
# print() # 添加一个空行便于阅读
|
funcguard/tools.py
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import requests
|
|
3
|
-
from
|
|
4
|
-
|
|
3
|
+
from typing import Optional, Dict, Any, Union
|
|
4
|
+
from .core import retry_function
|
|
5
|
+
|
|
5
6
|
|
|
6
7
|
# 发起请求
|
|
7
|
-
def send_request(
|
|
8
|
-
|
|
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
|
+
"""
|
|
9
18
|
发送HTTP请求的通用函数
|
|
10
|
-
|
|
19
|
+
|
|
11
20
|
:param method: HTTP方法(GET, POST等)
|
|
12
21
|
:param url: 请求URL
|
|
13
22
|
:param headers: 请求头
|
|
@@ -17,30 +26,41 @@ def send_request( method , url , headers , data = None , return_type = "json" ,
|
|
|
17
26
|
:param auto_retry: 自动重试配置,格式为:
|
|
18
27
|
{"task_name": "任务名称", "max_retries": 最大重试次数, "execute_timeout": 执行超时时间}
|
|
19
28
|
:return: 请求结果
|
|
20
|
-
|
|
21
|
-
if data is None
|
|
22
|
-
payload = {
|
|
23
|
-
else
|
|
24
|
-
if (isinstance(
|
|
25
|
-
payload = json.dumps(
|
|
26
|
-
else
|
|
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:
|
|
27
36
|
payload = data
|
|
28
|
-
if auto_retry is None
|
|
29
|
-
response = requests.request(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
)
|
|
36
56
|
|
|
37
57
|
if response is None:
|
|
38
58
|
raise ValueError("请求返回的响应为None")
|
|
39
|
-
|
|
40
|
-
if return_type == "json"
|
|
59
|
+
|
|
60
|
+
if return_type == "json":
|
|
41
61
|
result = response.json()
|
|
42
|
-
elif return_type == "response"
|
|
62
|
+
elif return_type == "response":
|
|
43
63
|
return response
|
|
44
|
-
else
|
|
64
|
+
else:
|
|
45
65
|
result = response.text
|
|
46
66
|
return result
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: funcguard
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A funcguard for Python.
|
|
5
5
|
Home-page: https://github.com/tinycen/funcguard
|
|
6
6
|
Author: tinycen
|
|
@@ -22,13 +22,10 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
|
|
|
22
22
|
- 函数执行超时控制
|
|
23
23
|
- 函数执行失败自动重试
|
|
24
24
|
- HTTP请求封装(支持自动重试)
|
|
25
|
+
- 格式化打印工具(分隔线和块打印)
|
|
25
26
|
|
|
26
27
|
## 安装/升级
|
|
27
28
|
|
|
28
|
-
```bash
|
|
29
|
-
pip install funcguard
|
|
30
|
-
```
|
|
31
|
-
|
|
32
29
|
```bash
|
|
33
30
|
pip install --upgrade funcguard
|
|
34
31
|
```
|
|
@@ -111,6 +108,32 @@ response = send_request(
|
|
|
111
108
|
print(response)
|
|
112
109
|
```
|
|
113
110
|
|
|
111
|
+
### 格式化打印
|
|
112
|
+
|
|
113
|
+
使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from funcguard.printer import print_line, print_block
|
|
117
|
+
|
|
118
|
+
# 打印分隔线
|
|
119
|
+
print_line() # 默认使用40个'-'字符
|
|
120
|
+
print_line("*", 30) # 使用30个'*'字符
|
|
121
|
+
|
|
122
|
+
# 打印块内容
|
|
123
|
+
print_block("用户信息", {"name": "张三", "age": 25})
|
|
124
|
+
|
|
125
|
+
# 自定义分隔符
|
|
126
|
+
print_block("配置信息", {"debug": True, "port": 8080}, "=", 50)
|
|
127
|
+
|
|
128
|
+
# 打印复杂内容
|
|
129
|
+
result = {
|
|
130
|
+
"status": "success",
|
|
131
|
+
"data": [1, 2, 3, 4, 5],
|
|
132
|
+
"message": "操作完成"
|
|
133
|
+
}
|
|
134
|
+
print_block("API响应", result)
|
|
135
|
+
```
|
|
136
|
+
|
|
114
137
|
## API文档
|
|
115
138
|
|
|
116
139
|
### funcguard.core
|
|
@@ -152,6 +175,26 @@ print(response)
|
|
|
152
175
|
- **返回值**: 根据return_type参数返回不同格式的响应数据
|
|
153
176
|
- **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
|
|
154
177
|
|
|
178
|
+
### funcguard.printer
|
|
179
|
+
|
|
180
|
+
#### print_line(separator_char: str = "-", separator_length: int = 40) -> None
|
|
181
|
+
|
|
182
|
+
- **参数**:
|
|
183
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
184
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
185
|
+
- **返回值**: 无
|
|
186
|
+
- **功能**: 打印分隔线,用于分隔不同的打印块
|
|
187
|
+
|
|
188
|
+
#### print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None
|
|
189
|
+
|
|
190
|
+
- **参数**:
|
|
191
|
+
- `title`: 标题
|
|
192
|
+
- `content`: 打印的内容
|
|
193
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
194
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
195
|
+
- **返回值**: 无
|
|
196
|
+
- **功能**: 使用分隔符打印标题和内容,便于查看
|
|
197
|
+
|
|
155
198
|
## 许可证
|
|
156
199
|
|
|
157
200
|
MIT License
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
funcguard/__init__.py,sha256=i28xTP82-BwZAfvJdYQhV72kGe3BOHfx_5PkPw5OH34,288
|
|
2
|
+
funcguard/core.py,sha256=-rFRkE-udxM0wxlcv9Qi_yIQBRdVrGgwza-LYQsVRLg,3632
|
|
3
|
+
funcguard/printer.py,sha256=c93x6Z-6a7nnead_Rk_f0Xe23kdVdSj4qaxsuBMKfd0,958
|
|
4
|
+
funcguard/tools.py,sha256=RmnUFuGUR4M8ryFuBsiYgoMerPIUYPMnN8VPmxeg8M0,1971
|
|
5
|
+
tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
|
|
6
|
+
tests/run_test.py,sha256=-SLdUV7gDifLxuCCAlU8qLxYMx6KpK4cxfG4UYfUIgA,1005
|
|
7
|
+
tests/test_core.py,sha256=aZNbQK4eTnnkCI4c2txYZNTcYIUhSJvII5Dvn0vNJKo,3732
|
|
8
|
+
tests/test_tools.py,sha256=g9dK-WW1s5SIobscRuelufBfPrSgNLZU8d1AJteBpd0,5609
|
|
9
|
+
funcguard-0.1.3.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
|
|
10
|
+
funcguard-0.1.3.dist-info/METADATA,sha256=1AuwLbu3L2CtmFNFtpyqPkzQDZ3of0xEytqxmyytcuc,5528
|
|
11
|
+
funcguard-0.1.3.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
12
|
+
funcguard-0.1.3.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
|
|
13
|
+
funcguard-0.1.3.dist-info/RECORD,,
|
tests/test_tools.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import unittest
|
|
3
3
|
from unittest.mock import patch, MagicMock
|
|
4
|
+
from typing import Any, Optional, Dict
|
|
4
5
|
import requests
|
|
5
6
|
|
|
6
7
|
from funcguard.tools import send_request
|
|
@@ -9,12 +10,16 @@ from funcguard.tools import send_request
|
|
|
9
10
|
class MockResponse:
|
|
10
11
|
"""模拟requests响应"""
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
json_data: Dict[str, Any]
|
|
14
|
+
text: str
|
|
15
|
+
status_code: int
|
|
16
|
+
|
|
17
|
+
def __init__(self, json_data: Optional[Dict[str, Any]] = None, text: str = "", status_code: int = 200):
|
|
13
18
|
self.json_data = json_data or {}
|
|
14
19
|
self.text = text
|
|
15
20
|
self.status_code = status_code
|
|
16
21
|
|
|
17
|
-
def json(self):
|
|
22
|
+
def json(self) -> Dict[str, Any]:
|
|
18
23
|
return self.json_data
|
|
19
24
|
|
|
20
25
|
|
|
@@ -97,7 +102,7 @@ class TestSendRequest(unittest.TestCase):
|
|
|
97
102
|
|
|
98
103
|
mock_request.assert_called_once()
|
|
99
104
|
|
|
100
|
-
@patch('funcguard.
|
|
105
|
+
@patch('funcguard.tools.retry_function')
|
|
101
106
|
@patch('requests.request')
|
|
102
107
|
def test_auto_retry_enabled(self, mock_request, mock_retry):
|
|
103
108
|
"""测试启用自动重试"""
|
|
@@ -117,12 +122,16 @@ class TestSendRequest(unittest.TestCase):
|
|
|
117
122
|
}
|
|
118
123
|
)
|
|
119
124
|
|
|
120
|
-
# 验证结果
|
|
121
|
-
if
|
|
122
|
-
# 如果result
|
|
123
|
-
self.assertEqual(result
|
|
125
|
+
# 验证结果 - 使用更安全的方式处理可能的类型差异
|
|
126
|
+
if isinstance(result, dict):
|
|
127
|
+
# 如果result是字典(JSON解析结果)
|
|
128
|
+
self.assertEqual(result, {"success": True})
|
|
129
|
+
elif hasattr(result, 'json_data'):
|
|
130
|
+
# 如果result是MockResponse对象,使用getattr避免类型检查错误
|
|
131
|
+
json_data = getattr(result, 'json_data')
|
|
132
|
+
self.assertEqual(json_data, {"success": True})
|
|
124
133
|
else:
|
|
125
|
-
#
|
|
134
|
+
# 其他情况,直接比较
|
|
126
135
|
self.assertEqual(result, {"success": True})
|
|
127
136
|
mock_retry.assert_called_once()
|
|
128
137
|
|
funcguard-0.1.1.dist-info/RECORD
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
funcguard/__init__.py,sha256=He897o7aWOzpu2RnoRsjYQb5bhEMwC6XhFy0pot7-pA,189
|
|
2
|
-
funcguard/core.py,sha256=41C2bpVTaabK3QHN0vJmnhrKwnT_Ml8mxNitfN5gNdc,3291
|
|
3
|
-
funcguard/tools.py,sha256=UWvMUGj-ga_QbCZp1dJANeZbkyp76ozqbD4CpZkAymo,1807
|
|
4
|
-
tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
|
|
5
|
-
tests/run_test.py,sha256=-SLdUV7gDifLxuCCAlU8qLxYMx6KpK4cxfG4UYfUIgA,1005
|
|
6
|
-
tests/test_core.py,sha256=aZNbQK4eTnnkCI4c2txYZNTcYIUhSJvII5Dvn0vNJKo,3732
|
|
7
|
-
tests/test_tools.py,sha256=jBKoam-X3irTynaNg23V_3HgzAHBRvE6S9OyoalLfvI,5170
|
|
8
|
-
funcguard-0.1.1.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
|
|
9
|
-
funcguard-0.1.1.dist-info/METADATA,sha256=ns_c9tJ_fjnsQjAA3OiKT26cSpDfpqebE8y12608Lu8,4230
|
|
10
|
-
funcguard-0.1.1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
11
|
-
funcguard-0.1.1.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
|
|
12
|
-
funcguard-0.1.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|