目录[-]
tqdm 的使用与延伸
简述:tqdm在西班牙语中是进步的意思(taqadum,تقدم),西班牙语(te quiero demasiado)中“我爱你这么多”的缩写。它是一个可以显示进度条的包,我们可以将其用到任何我们需要的地方,可以用在迭代器,列表
项目官网:https://pypi.org/project/tqdm/backgroundCover.opacity
安装最新的PyPi稳定版本
简单的使用
#我们先看一下tqdm的类
class tqdm(object):
"""
Decorate an iterable object, returning an iterator which acts exactly
like the original iterable, but prints a dynamically updating
progressbar every time a value is requested.
"""
def __init__(self, iterable=None, desc=None, total=None, leave=True,
file=None, ncols=None, mininterval=0.1,
maxinterval=10.0, miniters=None, ascii=None, disable=False,
unit='it', unit_scale=False, dynamic_ncols=False,
smoothing=0.3, bar_format=None, initial=0, position=None,
postfix=None, unit_divisor=1000):
我们可以看到这是一个装饰器,用来装饰一个迭代器,然后打印一个动态更新的精度条。
参数介绍,捡重点说:
desc:str() 精度条前缀
total:int() 预计可迭代的次数,我们可以设置文件大小
initial:int() 初始计数器值。重新启动进度条时有用[默认值:0]
leave:bool() 在迭代结束,是否保留所有更新信息
mininterval :float() 最小进度显示更新间隔[默认值:0.1]秒。
unit :str() 将用于定义每次迭代的单位的字符串[default:it]。二进制设置为"B"
unit_scale : bool或int或float,如果为1或True,则迭代次数将自动减少/缩放,并且将添加遵循国际单位制标准的公制前缀(千,兆等)[默认值:False]。如果有任何其他非零数字,将缩放总数和n。
返回:
tqdm包含的方法有:
def update(self, n=1):
"""
Manually update the progress bar, useful for streams
such as reading files.
E.g.:
>>> t = tqdm(total=filesize) # Initialise
>>> for current_buffer in stream:
... ...
... t.update(len(current_buffer))
>>> t.close()
The last line is highly recommended, but possibly not necessary if
t.update()
will be called in such a way that filesize
will be
exactly reached and printed.
Parameters
----------
n : int, optional
Increment to add to the internal counter of iterations
[default: 1].
"""
def close(self):
"""
Cleanup and (if leave=False) close the progressbar.
"""
def unpause(self):
"""
Restart tqdm timer from last print time.
"""
def clear(self, nomove=False):
"""
Clear current bar display
"""
def refresh(self):
"""
Force refresh the display of this bar
"""
def write(cls, s, file=sys.stdout, end="\n"):
"""
Print a message via tqdm (without overlap with bars)
"""
我们主要讲说一下update()方法
>>> t = tqdm(total=filesize) # Initialise
>>> for current_buffer in stream:
... ...
... t.update(len(current_buffer))
>>> t.close()
我们在命令行中运行,可以发现参数n就是每次更新的大小。