# 最基本的用法 import time from tqdm import tqdm for i in tqdm(range(9)): time.sleep(0.1) # 效果如下 >>> 100%|██████████| 10/10 [00:01<00:00, 9.79it/s]
# trange类似于tqdm import time from tqdm import trange for i in trange(10): time.sleep(0.1) # 效果如下 >>> 100%|██████████| 10/10 [00:01<00:00, 9.79it/s]
# 传入list import time from tqdm import tqdm pbar = tqdm([1,2,3,4,5,6,7,8,9,10]) for char in pbar: pbar.set_description("Processing %s" % char) time.sleep(0.1) # 效果如下 >>> Processing 10: 100%|██████████| 10/10 [00:01<00:00, 9.49it/s] # 手动控制更新 import time from tqdm import tqdm with tqdm(total=10) as pbar: for i in range(10): pbar.update(1) time.sleep(0.1) # 效果如下 >>> 100%|██████████| 10/10 [00:00<00:00, 10.10it/s]
# 也可以这样 import time from tqdm import tqdm pbar = tqdm(total=10) for i in range(10): pbar.update(1) time.sleep(0.1) pbar.close() # 效果如下 >>> 100%|██████████| 10/10 [00:00<00:00, 10.10it/s]
在Spyder下正常了,然而在命令窗口有问题。
Rich
非常炫酷的包,当然这里仅仅用它的进度条。(由@kotori-y投稿)
安装
1
pip install rich
使用
基本用法:
1 2 3 4
from rich.progress import track
for step in track(range(100)): do_step(step)
按步更新:
1 2 3 4 5 6
from rich.progress import Progress
with Progress() as progress: task = progress.add_task("[red]Downloading...", total=total) #do sth progress.update(task, advance=1)
import time from func_timeout import func_set_timeout,FunctionTimedOut
# 基本用法 try: doitReturnValue = func_timeout(5, doit, args=('arg1', 'arg2')) except FunctionTimedOut: print ( "doit('arg1', 'arg2') could not complete within 5 seconds and was terminated.\n") except Exception as e: # 其他Exception
# 装饰器用法 @func_set_timeout(2) deftask(): time.sleep(5) # 效果如下 FunctionTimedOut: Function task (args=()) (kwargs={}) timed out after 2.000000 seconds.