python 的 Tqdm 模块

python的Tqdm模块


Tqdm 是一个快速,可扩展的Python进度条,可以在 Python 长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator)。

我的系统是window环境,首先安装python,接下来就是pip。

pip安装:

在python根目录下创建一个get-pip.py的文件,内容:


https://bootstrap.pypa.io/get-pip.py
然后在CMD窗口进入python下面:
输出:


python -m pip install -U pip
由于Tqdm要求的pip版本是9.0所以需要手动安装pip9.0
http://pypi.python.org/pypi/pip

下载安装包9.0

然后解压进入,CMD窗口输入:python setup.py install

然后就可以安装Tqdm了,


pip install tqdm
安装最新的开发版的话

pip install -e git+https://github.com/tqdm/tqdm.git@master#egg=tqdm
最后看看怎么用呢?https://pypi.python.org/pypi/tqdm
基本用法:


from tqdm import tqdm
for i in tqdm(range(10000)):
sleep(0.01)
当然除了tqdm,还有trange,使用方式完全相同
for i in trange(100):
sleep(0.1)
只要传入list都可以:

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
pbar.set_description("Processing %s" % char)
也可以手动控制更新
with tqdm(total=100) as pbar:
for i in range(10):
pbar.update(10)
也可以这样:

pbar = tqdm(total=100)
for i in range(10):
pbar.update(10)
pbar.close()
在Shell的tqdm用法
统计所有python脚本的行数:


$ time find . -name '*.py' -exec cat {} ; | wc -l
857365

real 0m3.458s
user 0m0.274s
sys 0m3.325s

$ time find . -name '*.py' -exec cat {} ; | tqdm | wc -l
857366it [00:03, 246471.31it/s]
857365

real 0m3.585s
user 0m0.862s
sys 0m3.358s
使用参数:
$ find . -name '*.py' -exec cat {} ; |
tqdm --unit loc --unit_scale --total 857366 >> /dev/null
100%|███████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s]
备份一个目录:
$ 7z a -bd -r backup.7z docs/ | grep Compressing |
tqdm --total $(find docs/ -type f | wc -l) --unit files >> backup.log
100%|███████████████████████████████▉| 8014/8014 [01:37<00:00, 82.29files/s
————————————————
版权声明:本文为CSDN博主「langb2014」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/langb2014/article/details/54798823

=================================

tqdm输出的含义:

 17%|█▋        | 134/782 [00:19<01:21,  7.98it/s, loss=0.375 ]

The fields in order are:

  • 17%: Percentage complete.
  • |█▋ | : Progress bar
  • 134/782: Number of items iterated over total number of items.
  • [00:19<01:21, 7.98it/s, loss=0.375 ]: Lets break this down below separately.
    • 00:19<01:21 : it refers to {elapsed}<{remaining}.
    • 7.98it/s: iterations per second
    • loss=0.375: As the label says, it is the loss.



如果这篇文章帮助到了你,你可以请作者喝一杯咖啡

原文地址:https://www.cnblogs.com/sddai/p/14505446.html