funcguard 0.1.8__py3-none-any.whl → 0.2.0__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 CHANGED
@@ -1,7 +1,7 @@
1
1
  from .core import timeout_handler, retry_function
2
2
  from .tools import send_request
3
- from .time_utils import time_log, time_diff, monitor_execution_time
4
- from .printer import print_block, print_line, print_title
3
+ from .time_utils import time_log, time_diff, time_monitor
4
+ from .printer import print_block, print_line, print_title, print_progress
5
5
 
6
6
  __author__ = "ruocen"
7
7
 
@@ -12,8 +12,9 @@ __all__ = [
12
12
  "send_request",
13
13
  "time_log",
14
14
  "time_diff",
15
- "monitor_execution_time",
15
+ "time_monitor",
16
16
  "print_block",
17
17
  "print_line",
18
18
  "print_title",
19
+ "print_progress",
19
20
  ]
funcguard/printer.py CHANGED
@@ -1,5 +1,32 @@
1
1
  from typing import Any
2
2
 
3
+ # 打印进度条
4
+ def print_progress(idx: int, total: int, message: str = "") -> None:
5
+ """
6
+ 打印进度条,显示当前进度
7
+
8
+ 设计原理:
9
+ - 进度条总长度固定为50个字符,这是终端显示的最佳长度
10
+ - 使用整除2(//2)将0-100%映射到0-50字符,确保平滑过渡
11
+ - 已完成部分用'█'表示,未完成部分用'-'表示
12
+
13
+ :param idx: 当前索引(从0开始)
14
+ :param total: 总数量
15
+ :param message: 额外消息,默认为空字符串
16
+ """
17
+ # 计算当前进度百分比(0-100)
18
+ percent = int(idx / total * 100)
19
+
20
+ # 构建进度条:
21
+ bar = '█' * (percent // 2) + '-' * (50 - percent // 2)
22
+
23
+ # 打印进度条:
24
+ # - \r:回车符,回到行首覆盖之前的内容
25
+ # - end='':不换行,保持在同一行更新
26
+ # - flush=True:立即刷新输出,确保实时显示
27
+ print(f"\r进度: |{bar}| {percent}% ({idx}/{total}) {message}", end='', flush=True)
28
+
29
+
3
30
  # 打印带等号的标题(如:=== 初始化分类器 ===)
4
31
  def print_title(title: str, separator_char: str = "=", padding_length: int = 3) -> None:
5
32
  """
funcguard/time_utils.py CHANGED
@@ -2,11 +2,10 @@
2
2
  时间工具模块,提供时间计算、日志记录和执行时间监控功能
3
3
  """
4
4
  from datetime import datetime, timezone, timedelta
5
- from typing import Optional, Union
6
5
 
7
6
 
8
7
  # 打印时间
9
- def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
8
+ def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 , return_field = "progress_info") :
10
9
  """
11
10
  打印带时间戳的日志信息,支持进度显示和预计完成时间
12
11
 
@@ -15,17 +14,20 @@ def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
15
14
  :param max_num: 总进度数量
16
15
  :param s_time: 开始时间,用于计算预计完成时间
17
16
  :param start_from: i是否从0开始,0表示从0开始,1表示从1开始
18
- :return: None
17
+ :param return_field: 返回字段,支持以下:
18
+ "progress_info" 表示完整进度信息,"remaining_time" 表示剩余时间,"end_time" 表示预计完成时间
19
+ :return: 根据 return_field 参数返回不同的信息
19
20
  """
20
21
  now = datetime.now( timezone( timedelta( hours = 8 ) ) )
21
- time_log = "{:02d}:{:02d}:{:02d}".format( now.hour, now.minute, now.second )
22
+ time_str = "{:02d}:{:02d}:{:02d}".format( now.hour, now.minute, now.second )
23
+ progress_info = ""
22
24
  if i < 2 or max_num < 2 :
23
- print( time_log + " " + message )
25
+ print( time_str + " " + message )
24
26
 
25
27
  else :
26
28
  # 根据start_from参数计算实际处理的项目数
27
29
  process_item = i + 1 if start_from == 0 else i
28
- text = "{}/{}".format( process_item, max_num )
30
+ progress_info = "{}/{}".format( process_item, max_num )
29
31
  # 检查是否应该显示预计完成时间和剩余时间
30
32
  if process_item % 10 == 0 and s_time is not None and process_item < max_num :
31
33
  duration = now - s_time
@@ -35,9 +37,19 @@ def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
35
37
  end_time = now + time_left
36
38
  end_time_str = end_time.strftime( "%Y-%m-%d %H:%M" )
37
39
  remaining_time_str = str( timedelta( seconds = int( time_left.total_seconds() ) ) )
38
- text = text + "({})etr {}".format( end_time_str, remaining_time_str )
39
- print( time_log + " " + message + " " + text )
40
- return
40
+
41
+ # Estimated Time of Arrival(预计完成/到达时间)
42
+ if return_field == "end_time" :
43
+ return f"eta {end_time_str}"
44
+
45
+ # Estimated Time Remaining(预计剩余时间)
46
+ elif return_field == "remaining_time" :
47
+ return f"etr {remaining_time_str}"
48
+
49
+ progress_info = progress_info + "({})etr {}".format( end_time_str, remaining_time_str )
50
+
51
+ print( time_str + " " + message + " " + progress_info )
52
+ return progress_info
41
53
 
42
54
 
43
55
  # 计算持续时间
