python进度条百分比_一个提供程序执行的进度显示和百分比进度条的库

PyPrind (Python Progress Indicator)

The PyPrind (Python Progress Indicator) module provides a progress bar and a percentage indicator object that let you track the progress of a loop structure or other iterative computation.

Typical applications include the processing of large data sets to provide an intuitive estimate at runtime about the progress of the computation.

Progress Bars and Percentage Generators

import pyprind

for i in pyprind.prog_bar(range(n)):

time.sleep(timesleep) # your computation here

0% 100%

[##############################] | ETA: 00:00:00

Total time elapsed: 00:00:05

for i in pyprind.prog_percent(range(n)):

time.sleep(timesleep) # your computation here

[10 %] Time elapsed: 00:00:01 | ETA: 00:00:04

While-loops

The ProgBar and ProgPercent classes also support while loops if desired. The objects are updated inside the loop using the update method as shown below:

import random

import pyprind

import time

timesleep = 0.05

random.seed(1)

collection = set()

n = 100

bar = pyprind.ProgBar(n, track_time=False, title='while example')

while len(collection) < n:

r = random.randint(0, 10**5)

if r % 7 and r not in collection:

collection.add(r)

bar.update()

time.sleep(timesleep)

print(bar)

while example

0% 100%

[##############################]

Title: while example

Started: 09/07/2016 13:06:58

Finished: 09/07/2016 13:07:03

Total time elapsed: 00:00:05

Advanced Tracking

If you have the psutil package installed, you can set the monitor=True to track CPU and memory usage:

bar = pyprind.ProgBar(n, monitor=True)

for i in range(n):

time.sleep(timesleep) # your computation here

bar.update()

print(bar)

0% 100%

[##############################] | ETA: 00:00:00

Total time elapsed: 00:00:05

Title:

Started: 09/07/2016 13:14:09

Finished: 09/07/2016 13:14:14

Total time elapsed: 00:00:05

CPU %: 1.90

Memory %: 0.48

Choose Your Favorite Bar Style

bar = pyprind.ProgBar(n, bar_char='█')

for i in range(n):

time.sleep(0.1) # do some computation

bar.update()

0% 100%

[██████████████████████████████] | ETA: 00:00:00

Total time elapsed: 00:00:10

Note to PyCharm users

If you are using the PyCharm IDE, you need to pass the sys.stdout or sys.err as a stream argument to display the progress indicators correctly in the IDE. For example,

import sys

bar = pyprind.ProgBar(n, stream=sys.stdout)

for i in range(n):

time.sleep(0.1) # do some computation

bar.update()

Sections

Installation

The most convenient way to install PyPrind is via tools like pip or easy_install:

pip install pyprind

easy_install pyprind

PyPrind comes without any dependencies except for the optional psutil to monitor CPU and memory usages. You can install psutil via pip install psutil or install it automatically with PyPrind:

pip install pyprind -r requirements.txt

Alternatively, you can install PyPrind the classic way: Download the package from the Python Package Index https://pypi.python.org/pypi/PyPrind, unzip it, navigate into the unzipped directory, and use the command

python setup.py install

Documentation

PyPrind consists of two class objects that can visualize the progress of a computation on the output screen.

Progress bars are available via ProgBar, and percentage indicators can be used via a ProgPercent.

n = 10000000

bar = pyprind.ProgBar(n) # 1) initialization with number of iterations

for i in range(n):

# do some computation

bar.update() # 2) update the progress visualization

Alternatively, the progress can be tracked via the equivalent generator functions prog_bar and prog_percent:

for i in pyprind.prog_bar(range(n)):

# do something

pass

Complete Parameter of Parameters and Options

ProgBar

ProgBar(iterations, track_time=True, width=30, bar_char='#', stream=2, title='', monitor=False, update_interval=None))

iterations : int

Number of iterations for the iterative computation.

track_time : bool (default: True)

Prints elapsed time when loop has finished.

width : int (default: 30)

Sets the progress bar width in characters.

stream : int (default: 2).

Setting the output stream. Takes 1 for stdout, 2 for stderr, or a custom stream object

title : str (default: '')

Setting a title for the progress bar.

monitor : bool (default: False)

Monitors CPU and memory usage if True (requires psutil package).

update_interval : float or int (default: None)

The update_interval in seconds controls how often the progress is flushed to the screen. Automatic mode if update_interval=None.

ProgPercent

ProgPercent(iterations, track_time=True, stream=2, title='', monitor=False, update_interval=None)

iterations : int

Number of iterations for the iterative computation.

track_time : bool (default: True)

Prints elapsed time when loop has finished.

stream : int (default: 2).

Setting the output stream. Takes 1 for stdout, 2 for stderr, or a custom stream object

title : str (default : '').

Setting a title for the percentage indicator.

monitor : bool (default: False)

Monitors CPU and memory usage if True (requires psutil package).

update_interval : float or int (default: None)

The update_interval in seconds controls how often the progress is flushed to the screen. Automatic mode if update_interval=None.

update method

update(iterations=1, item_id=None, force_flush=False)

iterations : int (default: 1)

default argument can be changed to integer values >=1 in order to update the progress indicators more than once per iteration.

item_id : str (default: None)

Print an item_id sring behind the progress bar

force_flush : bool (default: False)

If True, flushes the progress indicator to the output screen in each iteration.

Examples

Examples for using the progress bar and percentage indicator objects can be found in the IPython Demo Notebook.

Contact

If you have any questions or comments about PyPrind, please feel free to contact me via

eMail: mail@sebastianraschka.com

or Twitter: @rasbt

The pyprind module is available on GitHub at https://github.com/rasbt/pyprind.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你执行长时间运行的任务时,可以使用进度条显示任务的进度,让用户知道任务的执行情况。下面是一个使用Python编写的简单进度条程序的示例: ```python import time def progress_bar(total, progress, length=50): percent = progress / total filled_length = int(length * percent) bar = '=' * filled_length + '-' * (length - filled_length) print(f'\rProgress: [{bar}] {percent:.0%}', end='', flush=True) # 模拟耗时操作 total = 100 for i in range(total + 1): progress_bar(total, i) time.sleep(0.1) print("\nTask completed!") ``` 在上面的示例中,我们定义了一个名为`progress_bar`的函数,它接受三个参数:`total`(总进度数)、`progress`(当前进度数)和`length`(进度条长度,默认为50)。 函数内部首先计算当前进度相对于总进度百分比,并根据百分比计算出已填充部分和未填充部分的长度。然后使用字符串乘法操作符生成一个由填充部分和未填充部分组成的字符串,并使用`print`函数将其输出到屏幕上。通过设置`end=''`和`flush=True`参数,我们可以确保进度条在同一行上更新,而不是每次迭代都创建新的行。 在主程序中,我们模拟了一个耗时操作,循环从0到总进度数,并在每次迭代中调用`progress_bar`函数来更新进度条。为了模拟耗时操作,我们使用`time.sleep(0.1)`让程序休眠0.1秒。最后,在循环结束后,我们打印出"Task completed!"来表示任务完成。 运行以上代码,你将看到一个进度条在屏幕上以百分比的形式显示任务的进度,随着时间的推移,进度条将填充,直到任务完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值