Python progress - 文本进度条

Python progress - 文本进度条

https://pypi.org/project/progress/
https://github.com/verigak/progress

Project description

Easy progress reporting for Python

在这里插入图片描述

Bars

There are 7 progress bars to choose from:

  • Bar
  • ChargingBar
  • FillingSquaresBar
  • FillingCirclesBar
  • IncrementalBar
  • PixelBar
  • ShadyBar

To use them, just call next to advance and finish to finish:

from progress.bar import Bar

bar = Bar('Processing', max=20)
for i in range(20):
    # Do some work
    bar.next()
bar.finish()

or use any bar of this class as a context manager:

from progress.bar import Bar

with Bar('Processing', max=20) as bar:
    for i in range(20):
        # Do some work
        bar.next()

The result will be a bar like the following:

strong@foreverstrong:~/demo_workspace/progress_bar$ python progress_bar_v2.py 
Processing |################################| 20/20
strong@foreverstrong:~/demo_workspace/progress_bar$ python progress_bar_v2.py 
Processing |################################| 20/20
strong@foreverstrong:~/demo_workspace/progress_bar$

To simplify the common case where the work is done in an iterator, you can
use the iter method:

    for i in Bar('Processing').iter(it):
        # Do some work

Progress bars are very customizable, you can change their width, their fill
character, their suffix and more:

    bar = Bar('Loading', fill='@', suffix='%(percent)d%%')

This will produce a bar like the following:

    Loading |@@@@@@@@@@@@@                   | 42%

You can use a number of template arguments in message and suffix:

Name Value
index current value
max maximum value
remaining max - index
progress index / max
percent progress * 100
avg simple moving average time per item (in seconds)
elapsed elapsed time in seconds
elapsed_td elapsed as a timedelta (useful for printing as a string)
eta avg * remaining
eta_td eta as a timedelta (useful for printing as a string)

Instead of passing all configuration options on instatiation, you can create
your custom subclass:

    class FancyBar(Bar):
        message = 'Loading'
        fill = '*'
        suffix = '%(percent).1f%% - %(eta)ds'

You can also override any of the arguments or create your own:

    class SlowBar(Bar):
        suffix = '%(remaining_hours)d hours remaining'
        @property
        def remaining_hours(self):
            return self.eta // 3600

Spinners

For actions with an unknown number of steps you can use a spinner:

    from progress.spinner import Spinner

    spinner = Spinner('Loading ')
    while state != 'FINISHED':
        # Do some work
        spinner.next()

There are 5 predefined spinners:

  • Spinner
  • PieSpinner
  • MoonSpinner
  • LineSpinner
  • PixelSpinner
spinner ['spɪnə]:n. 纺纱机,纺纱工人,旋床工人,旋式诱饵
override [əʊvə'raɪd]:vt. 推翻,不顾,践踏 n. 代理佣金

Installation

pip install progress
strong@foreverstrong:~$ pip install progress
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
Collecting progress
  Downloading https://files.pythonhosted.org/packages/38/ef/2e887b3d2b248916fc2121889ce68af8a16aaddbe82f9ae6533c24ff0d2b/progress-1.5.tar.gz
Building wheels for collected packages: progress
  Running setup.py bdist_wheel for progress ... done
  Stored in directory: /home/strong/.cache/pip/wheels/6c/c8/80/32a294e3041f006c661838c05a411c7b7ffc60ff939d14e116
Successfully built progress
Installing collected packages: progress
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/progress'
Consider using the `--user` option or check the permissions.

You are using pip version 18.0, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
strong@foreverstrong:~$ 
strong@foreverstrong:~$ sudo pip install progress
[sudo] password for strong: 
Sorry, try again.
[sudo] password for strong: 
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/strong/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/strong/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting progress
Installing collected packages: progress
Successfully installed progress-1.5
strong@foreverstrong:~$
strong@foreverstrong:~$ sudo pip3 install progress
[sudo] password for strong: 
The directory '/home/strong/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/strong/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting progress
  Downloading https://files.pythonhosted.org/packages/38/ef/2e887b3d2b248916fc2121889ce68af8a16aaddbe82f9ae6533c24ff0d2b/progress-1.5.tar.gz
Installing collected packages: progress
  Running setup.py install for progress ... done
Successfully installed progress-1.5
You are using pip version 8.1.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
strong@foreverstrong:~$ 

test_progress.py

#!/usr/bin/env python

from __future__ import print_function

import random
import time

from progress.bar import (Bar, ChargingBar, FillingSquaresBar,
                          FillingCirclesBar, IncrementalBar, PixelBar,
                          ShadyBar)
from progress.spinner import (Spinner, PieSpinner, MoonSpinner, LineSpinner,
                              PixelSpinner)
from progress.counter import Counter, Countdown, Stack, Pie


def sleep():
    t = 0.01
    t += t * random.uniform(-0.1, 0.1)  # Add some variance
    time.sleep(t)


for bar_cls in (Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar):
    suffix = '%(index)d/%(max)d [%(elapsed)d / %(eta)d / %(eta_td)s]'
    bar = bar_cls(bar_cls.__name__, suffix=suffix)
    for i in bar.iter(range(200)):
        sleep()

for bar_cls in (IncrementalBar, PixelBar, ShadyBar):
    suffix = '%(percent)d%% [%(elapsed_td)s / %(eta)d / %(eta_td)s]'
    with bar_cls(bar_cls.__name__, suffix=suffix, max=200) as bar:
        for i in range(200):
            bar.next()
            sleep()

for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
    for i in spin(spin.__name__ + ' ').iter(range(100)):
        sleep()

for singleton in (Counter, Countdown, Stack, Pie):
    for i in singleton(singleton.__name__ + ' ').iter(range(100)):
        sleep()

bar = IncrementalBar('Random', suffix='%(index)d')
for i in range(100):
    bar.goto(random.randint(0, 100))
    sleep()
bar.finish()
strong@foreverstrong:~/demo_workspace$ git clone https://github.com/verigak/progress.git
Cloning into 'progress'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 240 (delta 3), reused 9 (delta 2), pack-reused 230
Receiving objects: 100% (240/240), 646.83 KiB | 11.00 KiB/s, done.
Resolving deltas: 100% (143/143), done.
Checking connectivity... done.
strong@foreverstrong:~/demo_workspace$ 
strong@foreverstrong:~/demo_workspace$ cd progress
strong@foreverstrong:~/demo_workspace/progress$ python test_progress.py 
Bar |################################| 200/200 [2 / 0 / 0:00:00]
ChargingBar ████████████████████████████████ 200/200 [2 / 0 / 0:00:00]
FillingSquaresBar ▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣ 200/200 [2 / 0 / 0:00:00]
FillingCirclesBar ◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉◉ 200/200 [2 / 0 / 0:00:00]
IncrementalBar |████████████████████████████████| 100% [0:00:02 / 0 / 0:00:00]
PixelBar |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 100% [0:00:02 / 0 / 0:00:00]
ShadyBar |████████████████████████████████| 100% [0:00:02 / 0 / 0:00:00]
Spinner -
PieSpinner ◷
MoonSpinner ◑
LineSpinner ⎼
PixelSpinner ⡿
Counter 100
Countdown 0  
Stack █
Pie ●
Random |███████████████████▌            | 61
strong@foreverstrong:~/demo_workspace/progress$ 

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/89364435