# FuncGuard
FuncGuard是一个Python库,提供了函数执行超时控制和重试机制的实用工具。
## 功能特点
- 函数执行超时控制
- 函数执行失败自动重试
- HTTP请求封装(支持自动重试)
- 格式化打印工具(分隔线和块打印)
- 时间日志记录和耗时统计
- 函数执行时间监控和警告
## 安装/升级
```bash
pip install --upgrade funcguard
```
## 使用方法
### 超时控制
使用`timeout_handler`函数可以控制函数的执行时间,防止函数运行时间过长:
```python
from funcguard import timeout_handler
def long_running_function():
# 模拟一个耗时操作
import time
time.sleep(10)
return "操作完成"
try:
# 设置超时时间为5秒
result = timeout_handler(long_running_function, execution_timeout=5)
print(result)
except TimeoutError as e:
print(f"捕获到超时错误: {e}")
```
### 重试机制
使用`retry_function`函数可以在函数执行失败时自动重试:
```python
from funcguard import retry_function
def unstable_function():
# 模拟一个可能失败的操作
import random
if random.random() < 0.7: # 70%的概率失败
raise Exception("随机错误")
return "操作成功"
try:
# 最多重试3次,每次执行超时时间为10秒
result = retry_function(unstable_function, max_retries=3, execute_timeout=10, task_name="测试任务")
print(result)
except Exception as e:
print(f"重试后仍然失败: {e}")
```
### HTTP请求
使用`send_request`函数发送HTTP请求,支持自动重试:
```python
from funcguard import send_request
# 不使用重试
response = send_request(
method="GET",
url="https://api.example.com/data",
headers={"Content-Type": "application/json"},
timeout=30
)
print(response)
# 使用重试
response = send_request(
method="POST",
url="https://api.example.com/data",
headers={"Content-Type": "application/json"},
data={"key": "value"},
timeout=30,
auto_retry={
"task_name": "API请求",
"max_retries": 3,
"execute_timeout": 60
}
)
print(response)
```
### 格式化打印
使用`print_line`、`print_block`和`print_title`函数进行格式化打印,便于查看和调试:
```python
from funcguard import print_line, print_block, print_title
# 打印带等号的标题
print_title("初始化分类器") # 输出:=== 初始化分类器 ===
print_title("训练完成", separator_char="*", padding_length=2) # 输出:** 训练完成 **
# 打印分隔线
print_line() # 默认使用40个'-'字符
print_line("*", 30) # 使用30个'*'字符
# 打印块内容
print_block("用户信息", {"name": "张三", "age": 25})
# 自定义分隔符
print_block("配置信息", {"debug": True, "port": 8080}, "=", 50)
# 打印复杂内容
result = {
"status": "success",
"data": [1, 2, 3, 4, 5],
"message": "操作完成"
}
print_block("API响应", result)
```
### 时间日志记录
使用`time_log`和`time_diff`函数记录任务执行时间和统计信息:
```python
from funcguard import time_log, time_diff
# 获取开始时间
start_time = time_diff()
# 记录任务开始(i从0开始)
time_log("开始处理数据", 0, 100, start_time, 0)
# 模拟处理过程
import time
for i in range(1, 101):
time.sleep(0.1) # 模拟处理时间
if i % 20 == 0:
time_log(f"处理进度", i, 100, start_time, 0) # 显示进度和预计完成时间
# 记录任务完成并打印统计信息
time_diff(start_time, 100, "cn") # 中文显示统计信息
```
或者当i从1开始时:
```python
# 记录任务开始(i从1开始)
time_log("开始处理数据", 1, 100, start_time, 1)
# 模拟处理过程
import time
for i in range(1, 101):
time.sleep(0.1) # 模拟处理时间
if i % 20 == 0:
time_log(f"处理进度", i, 100, start_time, 1) # 显示进度和预计完成时间
```
### 执行时间监控
使用`time_monitor`函数监控函数执行时间:
```python
from funcguard import time_monitor
def some_function():
# 模拟一个耗时操作
import time
time.sleep(2)
return "操作完成"
# 模式1:总是打印执行时间
result = time_monitor(
func=some_function,
print_mode=1
)
print(f"结果: {result}")
# 模式2:仅在超过阈值时打印警告
result = time_monitor(
func=some_function,
warning_threshold=1.5, # 设置1.5秒的警告阈值
print_mode=2
)
print(f"结果: {result}")
# 模式0:不打印任何信息,仅返回结果和执行时间
result, duration = time_monitor(
func=some_function,
print_mode=0
)
print(f"结果: {result}, 耗时: {duration}秒")
```
时间日志功能特点:
- 自动显示北京时间(UTC+8)
- 支持进度显示和预计完成时间计算
- 提供中英文双语统计信息
- 可显示总耗时、平均耗时等详细统计
- 支持i从0或从1开始的计数方式
- 支持函数执行时间监控和警告
## API文档
### funcguard.core
#### timeout_handler(func, args=(), kwargs=None, execution_timeout=90)
- **参数**:
- `func`: 需要执行的目标函数
- `args`: 目标函数的位置参数,默认为空元组
- `kwargs`: 目标函数的关键字参数,默认为None
- `execution_timeout`: 函数执行的超时时间,单位为秒,默认为90秒
- **返回值**: 目标函数的返回值
- **异常**: `TimeoutError` - 当函数执行超过指定时间时抛出
#### retry_function(func, max_retries=5, execute_timeout=90, task_name="", *args, **kwargs)
- **参数**:
- `func`: 需要重试的函数
- `max_retries`: 最大重试次数,默认为5
- `execute_timeout`: 执行超时时间,默认为90秒
- `task_name`: 任务名称,用于打印日志
- `args`: func的位置参数
- `kwargs`: func的关键字参数
- **返回值**: func的返回值
- **异常**: 当重试次数用尽后仍然失败时,抛出最后一次的异常
### funcguard.tools
#### send_request(method, url, headers, data=None, return_type="json", timeout=60, auto_retry=None)
- **参数**:
- `method`: HTTP方法(GET, POST等)
- `url`: 请求URL
- `headers`: 请求头
- `data`: 请求数据,默认为None
- `return_type`: 返回类型,可选"json"、"response"或"text",默认为"json"
- `timeout`: 请求超时时间,单位为秒,默认为60
- `auto_retry`: 自动重试配置,格式为`{"task_name": "", "max_retries": 5, "execute_timeout": 90}`,默认为None
- **返回值**: 根据return_type参数返回不同格式的响应数据
- **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
### funcguard.time_utils
#### time_log(message, i=0, max_num=0, s_time=None, start_from=0)
- **参数**:
- `message`: 日志消息
- `i`: 当前进度,默认为0
- `max_num`: 总进度数量,默认为0
- `s_time`: 开始时间,用于计算预计完成时间,默认为None
- `start_from`: i是否从0开始,0表示从0开始,1表示从1开始,默认为0
- **返回值**: 无
- **功能**: 打印带时间戳的日志信息,支持进度显示和预计完成时间计算
#### time_diff(s_time=None, max_num=0, language="cn", return_duration=1)
- **参数**:
- `s_time`: 开始时间,默认为None
- `max_num`: 任务数量,默认为0
- `language`: 语言选择("cn"中文,其他为英文),默认为"cn"
- `return_duration`: 返回模式,默认为1:
- 0 - 仅返回 total_seconds,不打印信息
- 1 - 仅打印信息,不返回 total_seconds
- 2 - 打印信息,并返回 total_seconds
- **返回值**:
- 如果s_time为None则返回当前时间
- 如果return_duration为0或2则返回持续时间(秒)
- 否则返回None
- **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
#### time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)
- **参数**:
- `warning_threshold`: 警告阈值(秒),如果执行耗时超过此值则打印警告,默认为None
- `print_mode`: 打印模式,支持三种模式:
- 0 - 仅返回total_seconds,不打印任何信息
- 1 - 总是打印执行时间
- 2 - 仅在超时打印警告信息(默认)
- `func`: 要监控的函数
- `args`: 函数的位置参数
- `kwargs`: 函数的关键字参数
- **返回值**:
- print_mode == 0: 元组 (result, total_seconds) - 函数的执行结果和执行时间(秒)
- print_mode == 1: 函数的执行结果
- print_mode == 2: 函数的执行结果
- **功能**: 监控函数执行时间,并返回函数的执行结果和执行时间
- **注意**: 该方法内部使用 time_diff 函数,根据 print_mode 自动设置 return_duration 参数
- print_mode 为 0 或 2 时,设置 return_duration=0( time_diff 仅返回total_seconds,不打印信息)
- print_mode 为 1 时,设置 return_duration=2( time_diff 打印信息,并返回total_seconds)
### funcguard.printer
#### print_line(separator_char: str = "-", separator_length: int = 40) -> None
- **参数**:
- `separator_char`: 分隔符字符,默认为'-'
- `separator_length`: 分隔符长度,默认为40
- **返回值**: 无
- **功能**: 打印分隔线,用于分隔不同的打印块
#### print_title(title: str, separator_char: str = "=", padding_length: int = 3) -> None
- **参数**:
- `title`: 标题内容
- `separator_char`: 分隔符字符,默认为'='
- `padding_length`: 标题两侧的分隔符数量,默认为3
- **返回值**: 无
- **功能**: 打印带分隔符的标题,格式如:=== 初始化分类器 ===
#### print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None
- **参数**:
- `title`: 标题
- `content`: 打印的内容
- `separator_char`: 分隔符字符,默认为'-'
- `separator_length`: 分隔符长度,默认为40
- **返回值**: 无
- **功能**: 使用分隔符打印标题和内容,便于查看
## 许可证
MIT License
Raw data
{
"_id": null,
"home_page": "https://github.com/tinycen/funcguard",
"name": "funcguard",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "tinycen",
"author_email": "sky_ruocen@qq.com",
"download_url": "https://files.pythonhosted.org/packages/14/77/d26bf16c01d1eb1744b668e9684d92c4c09a8d6c8e8a7a35890234afa3d8/funcguard-0.2.0.tar.gz",
"platform": null,
"description": "# FuncGuard\n\nFuncGuard\u662f\u4e00\u4e2aPython\u5e93\uff0c\u63d0\u4f9b\u4e86\u51fd\u6570\u6267\u884c\u8d85\u65f6\u63a7\u5236\u548c\u91cd\u8bd5\u673a\u5236\u7684\u5b9e\u7528\u5de5\u5177\u3002\n\n## \u529f\u80fd\u7279\u70b9\n\n- \u51fd\u6570\u6267\u884c\u8d85\u65f6\u63a7\u5236\n- \u51fd\u6570\u6267\u884c\u5931\u8d25\u81ea\u52a8\u91cd\u8bd5\n- HTTP\u8bf7\u6c42\u5c01\u88c5\uff08\u652f\u6301\u81ea\u52a8\u91cd\u8bd5\uff09\n- \u683c\u5f0f\u5316\u6253\u5370\u5de5\u5177\uff08\u5206\u9694\u7ebf\u548c\u5757\u6253\u5370\uff09\n- \u65f6\u95f4\u65e5\u5fd7\u8bb0\u5f55\u548c\u8017\u65f6\u7edf\u8ba1\n- \u51fd\u6570\u6267\u884c\u65f6\u95f4\u76d1\u63a7\u548c\u8b66\u544a\n\n## \u5b89\u88c5/\u5347\u7ea7\n\n```bash\npip install --upgrade funcguard\n```\n\n\n## \u4f7f\u7528\u65b9\u6cd5\n\n### \u8d85\u65f6\u63a7\u5236\n\n\u4f7f\u7528`timeout_handler`\u51fd\u6570\u53ef\u4ee5\u63a7\u5236\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff0c\u9632\u6b62\u51fd\u6570\u8fd0\u884c\u65f6\u95f4\u8fc7\u957f\uff1a\n\n```python\nfrom funcguard import timeout_handler\n\ndef long_running_function():\n # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c\n import time\n time.sleep(10)\n return \"\u64cd\u4f5c\u5b8c\u6210\"\n\ntry:\n # \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a5\u79d2\n result = timeout_handler(long_running_function, execution_timeout=5)\n print(result)\nexcept TimeoutError as e:\n print(f\"\u6355\u83b7\u5230\u8d85\u65f6\u9519\u8bef: {e}\")\n```\n\n### \u91cd\u8bd5\u673a\u5236\n\n\u4f7f\u7528`retry_function`\u51fd\u6570\u53ef\u4ee5\u5728\u51fd\u6570\u6267\u884c\u5931\u8d25\u65f6\u81ea\u52a8\u91cd\u8bd5\uff1a\n\n```python\nfrom funcguard import retry_function\n\ndef unstable_function():\n # \u6a21\u62df\u4e00\u4e2a\u53ef\u80fd\u5931\u8d25\u7684\u64cd\u4f5c\n import random\n if random.random() < 0.7: # 70%\u7684\u6982\u7387\u5931\u8d25\n raise Exception(\"\u968f\u673a\u9519\u8bef\")\n return \"\u64cd\u4f5c\u6210\u529f\"\n\ntry:\n # \u6700\u591a\u91cd\u8bd53\u6b21\uff0c\u6bcf\u6b21\u6267\u884c\u8d85\u65f6\u65f6\u95f4\u4e3a10\u79d2\n result = retry_function(unstable_function, max_retries=3, execute_timeout=10, task_name=\"\u6d4b\u8bd5\u4efb\u52a1\")\n print(result)\nexcept Exception as e:\n print(f\"\u91cd\u8bd5\u540e\u4ecd\u7136\u5931\u8d25: {e}\")\n```\n\n### HTTP\u8bf7\u6c42\n\n\u4f7f\u7528`send_request`\u51fd\u6570\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u652f\u6301\u81ea\u52a8\u91cd\u8bd5\uff1a\n\n```python\nfrom funcguard import send_request\n\n# \u4e0d\u4f7f\u7528\u91cd\u8bd5\nresponse = send_request(\n method=\"GET\",\n url=\"https://api.example.com/data\",\n headers={\"Content-Type\": \"application/json\"},\n timeout=30\n)\nprint(response)\n\n# \u4f7f\u7528\u91cd\u8bd5\nresponse = send_request(\n method=\"POST\",\n url=\"https://api.example.com/data\",\n headers={\"Content-Type\": \"application/json\"},\n data={\"key\": \"value\"},\n timeout=30,\n auto_retry={\n \"task_name\": \"API\u8bf7\u6c42\",\n \"max_retries\": 3,\n \"execute_timeout\": 60\n }\n)\nprint(response)\n```\n\n### \u683c\u5f0f\u5316\u6253\u5370\n\n\u4f7f\u7528`print_line`\u3001`print_block`\u548c`print_title`\u51fd\u6570\u8fdb\u884c\u683c\u5f0f\u5316\u6253\u5370\uff0c\u4fbf\u4e8e\u67e5\u770b\u548c\u8c03\u8bd5\uff1a\n\n```python\nfrom funcguard import print_line, print_block, print_title\n\n# \u6253\u5370\u5e26\u7b49\u53f7\u7684\u6807\u9898\nprint_title(\"\u521d\u59cb\u5316\u5206\u7c7b\u5668\") # \u8f93\u51fa\uff1a=== \u521d\u59cb\u5316\u5206\u7c7b\u5668 ===\nprint_title(\"\u8bad\u7ec3\u5b8c\u6210\", separator_char=\"*\", padding_length=2) # \u8f93\u51fa\uff1a** \u8bad\u7ec3\u5b8c\u6210 **\n\n# \u6253\u5370\u5206\u9694\u7ebf\nprint_line() # \u9ed8\u8ba4\u4f7f\u752840\u4e2a'-'\u5b57\u7b26\nprint_line(\"*\", 30) # \u4f7f\u752830\u4e2a'*'\u5b57\u7b26\n\n# \u6253\u5370\u5757\u5185\u5bb9\nprint_block(\"\u7528\u6237\u4fe1\u606f\", {\"name\": \"\u5f20\u4e09\", \"age\": 25})\n\n# \u81ea\u5b9a\u4e49\u5206\u9694\u7b26\nprint_block(\"\u914d\u7f6e\u4fe1\u606f\", {\"debug\": True, \"port\": 8080}, \"=\", 50)\n\n# \u6253\u5370\u590d\u6742\u5185\u5bb9\nresult = {\n \"status\": \"success\",\n \"data\": [1, 2, 3, 4, 5],\n \"message\": \"\u64cd\u4f5c\u5b8c\u6210\"\n}\nprint_block(\"API\u54cd\u5e94\", result)\n```\n\n### \u65f6\u95f4\u65e5\u5fd7\u8bb0\u5f55\n\n\u4f7f\u7528`time_log`\u548c`time_diff`\u51fd\u6570\u8bb0\u5f55\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u548c\u7edf\u8ba1\u4fe1\u606f\uff1a\n\n```python\nfrom funcguard import time_log, time_diff\n\n# \u83b7\u53d6\u5f00\u59cb\u65f6\u95f4\nstart_time = time_diff()\n\n# \u8bb0\u5f55\u4efb\u52a1\u5f00\u59cb\uff08i\u4ece0\u5f00\u59cb\uff09\ntime_log(\"\u5f00\u59cb\u5904\u7406\u6570\u636e\", 0, 100, start_time, 0)\n\n# \u6a21\u62df\u5904\u7406\u8fc7\u7a0b\nimport time\nfor i in range(1, 101):\n time.sleep(0.1) # \u6a21\u62df\u5904\u7406\u65f6\u95f4\n if i % 20 == 0:\n time_log(f\"\u5904\u7406\u8fdb\u5ea6\", i, 100, start_time, 0) # \u663e\u793a\u8fdb\u5ea6\u548c\u9884\u8ba1\u5b8c\u6210\u65f6\u95f4\n\n# \u8bb0\u5f55\u4efb\u52a1\u5b8c\u6210\u5e76\u6253\u5370\u7edf\u8ba1\u4fe1\u606f\ntime_diff(start_time, 100, \"cn\") # \u4e2d\u6587\u663e\u793a\u7edf\u8ba1\u4fe1\u606f\n```\n\n\u6216\u8005\u5f53i\u4ece1\u5f00\u59cb\u65f6\uff1a\n\n```python\n\n# \u8bb0\u5f55\u4efb\u52a1\u5f00\u59cb\uff08i\u4ece1\u5f00\u59cb\uff09\ntime_log(\"\u5f00\u59cb\u5904\u7406\u6570\u636e\", 1, 100, start_time, 1)\n\n# \u6a21\u62df\u5904\u7406\u8fc7\u7a0b\nimport time\nfor i in range(1, 101):\n time.sleep(0.1) # \u6a21\u62df\u5904\u7406\u65f6\u95f4\n if i % 20 == 0:\n time_log(f\"\u5904\u7406\u8fdb\u5ea6\", i, 100, start_time, 1) # \u663e\u793a\u8fdb\u5ea6\u548c\u9884\u8ba1\u5b8c\u6210\u65f6\u95f4\n\n```\n\n### \u6267\u884c\u65f6\u95f4\u76d1\u63a7\n\n\u4f7f\u7528`time_monitor`\u51fd\u6570\u76d1\u63a7\u51fd\u6570\u6267\u884c\u65f6\u95f4\uff1a\n\n```python\nfrom funcguard import time_monitor\n\ndef some_function():\n # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c\n import time\n time.sleep(2)\n return \"\u64cd\u4f5c\u5b8c\u6210\"\n\n# \u6a21\u5f0f1\uff1a\u603b\u662f\u6253\u5370\u6267\u884c\u65f6\u95f4\nresult = time_monitor(\n func=some_function,\n print_mode=1\n)\nprint(f\"\u7ed3\u679c: {result}\")\n\n# \u6a21\u5f0f2\uff1a\u4ec5\u5728\u8d85\u8fc7\u9608\u503c\u65f6\u6253\u5370\u8b66\u544a\nresult = time_monitor(\n func=some_function,\n warning_threshold=1.5, # \u8bbe\u7f6e1.5\u79d2\u7684\u8b66\u544a\u9608\u503c\n print_mode=2\n)\nprint(f\"\u7ed3\u679c: {result}\")\n\n# \u6a21\u5f0f0\uff1a\u4e0d\u6253\u5370\u4efb\u4f55\u4fe1\u606f\uff0c\u4ec5\u8fd4\u56de\u7ed3\u679c\u548c\u6267\u884c\u65f6\u95f4\nresult, duration = time_monitor(\n func=some_function,\n print_mode=0\n)\nprint(f\"\u7ed3\u679c: {result}, \u8017\u65f6: {duration}\u79d2\")\n```\n\n\u65f6\u95f4\u65e5\u5fd7\u529f\u80fd\u7279\u70b9\uff1a\n- \u81ea\u52a8\u663e\u793a\u5317\u4eac\u65f6\u95f4\uff08UTC+8\uff09\n- \u652f\u6301\u8fdb\u5ea6\u663e\u793a\u548c\u9884\u8ba1\u5b8c\u6210\u65f6\u95f4\u8ba1\u7b97\n- \u63d0\u4f9b\u4e2d\u82f1\u6587\u53cc\u8bed\u7edf\u8ba1\u4fe1\u606f\n- \u53ef\u663e\u793a\u603b\u8017\u65f6\u3001\u5e73\u5747\u8017\u65f6\u7b49\u8be6\u7ec6\u7edf\u8ba1\n- \u652f\u6301i\u4ece0\u6216\u4ece1\u5f00\u59cb\u7684\u8ba1\u6570\u65b9\u5f0f\n- \u652f\u6301\u51fd\u6570\u6267\u884c\u65f6\u95f4\u76d1\u63a7\u548c\u8b66\u544a\n\n## API\u6587\u6863\n\n### funcguard.core\n\n#### timeout_handler(func, args=(), kwargs=None, execution_timeout=90)\n\n- **\u53c2\u6570**:\n - `func`: \u9700\u8981\u6267\u884c\u7684\u76ee\u6807\u51fd\u6570\n - `args`: \u76ee\u6807\u51fd\u6570\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u5143\u7ec4\n - `kwargs`: \u76ee\u6807\u51fd\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3aNone\n - `execution_timeout`: \u51fd\u6570\u6267\u884c\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0c\u9ed8\u8ba4\u4e3a90\u79d2\n- **\u8fd4\u56de\u503c**: \u76ee\u6807\u51fd\u6570\u7684\u8fd4\u56de\u503c\n- **\u5f02\u5e38**: `TimeoutError` - \u5f53\u51fd\u6570\u6267\u884c\u8d85\u8fc7\u6307\u5b9a\u65f6\u95f4\u65f6\u629b\u51fa\n\n#### retry_function(func, max_retries=5, execute_timeout=90, task_name=\"\", *args, **kwargs)\n\n- **\u53c2\u6570**:\n - `func`: \u9700\u8981\u91cd\u8bd5\u7684\u51fd\u6570\n - `max_retries`: \u6700\u5927\u91cd\u8bd5\u6b21\u6570\uff0c\u9ed8\u8ba4\u4e3a5\n - `execute_timeout`: \u6267\u884c\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a90\u79d2\n - `task_name`: \u4efb\u52a1\u540d\u79f0\uff0c\u7528\u4e8e\u6253\u5370\u65e5\u5fd7\n - `args`: func\u7684\u4f4d\u7f6e\u53c2\u6570\n - `kwargs`: func\u7684\u5173\u952e\u5b57\u53c2\u6570\n- **\u8fd4\u56de\u503c**: func\u7684\u8fd4\u56de\u503c\n- **\u5f02\u5e38**: \u5f53\u91cd\u8bd5\u6b21\u6570\u7528\u5c3d\u540e\u4ecd\u7136\u5931\u8d25\u65f6\uff0c\u629b\u51fa\u6700\u540e\u4e00\u6b21\u7684\u5f02\u5e38\n\n### funcguard.tools\n\n#### send_request(method, url, headers, data=None, return_type=\"json\", timeout=60, auto_retry=None)\n\n- **\u53c2\u6570**:\n - `method`: HTTP\u65b9\u6cd5\uff08GET, POST\u7b49\uff09\n - `url`: \u8bf7\u6c42URL\n - `headers`: \u8bf7\u6c42\u5934\n - `data`: \u8bf7\u6c42\u6570\u636e\uff0c\u9ed8\u8ba4\u4e3aNone\n - `return_type`: \u8fd4\u56de\u7c7b\u578b\uff0c\u53ef\u9009\"json\"\u3001\"response\"\u6216\"text\"\uff0c\u9ed8\u8ba4\u4e3a\"json\"\n - `timeout`: \u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0c\u9ed8\u8ba4\u4e3a60\n - `auto_retry`: \u81ea\u52a8\u91cd\u8bd5\u914d\u7f6e\uff0c\u683c\u5f0f\u4e3a`{\"task_name\": \"\", \"max_retries\": 5, \"execute_timeout\": 90}`\uff0c\u9ed8\u8ba4\u4e3aNone\n- **\u8fd4\u56de\u503c**: \u6839\u636ereturn_type\u53c2\u6570\u8fd4\u56de\u4e0d\u540c\u683c\u5f0f\u7684\u54cd\u5e94\u6570\u636e\n- **\u5f02\u5e38**: \u5f53\u8bf7\u6c42\u5931\u8d25\u4e14\u91cd\u8bd5\u6b21\u6570\u7528\u5c3d\u540e\uff0c\u629b\u51fa\u76f8\u5e94\u7684\u5f02\u5e38\n\n### funcguard.time_utils\n\n#### time_log(message, i=0, max_num=0, s_time=None, start_from=0)\n\n- **\u53c2\u6570**:\n - `message`: \u65e5\u5fd7\u6d88\u606f\n - `i`: \u5f53\u524d\u8fdb\u5ea6\uff0c\u9ed8\u8ba4\u4e3a0\n - `max_num`: \u603b\u8fdb\u5ea6\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a0\n - `s_time`: \u5f00\u59cb\u65f6\u95f4\uff0c\u7528\u4e8e\u8ba1\u7b97\u9884\u8ba1\u5b8c\u6210\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3aNone\n - `start_from`: i\u662f\u5426\u4ece0\u5f00\u59cb\uff0c0\u8868\u793a\u4ece0\u5f00\u59cb\uff0c1\u8868\u793a\u4ece1\u5f00\u59cb\uff0c\u9ed8\u8ba4\u4e3a0\n- **\u8fd4\u56de\u503c**: \u65e0\n- **\u529f\u80fd**: \u6253\u5370\u5e26\u65f6\u95f4\u6233\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u652f\u6301\u8fdb\u5ea6\u663e\u793a\u548c\u9884\u8ba1\u5b8c\u6210\u65f6\u95f4\u8ba1\u7b97\n\n#### time_diff(s_time=None, max_num=0, language=\"cn\", return_duration=1)\n\n- **\u53c2\u6570**:\n - `s_time`: \u5f00\u59cb\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3aNone\n - `max_num`: \u4efb\u52a1\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a0\n - `language`: \u8bed\u8a00\u9009\u62e9\uff08\"cn\"\u4e2d\u6587\uff0c\u5176\u4ed6\u4e3a\u82f1\u6587\uff09\uff0c\u9ed8\u8ba4\u4e3a\"cn\"\n - `return_duration`: \u8fd4\u56de\u6a21\u5f0f\uff0c\u9ed8\u8ba4\u4e3a1:\n - 0 - \u4ec5\u8fd4\u56de total_seconds\uff0c\u4e0d\u6253\u5370\u4fe1\u606f\n - 1 - \u4ec5\u6253\u5370\u4fe1\u606f,\u4e0d\u8fd4\u56de total_seconds\n - 2 - \u6253\u5370\u4fe1\u606f\uff0c\u5e76\u8fd4\u56de total_seconds\n- **\u8fd4\u56de\u503c**: \n - \u5982\u679cs_time\u4e3aNone\u5219\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\n - \u5982\u679creturn_duration\u4e3a0\u62162\u5219\u8fd4\u56de\u6301\u7eed\u65f6\u95f4\uff08\u79d2\uff09\n - \u5426\u5219\u8fd4\u56deNone\n- **\u529f\u80fd**: \u8ba1\u7b97\u5e76\u6253\u5370\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u7edf\u8ba1\u4fe1\u606f\uff0c\u652f\u6301\u4e2d\u82f1\u6587\u53cc\u8bed\u8f93\u51fa\n\n#### time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)\n\n- **\u53c2\u6570**:\n - `warning_threshold`: \u8b66\u544a\u9608\u503c\uff08\u79d2\uff09\uff0c\u5982\u679c\u6267\u884c\u8017\u65f6\u8d85\u8fc7\u6b64\u503c\u5219\u6253\u5370\u8b66\u544a\uff0c\u9ed8\u8ba4\u4e3aNone\n - `print_mode`: \u6253\u5370\u6a21\u5f0f\uff0c\u652f\u6301\u4e09\u79cd\u6a21\u5f0f:\n - 0 - \u4ec5\u8fd4\u56detotal_seconds\uff0c\u4e0d\u6253\u5370\u4efb\u4f55\u4fe1\u606f\n - 1 - \u603b\u662f\u6253\u5370\u6267\u884c\u65f6\u95f4\n - 2 - \u4ec5\u5728\u8d85\u65f6\u6253\u5370\u8b66\u544a\u4fe1\u606f\uff08\u9ed8\u8ba4\uff09\n - `func`: \u8981\u76d1\u63a7\u7684\u51fd\u6570\n - `args`: \u51fd\u6570\u7684\u4f4d\u7f6e\u53c2\u6570\n - `kwargs`: \u51fd\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570\n- **\u8fd4\u56de\u503c**: \n - print_mode == 0: \u5143\u7ec4 (result, total_seconds) - \u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u548c\u6267\u884c\u65f6\u95f4\uff08\u79d2\uff09\n - print_mode == 1: \u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\n - print_mode == 2: \u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\n- **\u529f\u80fd**: \u76d1\u63a7\u51fd\u6570\u6267\u884c\u65f6\u95f4\uff0c\u5e76\u8fd4\u56de\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u548c\u6267\u884c\u65f6\u95f4\n- **\u6ce8\u610f**: \u8be5\u65b9\u6cd5\u5185\u90e8\u4f7f\u7528 time_diff \u51fd\u6570\uff0c\u6839\u636e print_mode \u81ea\u52a8\u8bbe\u7f6e return_duration \u53c2\u6570\n - print_mode \u4e3a 0 \u6216 2 \u65f6\uff0c\u8bbe\u7f6e return_duration=0\uff08 time_diff \u4ec5\u8fd4\u56detotal_seconds\uff0c\u4e0d\u6253\u5370\u4fe1\u606f\uff09\n - print_mode \u4e3a 1 \u65f6\uff0c\u8bbe\u7f6e return_duration=2\uff08 time_diff \u6253\u5370\u4fe1\u606f\uff0c\u5e76\u8fd4\u56detotal_seconds\uff09\n\n### funcguard.printer\n\n#### print_line(separator_char: str = \"-\", separator_length: int = 40) -> None\n\n- **\u53c2\u6570**:\n - `separator_char`: \u5206\u9694\u7b26\u5b57\u7b26\uff0c\u9ed8\u8ba4\u4e3a'-'\n - `separator_length`: \u5206\u9694\u7b26\u957f\u5ea6\uff0c\u9ed8\u8ba4\u4e3a40\n- **\u8fd4\u56de\u503c**: \u65e0\n- **\u529f\u80fd**: \u6253\u5370\u5206\u9694\u7ebf\uff0c\u7528\u4e8e\u5206\u9694\u4e0d\u540c\u7684\u6253\u5370\u5757\n\n#### print_title(title: str, separator_char: str = \"=\", padding_length: int = 3) -> None\n\n- **\u53c2\u6570**:\n - `title`: \u6807\u9898\u5185\u5bb9\n - `separator_char`: \u5206\u9694\u7b26\u5b57\u7b26\uff0c\u9ed8\u8ba4\u4e3a'='\n - `padding_length`: \u6807\u9898\u4e24\u4fa7\u7684\u5206\u9694\u7b26\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a3\n- **\u8fd4\u56de\u503c**: \u65e0\n- **\u529f\u80fd**: \u6253\u5370\u5e26\u5206\u9694\u7b26\u7684\u6807\u9898\uff0c\u683c\u5f0f\u5982\uff1a=== \u521d\u59cb\u5316\u5206\u7c7b\u5668 ===\n\n#### print_block(title: str, content: Any, separator_char: str = \"-\", separator_length: int = 40) -> None\n\n- **\u53c2\u6570**:\n - `title`: \u6807\u9898\n - `content`: \u6253\u5370\u7684\u5185\u5bb9\n - `separator_char`: \u5206\u9694\u7b26\u5b57\u7b26\uff0c\u9ed8\u8ba4\u4e3a'-'\n - `separator_length`: \u5206\u9694\u7b26\u957f\u5ea6\uff0c\u9ed8\u8ba4\u4e3a40\n- **\u8fd4\u56de\u503c**: \u65e0\n- **\u529f\u80fd**: \u4f7f\u7528\u5206\u9694\u7b26\u6253\u5370\u6807\u9898\u548c\u5185\u5bb9\uff0c\u4fbf\u4e8e\u67e5\u770b\n\n## \u8bb8\u53ef\u8bc1\n\nMIT License\n",
"bugtrack_url": null,
"license": null,
"summary": "FuncGuard\u662f\u4e00\u4e2aPython\u5e93\uff0c\u63d0\u4f9b\u51fd\u6570\u6267\u884c\u8d85\u65f6\u63a7\u5236\u3001\u91cd\u8bd5\u673a\u5236\u3001HTTP\u8bf7\u6c42\u5c01\u88c5\u548c\u683c\u5f0f\u5316\u6253\u5370\u5de5\u5177\u3002",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/tinycen/funcguard"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "62dbdee2a842ee73e007ca3d839f3908b0999faa67f343b5bb2ad7e0b048110c",
"md5": "623391097e1745d9b91c1fdea18ad287",
"sha256": "06703dc7c814abbca712a0972f611621acd974bb3b2534ee9b1eb2f033465f3f"
},
"downloads": -1,
"filename": "funcguard-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "623391097e1745d9b91c1fdea18ad287",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 12251,
"upload_time": "2025-10-30T07:47:55",
"upload_time_iso_8601": "2025-10-30T07:47:55.272374Z",
"url": "https://files.pythonhosted.org/packages/62/db/dee2a842ee73e007ca3d839f3908b0999faa67f343b5bb2ad7e0b048110c/funcguard-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "1477d26bf16c01d1eb1744b668e9684d92c4c09a8d6c8e8a7a35890234afa3d8",
"md5": "15852370306863c399e0a9264942b281",
"sha256": "aee4e515c1db62877dd0ee4140bebb3cf3e143c978b56cf3a964b15078d9e0dc"
},
"downloads": -1,
"filename": "funcguard-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "15852370306863c399e0a9264942b281",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12736,
"upload_time": "2025-10-30T07:47:56",
"upload_time_iso_8601": "2025-10-30T07:47:56.046683Z",
"url": "https://files.pythonhosted.org/packages/14/77/d26bf16c01d1eb1744b668e9684d92c4c09a8d6c8e8a7a35890234afa3d8/funcguard-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-30 07:47:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tinycen",
"github_project": "funcguard",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "requests",
"specs": []
}
],
"lcname": "funcguard"
}