tqdm - 显示当前步骤和总进度。如何实现?

3

例如,当处理不同长度的N个文件时,您想要查看每个文件的处理进度(M/N)以及考虑它们的大小的总时间进度。 简单模拟:

from tqdm import tqdm
from time import sleep

fmt = '{n_fmt} of {total_fmt} {percentage:3.0f}%'
iteration = (3, 2, 1, 4)  # filesize
bar = tqdm(total=sum(iteration), miniters=1, bar_format=fmt)
for i in iteration:  # by file process
    sleep(i)
    bar.update(i)

进度条将根据大小显示进度:

3 of 10  30%
5 of 10  50% 
6 of 10  60%
10 of 10 100%

need:

1 of 4  30%
2 of 4  50% 
3 of 4  60%
4 of 4 100%

怎么做?
1个回答

0

基于帮助文档中的片段,介绍如何向进度条添加自定义参数。

from tqdm import tqdm
from time import sleep

class TqdmTick(tqdm):
    def __init__(self, ticks, *args, **kwargs):
        self._ticks = ticks
        self._tick = 0
        super().__init__(*args, **kwargs)

    def tick(self, value):
        self._tick += 1
        self.update(value)

    @property
    def format_dict(self):
        d = super().format_dict
        d.update(tick=str(self._tick), ticks=str(self._ticks))
        return d

fmt = '{tick} of {ticks} {percentage:3.0f}%'

iteration = (3, 2, 1, 4)
bar = TqdmTick(ticks=len(iteration), total=sum(iteration), bar_format=fmt)

for i in iteration:
    sleep(i)
    bar.tick(i)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接