极简和蟒蛇进度条
progressist的Python项目详细描述
进步主义者
极简和Python进度条。
安装
pip install progressist
用法
from progressist import ProgressBar
bar = ProgressBar(total=mytotalstuff)
for item in mystuff:
# do_stuff
bar.update()
或者透明地使用bar.iter
for item in bar.iter(mystuff):
do_stuff
它附带了一个默认的渲染,足以启动,但是 非常容易定制:只需编写一个模板字符串:
bar = ProgressBar(total=mytotalstuff, template='{prefix} {progress} ETA: {eta}')
很简单python formatting 因此可以使用任何有效的字符串格式来控制外观。 例如:
bar = ProgressBar(total=mytotalstuff, template='{progress} {percent:.2%} ETA: {eta:%H:%M:%S}')
您也可以更改填充字符:
bar = ProgressBar(total=mytotalstuff, done_char='#')
您可以更改进度逻辑本身,例如使用微调器(包括):
bar = ProgressBar(total=mytotalstuff, progress='{spinner}')
# 'progress' kwarg must return a valid template variable.
# included ones are {bar} and {spinner}
一次可以一步一步地进行操作:
for item in mystuff:
amount = do_stuff()
bar.update(step=amount)
通过子类化ProgressBar
:
class MyBar(ProgressBar):
@property
def swap(self):
return psutil.swap_memory().total
bar = MyBar(total=20, template='{prefix} {progress} Swap usage: {swap}')
如果在不同的地方使用相同的配置,则创建一个子类并 将其配置设置为类属性:
class MyBar(ProgressBar):
template = ('Download |{animation}| {done:B}/{total:B}')
done_char = '⬛'
bar = MyBar()
你想自己计算完成的部分吗?
bar.update(done=myvar / othervar * another)
或者在这个过程中目标总数可能会改变?
bar.update(total=newcomputedtotal)
用作urlretrieve 回拨:
bar = ProgressBar(template="Download |{animation}| {done:B}/{total:B}")
urllib.request.urlretrieve(myurl, mydest, reporthook=bar.on_urlretrieve)
有关灵感,请参见examples。
要运行示例,当git克隆存储库时,只需运行:
python examples.py
如果只想运行一个示例,请将其名称添加到命令行:
python examples.py example_download
参数
您可以将所有这些参数设置为类属性:
class MyBar(ProgressBar):
done_char = 'x'
bar = Bar()
或在初始时:
bar = ProgressBar(done_char='x')
或更新时:
bar = Bar()
bar.update(prefix='Finishing')
name | default | description |
---|---|---|
done_char | ^{ | Char used for filling the progress bar |
remain_char | ^{ | Char used for filling the empty portion of the progress bar |
template | ^{ | The template of the whole line |
prefix | ^{ | The leading label |
animation | '{progress}' | The actual widget used for progress, can be ^{ |
throttle | 0 | Minimum value between two ^{ |
内置模板变量
name | description | type | default formatting |
---|---|---|---|
prefix | Leading label in default template | string | str |
elapsed | The elapsed time from the first iteration (in seconds) | int | as timedelta |
eta | The computed ETA | datetime | ^{ |
tta | The estimated remaining time (time to arrival; in seconds) | int | as timedelta |
avg | The average time per iteration, in seconds | float | ^{ |
speed | The average number of iterations per second | float | ^{ |
done | The number of done iterations | integer | |
total | The total number of iterations to be done | integer | |
remaing | The number of iterations remaining to be done | integer | |
percent | The percent of iterations already done | float | ^{ |
animation | The actual progress bar | template string (^{ |
自定义格式
我们使用一些方便的自定义规范扩展了python默认格式化程序:
B
类型:将int呈现为人类友好的字节大小。例如:> bar.total = 109830983 > bar.template = '{total:B}' > bar.render() '104.7 MiB'
您仍然可以覆盖ndigts值:
> bar.total = 109830983 > bar.template = '{total:0.2B}' > bar.render() '104.74 MiB'
D
类型:尝试强制转换为整数。例如:> bar.speed = 103.23 > bar.template = '{speed:D}' > bar.render() '103'