funcguard 0.1.9__py3-none-any.whl → 0.2.1__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
3
  from .time_utils import time_log, time_diff, time_monitor
4
- from .printer import print_block, print_line, print_title
4
+ from .printer import print_block, print_line, print_title, print_progress
5
5
 
6
6
  __author__ = "ruocen"
7
7
 
@@ -16,4 +16,5 @@ __all__ = [
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,19 +14,29 @@ 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 = eta_time_info = etr_time_info = ""
22
24
  if i < 2 or max_num < 2 :
23
- print( time_log + " " + message )
25
+ if return_field in ["end_time", "remaining_time"] :
26
+ return ""
27
+ else:
28
+ print( time_str + " " + message )
24
29
 
25
30
  else :
26
31
  # 根据start_from参数计算实际处理的项目数
27
32
  process_item = i + 1 if start_from == 0 else i
28
- text = "{}/{}".format( process_item, max_num )
33
+ progress_info = "{}/{}".format( process_item, max_num )
29
34
  # 检查是否应该显示预计完成时间和剩余时间
30
- if process_item % 10 == 0 and s_time is not None and process_item < max_num :
35
+ # 当return_field为"end_time"或"remaining_time"时,每次都计算时间
36
+ # 否则只在每10个处理一次时计算
37
+ should_calculate_time = (return_field in ["end_time", "remaining_time"] and process_item < max_num ) or (process_item % 10 == 0 and s_time is not None and process_item < max_num)
38
+
39
+ if should_calculate_time and s_time is not None and process_item < max_num:
31
40
  duration = now - s_time
32
41
  ev_duration = duration / process_item # 每项平均耗时
33
42
  remaining_items = max_num - process_item
@@ -35,9 +44,20 @@ def time_log(message, i = 0, max_num = 0, s_time = None, start_from = 0 ) :
35
44
  end_time = now + time_left
36
45
  end_time_str = end_time.strftime( "%Y-%m-%d %H:%M" )
37
46
  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
47
+ eta_time_info = f"eta {end_time_str}"
48
+ etr_time_info = f"etr {remaining_time_str}"
49
+ progress_info = progress_info + f" ( {eta_time_info} {etr_time_info} )"
50
+
51
+ # Estimated Time of Arrival(预计完成/到达时间)
52
+ if return_field == "end_time" :
53
+ return eta_time_info
54
+
55
+ # Estimated Time Remaining(预计剩余时间)
56
+ elif return_field == "remaining_time" :
57
+ return etr_time_info
58
+
59
+ print( time_str + " " + message + " " + progress_info )
60
+ return progress_info
41
61
 
42
62
 
43
63
  # 计算持续时间
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, time_monitor
7
5
 
8
6
 
9
7
  # 发起请求
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: funcguard
3
- Version: 0.1.9
3
+ Version: 0.2.1
4
4
  Summary: FuncGuard是一个Python库,提供函数执行超时控制、重试机制、HTTP请求封装和格式化打印工具。
5
5
  Home-page: https://github.com/tinycen/funcguard
6
6
  Author: tinycen
@@ -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=kFwrrLVHIi_UYmSxRzXLmgGUM_hURJ-xoCPa_Set3z4,7268
5
+ funcguard/tools.py,sha256=pw6QCi8BAOTkHKhG3sxqfU3pYzST08YjxTiPvK-SWnU,1972
6
+ tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
7
+ funcguard-0.2.1.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
8
+ funcguard-0.2.1.dist-info/METADATA,sha256=fKt8L1Yj9v-CncSW_8GZ-CdI89WY-qj2aH21X1yiuGY,10598
9
+ funcguard-0.2.1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
10
+ funcguard-0.2.1.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
11
+ funcguard-0.2.1.dist-info/RECORD,,
@@ -1,11 +0,0 @@
1
- funcguard/__init__.py,sha256=Tr5d54OPz1tK9nBQKjiYlSKAR0nmqsU4HpRrkRoRS3k,431
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=AZ3rPJkepnFL3Z66h0miql8-Elox0knn7q1RbqMrpsc,6129
5
- funcguard/tools.py,sha256=zocd9rMP8fzEPLoU4yfRrTKcX57qF10fFYk38lyhif8,2081
6
- tests/__init__.py,sha256=VW6FdWdSC_PL3zEtpkRQfUMf6yVD2OfwtSds82jawTs,26
7
- funcguard-0.1.9.dist-info/LICENSE,sha256=jgOquECfjiXp5xXQ2zuzItDr4XDBLan-bIzIXl1lS4Y,1064
8
- funcguard-0.1.9.dist-info/METADATA,sha256=YpiwpYYF3e2qBz9MffaN8WavVdH2Y_lpcbeNNt8uQ5M,10598
9
- funcguard-0.1.9.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
10
- funcguard-0.1.9.dist-info/top_level.txt,sha256=7wL9mWT062DttKNO7Wi1wYWTZilR2AOPRO0rE3gvtB4,16
11
- funcguard-0.1.9.dist-info/RECORD,,