【Python】【有趣的模块】

tqdm

"""

【tqdm】 显示循环的进度条,再也不用担心程序跑到哪里还要跑多久了

tqdm 可以直接包裹iterable对象

from tqdm import tqdm,trange
from time import sleep

text = ""
for char in tqdm(['a','b','c','d']):
text += char
sleep(1)

trange(i)相当于tqdm(range(i))

from tqdm import tqdm,trange
from time import sleep
for i in trange(100):
sleep(0.01)

可以在循环外预先定义tqdm对象

pbar = tqdm(['a','b','c','d','e','f','g','aaa'])
for char in pbar:
pbar.set_description("Processing %s" % char) #Processing aaa: 100%|██████████| 8/8 [00:00<00:00, 14260.28it/s]

'''
有两个参数比较有用,desc(str)和leave(bool)
desc可以指定这个循环的的信息,以便区分。上面的set_description(str)和这个应该是一样的。
leave则表示进度条跑完了之后是否继续保留
'''

from tqdm import tqdm,trange
from time import sleep

for i in tqdm(range(3),desc='1st loop'):
for j in trange(5,desc='2nd loop',leave=False):
sleep(0.1)
'''
leave == True
1st loop: 0%| | 0/3 [00:00<?, ?it/s]
2nd loop: 0%| | 0/5 [00:00<?, ?it/s]
2nd loop: 20%|██ | 1/5 [00:00<00:00, 9.96it/s]
2nd loop: 40%|████ | 2/5 [00:00<00:00, 9.82it/s]
2nd loop: 60%|██████ | 3/5 [00:00<00:00, 9.78it/s]
2nd loop: 80%|████████ | 4/5 [00:00<00:00, 9.76it/s]
2nd loop: 100%|██████████| 5/5 [00:00<00:00, 9.73it/s]
1st loop: 33%|███▎ | 1/3 [00:00<00:01, 1.94it/s]
2nd loop: 0%| | 0/5 [00:00<?, ?it/s]
2nd loop: 20%|██ | 1/5 [00:00<00:00, 9.94it/s]
2nd loop: 40%|████ | 2/5 [00:00<00:00, 9.81it/s]
2nd loop: 60%|██████ | 3/5 [00:00<00:00, 9.78it/s]
2nd loop: 80%|████████ | 4/5 [00:00<00:00, 9.76it/s]
2nd loop: 100%|██████████| 5/5 [00:00<00:00, 9.74it/s]
1st loop: 67%|██████▋ | 2/3 [00:01<00:00, 1.95it/s]
2nd loop: 0%| | 0/5 [00:00<?, ?it/s]
2nd loop: 20%|██ | 1/5 [00:00<00:00, 9.86it/s]
2nd loop: 40%|████ | 2/5 [00:00<00:00, 9.75it/s]
2nd loop: 60%|██████ | 3/5 [00:00<00:00, 9.69it/s]
2nd loop: 80%|████████ | 4/5 [00:00<00:00, 9.67it/s]
2nd loop: 100%|██████████| 5/5 [00:00<00:00, 9.67it/s]
1st loop: 100%|██████████| 3/3 [00:01<00:00, 1.94it/s]
leave == False
1st loop: 0%| | 0/3 [00:00<?, ?it/s]
2nd loop: 0%| | 0/5 [00:00<?, ?it/s]
2nd loop: 20%|██ | 1/5 [00:00<00:00, 9.85it/s]
2nd loop: 40%|████ | 2/5 [00:00<00:00, 9.69it/s]
2nd loop: 60%|██████ | 3/5 [00:00<00:00, 9.63it/s]
2nd loop: 80%|████████ | 4/5 [00:00<00:00, 9.68it/s]
2nd loop: 100%|██████████| 5/5 [00:00<00:00, 9.67it/s]
1st loop: 33%|███▎ | 1/3 [00:00<00:01, 1.93it/s]
2nd loop: 0%| | 0/5 [00:00<?, ?it/s]
2nd loop: 20%|██ | 1/5 [00:00<00:00, 9.55it/s]
2nd loop: 40%|████ | 2/5 [00:00<00:00, 9.69it/s]
2nd loop: 60%|██████ | 3/5 [00:00<00:00, 9.68it/s]
2nd loop: 80%|████████ | 4/5 [00:00<00:00, 9.64it/s]
2nd loop: 100%|██████████| 5/5 [00:00<00:00, 9.68it/s]
1st loop: 67%|██████▋ | 2/3 [00:01<00:00, 1.93it/s]
2nd loop: 0%| | 0/5 [00:00<?, ?it/s]
2nd loop: 20%|██ | 1/5 [00:00<00:00, 9.60it/s]
2nd loop: 40%|████ | 2/5 [00:00<00:00, 9.70it/s]
2nd loop: 60%|██████ | 3/5 [00:00<00:00, 9.68it/s]
2nd loop: 80%|████████ | 4/5 [00:00<00:00, 9.65it/s]
2nd loop: 100%|██████████| 5/5 [00:00<00:00, 9.66it/s]
1st loop: 100%|██████████| 3/3 [00:01<00:00, 1.93it/s]
'''

'''
如果要在Jupyter Notebook上面使用,那么要把tqdm换成tqdm_notebook,trange换成tnrange
在Jupyter Notebook里面没有这个问题,还能用print()。

from tqdm import tnrange, tqdm_notebook
from time import sleep

for i in tqdm_notebook(range(10), desc='1st loop'):
for j in tnrange(100, desc='2nd loop', leave=False):
sleep(0.01)
'''

如果要在终端运行期间打印什么,用tqdm.write(str)

from tqdm import tqdm,trange
from time import sleep

for i in trange(10):
tqdm.write(str(i))
sleep(0.1)
'''
千奇百怪的结果
1⃣️
0
1
2
3
4
5
6
7
8
80%|████████ | 8/10 [00:00<00:00, 9.65it/s]9
90%|█████████ | 9/10 [00:00<00:00, 9.67it/s]100%|██████████| 10/10 [00:01<00:00, 9.66it/s]
2⃣️
0
0%| | 0/10 [00:00<?, ?it/s]1
10%|█ | 1/10 [00:00<00:00, 9.70it/s]2
20%|██ | 2/10 [00:00<00:00, 9.70it/s]3
40%|████ | 4/10 [00:00<00:00, 9.65it/s]4
5
50%|█████ | 5/10 [00:00<00:00, 9.60it/s]6
60%|██████ | 6/10 [00:00<00:00, 9.61it/s]7
70%|███████ | 7/10 [00:00<00:00, 9.60it/s]8
80%|████████ | 8/10 [00:00<00:00, 9.60it/s]9
90%|█████████ | 9/10 [00:00<00:00, 9.62it/s]100%|██████████| 10/10 [00:01<00:00, 9.63it/s]
3⃣️
0
1
10%|█ | 1/10 [00:00<00:00, 9.48it/s]2
30%|███ | 3/10 [00:00<00:00, 9.54it/s]3
4
40%|████ | 4/10 [00:00<00:00, 9.61it/s]5
60%|██████ | 6/10 [00:00<00:00, 9.61it/s]6
7
70%|███████ | 7/10 [00:00<00:00, 9.61it/s]8
80%|████████ | 8/10 [00:00<00:00, 9.60it/s]9
100%|██████████| 10/10 [00:01<00:00, 9.66it/s]
4⃣️
0
10%|█ | 1/10 [00:00<00:00, 9.71it/s]1
2
20%|██ | 2/10 [00:00<00:00, 9.73it/s]3
40%|████ | 4/10 [00:00<00:00, 9.72it/s]4
5
50%|█████ | 5/10 [00:00<00:00, 9.69it/s]6
60%|██████ | 6/10 [00:00<00:00, 9.67it/s]7
80%|████████ | 8/10 [00:00<00:00, 9.67it/s]8
90%|█████████ | 9/10 [00:00<00:00, 9.65it/s]9
100%|██████████| 10/10 [00:01<00:00, 9.64it/s]
5⃣️
0
0%| | 0/10 [00:00<?, ?it/s]1
10%|█ | 1/10 [00:00<00:00, 9.49it/s]2
40%|████ | 4/10 [00:00<00:00, 9.56it/s]3
4
50%|█████ | 5/10 [00:00<00:00, 9.59it/s]5
6
70%|███████ | 7/10 [00:00<00:00, 9.59it/s]7
8
90%|█████████ | 9/10 [00:00<00:00, 9.58it/s]9
100%|██████████| 10/10 [00:01<00:00, 9.59it/s]
等等。。。。
'''

"""

猜你喜欢

转载自www.cnblogs.com/suren2017/p/9435022.html