TQDM进度条助手,用于日志记录和其他无头应用程序
项目描述
tqdm-loggable
tqdm-loggable
是一个小巧的Python包,提供日志友好的TQDM进度条。
如果你的Python应用程序有 tqdm 进度条,并在像以下这样的非交互式会话中使用它们...
...您不能拥有交互式进度条。当您使用监控工具观察应用程序时,通常在应用程序有tqdm
进度进行时,您不会看到任何活动。如果进度条操作需要几分钟,则您的应用程序可能看起来已冻结。这可以通过tqdm-logging
来解决,它会通过向文件和日志监控工具等日志后端发送常规报告来报告进度。
在这些情况下,tqdm-loggable
会自动将您的tqdm
进度条转换为可在无头系统中读取的可记录进度消息。
tqdm-loggable
...
- 是正常
tqdm
的替代品 - 除非检测到非交互式会话,否则不会发生变化 - 与
tqdm.auto
和Jupyter Notebooks中的基于HTML的进度条兼容 - 使用Python logging子系统来报告状态而不是终端
- 每X秒打印一条日志行
- 日志消息是结构化的,因此它们可以与Sentry、LogStash等高级日志服务一起工作,这些服务提供基于变量的高级搜索和标记
- 特别支持Github Actions和其他持续集成环境
以下是在纯文本日志中输出tqdm
日志消息的示例
tqdm_logging.py :139 2022-09-21 12:13:44,138 Progress on:Progress bar without total -/- rate:- remaining:? elapsed:00:00 postfix:-
tqdm_logging.py :139 2022-09-21 12:13:46,225 Progress on:Progress bar without total 10000/- rate:- remaining:? elapsed:00:02 postfix:-
tqdm_logging.py :139 2022-09-21 12:13:46,225 Progress on:Sample progress -/60000 rate:- remaining:? elapsed:00:00 postfix:-
tqdm_logging.py :139 2022-09-21 12:13:56,307 Progress on:Sample progress 21.0kit/60.0kit rate:1,982.9it/s remaining:00:19 elapsed:00:10 postfix:Current time=2022-09-21 10:13:55.801787
tqdm_logging.py :139 2022-09-21 12:14:06,392 Progress on:Sample progress 41.0kit/60.0kit rate:1,984.1it/s remaining:00:09 elapsed:00:20 postfix:Current time=2022-09-21 10:14:05.890220
请注意,tqdm-loggable
与tqdm.contrib.logging不同,它采用不同的方法来解决不同的问题。
安装
包名为tqdm-loggable.
有关如何在您的系统上安装包的说明,请阅读Python打包手册。
用法
您需要做的唯一事情
- 确保您的Python日志系统配置正确
- 将导入从
from tqdm.auto import tqdm
更改为from tqdm_loggable.auto import tqdm
- 可选:在应用程序初始化时调用
tqdm_logging.set_level()
- 可选:在应用程序初始化时调用
tqdm_logging.set_log_rate()
Python代码库的搜索和替换说明
from tqdm import tqdm -> from tqdm_loggable.auto import tqdm
from tqdm.auto import tqdm -> from tqdm_loggable.auto import tqdm
以下是一个示例脚本
import datetime
import logging
import time
from tqdm_loggable.auto import tqdm
from tqdm_loggable.tqdm_logging import tqdm_logging
logger = logging.getLogger(__name__)
def main():
fmt = f"%(filename)-20s:%(lineno)-4d %(asctime)s %(message)s"
logging.basicConfig(level=logging.INFO, format=fmt, handlers=[logging.StreamHandler()])
# Set the log level to all tqdm-logging progress bars.
# Defaults to info - no need to set if you do not want to change the level
tqdm_logging.set_level(logging.INFO)
# Set the rate how often we update logs
# Defaults to 10 seconds - optional
tqdm_logging.set_log_rate(datetime.timedelta(seconds=10))
logger.info("This is an INFO test message using Python logging")
with tqdm(total=60_000, desc="Sample progress", unit_scale=True) as progress_bar:
for i in range(60_000):
progress_bar.update(1000)
# Test postfix output
progress_bar.set_postfix({"Current time": datetime.datetime.utcnow()})
time.sleep(0.5)
tqdm_loggable
会检测非交互式会话。如果应用程序在没有适当终端的情况下运行,则将使用非交互式进度消息。否则,进度条将委托给tqdm.auto
模块以保持与任何tqdm
系统兼容,而无需对代码进行任何更改。
tqdm_loggable
还为Jupyter Notebook环境(如Datalore)提供进度条解决方案,这些环境与原始Jupyter Notebook不完全兼容。
用于记录消息的Python日志实例名为tqqm_loggable.tqm_logging
。
开发
注意:此存储库活动度很低,我们不积极监控新问题。如果您有问题或PR,请在Discord中联系我们。
您可以使用tqdm_loggable/manual_tests.py运行各种检查,以查看不同的交互式和非交互式会话会给出什么结果。
# Normal interactive terminal run
poetry run manual-tests
因为这是一个正常的shell会话,所以您将获得正常的进度条
Sample progress: 20%|████████▏ | 12.0k/60.0k [00:05<00:24, 1.98kit/s, Current time=2022-09-21 15:40:24.274670]
...然后在没有适当的TERM环境变量的情况下进行测试
# Disable interactive terminal by fiddling with TERM environment variable
TERM=dumb poetry run manual-tests
您将获得日志消息
tqdm_logging.py :139 2022-09-21 17:41:20,720 Progress on:Sample progress -/60000 rate:- remaining:? elapsed:00:00 postfix:-
tqdm_logging.py :139 2022-09-21 17:41:30,803 Progress on:Sample progress 21.0kit/60.0kit rate:1,984.7it/s remaining:00:19 elapsed:00:10 postfix:Current time=2022-09-21 15:41:30.300714
...或者使用不同的Docker会话
# This will display process as log messages
docker build -t manual-tests . && docker run manual-tests
# This will allocate a terminal and display progress as a normal tqdm progress bar
docker build -t manual-tests . && docker run -ti manual-tests
或者使用重定向的stdout
poetry run manual-tests > output.txt
cat output.txt
这将输出我们的终端检测信息并绘制一个进度条,总共30秒。
tqdm-loggable manual tests
sys.stdout.isatty(): False
TERM: -
is_interactive_session(): False
然后根据您是否交互式运行手动测试,将跟随进一步的进度条或进度消息。
另请参阅
查看其他相关的日志包
致敬
最初为Trading Strategy区块链交易自动化构建。
许可证
MIT
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
构建分发
tqdm_loggable-0.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 175abec3e1f63bbd2eac192fa5da075e80c7bb715d7ccf3cd1a29b7ab5af0617 |
|
MD5 | bc6919d1e91729e7cb2f7f27c23b3f6a |
|
BLAKE2b-256 | 6596d924c326727dbdcac6043065dba08b1455aaaca4f7ef1e79d4fea889b34d |
tqdm_loggable-0.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9703046302b93a667166487759e6f3f49597e86c89eb132ba1f31caa07bf0941 |
|
MD5 | bf743e4176228091ea5f6980168b3890 |
|
BLAKE2b-256 | 121f1acb36a85797beba22934f124be6b51a7c18a4f408ce31443bec073181c7 |