@@ -97,7 +109,7 @@ def time_diff(s_time = None, max_num = 0, language = "cn", return_duration = 1)
97
109
 
98
110
 
99
111
  # 监控程序的执行时间
100
- def monitor_execution_time(warning_threshold=None, print_mode=2, func=None, *args, **kwargs):
112
+ def time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs):
101
113
  """
102
114
  监控函数执行时间,并返回函数的执行结果和执行时间
103
115
 
funcguard/tools.py CHANGED
@@ -1,9 +1,7 @@
1
1
  import json
2
2
  import requests
3
- from datetime import datetime, timezone, timedelta
4
3
  from typing import Optional, Dict, Any, Union
5
4
  from .core import retry_function
6
- from .time_utils import time_log, time_diff, monitor_execution_time
7
5
 
8
6
 
9
7
  # 发起请求
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: funcguard
3
- Version: 0.1.8
3
+ Version: 0.2.0
4
4
  Summary: FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。
5
5
  Home-page: https://github.com/tinycen/funcguard
6
6
  Author: tinycen
@@ -182,10 +182,10 @@ for i in range(1, 101):
182
182
 
183
183
  ### 执行时间监控
184
184
 
185
- 使用`monitor_execution_time`函数监控函数执行时间:
185
+ 使用`time_monitor`函数监控函数执行时间:
186
186
 
187
187
  ```python
188
- from funcguard import monitor_execution_time
188
+ from funcguard import time_monitor
189
189
 
190
190
  def some_function():
191
191
  # 模拟一个耗时操作
@@ -194,14 +194,14 @@ def some_function():
194
194
  return "操作完成"
195
195
 
196
196
  # 模式1:总是打印执行时间
197
- result = monitor_execution_time(
197
+ result = time_monitor(
198
198
  func=some_function,
199
199
  print_mode=1
200
200
  )
201
201
  print(f"结果: {result}")
202
202
 
203
203
  # 模式2:仅在超过阈值时打印警告
204
- result = monitor_execution_time(
204
+ result = time_monitor(
205
205
  func=some_function,
206
206
  warning_threshold=1.5, # 设置1.5秒的警告阈值
207
207
  print_mode=2
@@ -209,7 +209,7 @@ result = monitor_execution_time(
209
209
  print(f"结果: {result}")
210
210
 
211
211
  # 模式0:不打印任何信息,仅返回结果和执行时间
212
- result, duration = monitor_execution_time(
212
+ result, duration = time_monitor(
213
213
  func=some_function,
214
214
  print_mode=0
215
215
  )
@@ -294,7 +294,7 @@ print(f"结果: {result}, 耗时: {duration}秒")
294
294
  - 否则返回None
295
295
  - **功能**: 计算并打印任务执行时间统计信息,支持中英文双语输出
296
296
 
297
- #### monitor_execution_time(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)
297
+ #### time_monitor(warning_threshold=None, print_mode=2, func=None, *args, **kwargs)
298
298
 
299
299
  - **参数**:
300
300
  - `warning_threshold`: 警告阈值(秒),如果执行耗时超过此值则打印警告,默认为None
@@ -0,0 +1,11 @@
1
+ funcguard/__init__.py,sha256=XdbsQ5p2wG9YK9CoI7xWc6UEzFuuOuR_r2dov6D_oiE,469
2
+ funcguard/core.py,sha256=-rFRkE-udxM0wxlcv9Qi_yIQBRdVrGgwza-LYQsVRLg,3632
3
+ funcguard/printer.py,sha256=zjCPPkBrz_D1z8563Vi-raxEti5kLSwahSoqYuP20eg,2455
4
+ funcguard/time_utils.py,sha256=XVk95wxyfwnAAacGpbomoRGznGzpRVwMiSMs5L-ncJU,6758
5
+ funcguard/tools.py,sha256=pw6QCi8BAOTkHKhG3sxqfU3pYzST08YjxTiPvK-SWnU,1972
6
+ tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
7
+ funcguard-0.2.0.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
8
+ funcguard-0.2.0.dist-info/METADATA,sha256=KkUoSZ7dYfAoVKIPFJSpsMB8Vqzej6LZTHB_i8IiefQ,10598
9
+ funcguard-0.2.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
10
+ funcguard-0.2.0.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
11
+ funcguard-0.2.0.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- funcguard/__init__.py,sha256=irZLaE4wDu4bRilk0ySHy7ZTZD0-CGWaogrC7MCKM3k,451
2
- funcguard/core.py,sha256=-rFRkE-udxM0wxlcv9Qi_yIQBRdVrGgwza-LYQsVRLg,3632
3
- funcguard/printer.py,sha256=Iig_UO4h5LcL0BqCEEdxiu32h4rUq9GJbmf1pC_hd7o,1462
4
- funcguard/time_utils.py,sha256=be7l6vMivWAfmLP4V91z9jRUYfnn-T4XrCU21eQkXeo,6139
5
- funcguard/tools.py,sha256=J25wE1JCRqfDnJn7NJGaS9nepATPxNJT4ko4ITkghQA,2091
6
- tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
7
- funcguard-0.1.8.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
8
- funcguard-0.1.8.dist-info/METADATA,sha256=l7J2kuqmcjPUPnyCDt10leuNvwGJpLrSiMSM3sbxzZc,10658
9
- funcguard-0.1.8.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
10
- funcguard-0.1.8.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
11
- funcguard-0.1.8.dist-info/RECORD,,