极简和蟒蛇进度条

progressist的Python项目详细描述


Build StatusPyPICoverage StatusPyPI

进步主义者

极简和Python进度条。

asciicast

安装

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')
namedefaultdescription
done_char^{}Char used for filling the progress bar
remain_char^{} (a space)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 ^{}, ^{} or ^{}
throttle0Minimum value between two ^{} call to issue a render: can accept an ^{} for an absolute throttling, a float for a percentage throttling (total must then be set) or a dimedelta for a throttling in seconds

内置模板变量

namedescriptiontypedefault formatting
prefixLeading label in default templatestringstr
elapsedThe elapsed time from the first iteration (in seconds)intas timedelta
etaThe computed ETAdatetime^{} if less than 24 hours, else ^{}
ttaThe estimated remaining time (time to arrival; in seconds)intas timedelta
avgThe average time per iteration, in secondsfloat^{}
speedThe average number of iterations per secondfloat^{}
doneThe number of done iterationsinteger
totalThe total number of iterations to be doneinteger
remaingThe number of iterations remaining to be doneinteger
percentThe percent of iterations already donefloat^{}
animationThe actual progress bartemplate string (^{}, ^{} or ^{})

自定义格式

我们使用一些方便的自定义规范扩展了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'
    

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java扫描器到字节[]?   java JSON对象泄漏窗口   n个列表中的java笛卡尔积   使用jfreechart的具有上限线的java堆叠条形图   java ANTLR以递归方式替换令牌   使用ORDER BY对端点中的值进行排序的java查询   java JFace为SWT添加了什么?   多线程Java:在后台用大量行更新JTable   T具有不兼容的Java泛型边界   java如何获取订阅我的公共viber帐户的订阅者的信息?   java正则表达式:获取嵌套重复组   java Jackson正在忽略XmlAccessorType。没有一个