mirror of
https://github.com/verigak/progress.git
synced 2025-12-08 19:33:24 +00:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5c911ed83 | ||
|
|
5d52c5b299 | ||
|
|
a83f91f4b8 | ||
|
|
292a031c4b | ||
|
|
83f3b79137 | ||
|
|
84c3b9197a | ||
|
|
715a2e130f | ||
|
|
33ab0be1ec | ||
|
|
91d9d3cb05 | ||
|
|
6661bcbe1d | ||
|
|
d440d5bbaf | ||
|
|
7ecf7594a4 | ||
|
|
92665ef189 | ||
|
|
c5043685c5 | ||
|
|
f1f6ea57da | ||
|
|
0b668811f9 | ||
|
|
e61e49bbf4 | ||
|
|
61ac9b6980 | ||
|
|
0435756cf7 | ||
|
|
ded975221b | ||
|
|
5e45abaa95 | ||
|
|
80b54c1228 | ||
|
|
36ba012ffb | ||
|
|
f1bfb28df9 | ||
|
|
ee7831362c | ||
|
|
85b59e0a1d | ||
|
|
94ff8dd979 | ||
|
|
288ddf1ec5 | ||
|
|
723024a296 | ||
|
|
a5981103cf | ||
|
|
120b3e53b1 | ||
|
|
8b02a5b59f | ||
|
|
e5cffc8c72 | ||
|
|
41d5f916c1 |
100
README.rst
100
README.rst
@@ -1,19 +1,30 @@
|
||||
Easy progress reporting for Python
|
||||
==================================
|
||||
|
||||
|pypi|
|
||||
|
||||
|demo|
|
||||
|
||||
.. |pypi| image:: https://img.shields.io/pypi/v/progress.svg
|
||||
.. |demo| image:: https://raw.github.com/verigak/progress/master/demo.gif
|
||||
:alt: Demo
|
||||
|
||||
Bars
|
||||
----
|
||||
|
||||
There are 6 progress bars to choose from:
|
||||
There are 7 progress bars to choose from:
|
||||
|
||||
- Bar
|
||||
- ChargingBar
|
||||
- FillingSquaresBar
|
||||
- FillingCirclesBar
|
||||
- IncrementalBar
|
||||
- ShadyBar
|
||||
- ``Bar``
|
||||
- ``ChargingBar``
|
||||
- ``FillingSquaresBar``
|
||||
- ``FillingCirclesBar``
|
||||
- ``IncrementalBar``
|
||||
- ``PixelBar``
|
||||
- ``ShadyBar``
|
||||
|
||||
To use them, just call ``next`` to advance and ``finish`` to finish. ::
|
||||
To use them, just call ``next`` to advance and ``finish`` to finish:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from progress.bar import Bar
|
||||
|
||||
@@ -28,13 +39,17 @@ The result will be a bar like the following: ::
|
||||
Processing |############# | 42/100
|
||||
|
||||
To simplify the common case where the work is done in an iterator, you can
|
||||
use the ``iter`` method. ::
|
||||
use the ``iter`` method:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
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. ::
|
||||
character, their suffix and more:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
bar = Bar('Loading', fill='@', suffix='%(percent)d%%')
|
||||
|
||||
@@ -44,48 +59,73 @@ This will produce a bar like the following: ::
|
||||
|
||||
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 rolling average time per item (in seconds)
|
||||
eta avg * remaining
|
||||
========= =============================
|
||||
========== ================================
|
||||
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. ::
|
||||
your custom subclass:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class FancyBar(Bar):
|
||||
message = 'Loading'
|
||||
fill = '*'
|
||||
suffix = '%(percent).1f%% - %(eta)ds'
|
||||
|
||||
You can also override any of the arguments or create your own:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
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. ::
|
||||
For actions with an unknown number of steps you can use a spinner:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from progress.spinner import Spinner
|
||||
|
||||
|
||||
spinner = Spinner('Loading ')
|
||||
while state != 'FINISHED':
|
||||
# Do some work
|
||||
spinner.next()
|
||||
|
||||
There are 4 predefined spinners:
|
||||
There are 5 predefined spinners:
|
||||
|
||||
- ``Spinner``
|
||||
- ``PieSpinner``
|
||||
- ``MoonSpinner``
|
||||
- ``LineSpinner``
|
||||
- ``PixelSpinner``
|
||||
|
||||
- Spinner
|
||||
- PieSpinner
|
||||
- MoonSpinner
|
||||
- LineSpinner
|
||||
|
||||
Other
|
||||
=====
|
||||
|
||||
Thera are a number of other classes available too, please check the source or
|
||||
There are a number of other classes available too, please check the source or
|
||||
subclass one of them to create your own.
|
||||
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
progress is licensed under ISC
|
||||
|
||||
@@ -14,39 +14,46 @@
|
||||
|
||||
from __future__ import division
|
||||
|
||||
from collections import deque
|
||||
from datetime import timedelta
|
||||
from math import ceil
|
||||
from sys import stderr
|
||||
from time import time
|
||||
|
||||
|
||||
__version__ = '1.1'
|
||||
__version__ = '1.4'
|
||||
|
||||
|
||||
class Infinite(object):
|
||||
file = stderr
|
||||
sma_window = 10 # Simple Moving Average window
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.ctx = {}
|
||||
for key, val in kwargs.items():
|
||||
if hasattr(self, key):
|
||||
setattr(self, key, val)
|
||||
else:
|
||||
self.ctx[key] = val
|
||||
|
||||
self.index = 0
|
||||
self.start_ts = time()
|
||||
self.avg = 0
|
||||
self._ts = time()
|
||||
self._ts = self.start_ts
|
||||
self._xput = deque(maxlen=self.sma_window)
|
||||
for key, val in kwargs.items():
|
||||
setattr(self, key, val)
|
||||
|
||||
def update_stats(self):
|
||||
# Calculate moving average
|
||||
now = time()
|
||||
dt = now - self._ts
|
||||
self.avg = (dt + self.index * self.avg) / (self.index + 1) if self.avg else dt
|
||||
self._ts = now
|
||||
def __getitem__(self, key):
|
||||
if key.startswith('_'):
|
||||
return None
|
||||
return getattr(self, key, None)
|
||||
|
||||
kv = [(key, val) for key, val in self.__dict__.items()
|
||||
if not key.startswith('_')]
|
||||
self.ctx.update(kv)
|
||||
@property
|
||||
def elapsed(self):
|
||||
return int(time() - self.start_ts)
|
||||
|
||||
@property
|
||||
def elapsed_td(self):
|
||||
return timedelta(seconds=self.elapsed)
|
||||
|
||||
def update_avg(self, n, dt):
|
||||
if n > 0:
|
||||
self._xput.append(dt / n)
|
||||
self.avg = sum(self._xput) / len(self._xput)
|
||||
|
||||
def update(self):
|
||||
pass
|
||||
@@ -58,64 +65,53 @@ class Infinite(object):
|
||||
pass
|
||||
|
||||
def next(self, n=1):
|
||||
now = time()
|
||||
dt = now - self._ts
|
||||
self.update_avg(n, dt)
|
||||
self._ts = now
|
||||
self.index = self.index + n
|
||||
self.update_stats()
|
||||
self.update()
|
||||
|
||||
def iter(self, it):
|
||||
for x in it:
|
||||
yield x
|
||||
self.next()
|
||||
self.finish()
|
||||
try:
|
||||
for x in it:
|
||||
yield x
|
||||
self.next()
|
||||
finally:
|
||||
self.finish()
|
||||
|
||||
|
||||
class Progress(Infinite):
|
||||
backtrack = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Progress, self).__init__(*args, **kwargs)
|
||||
self.max = kwargs.get('max', 100)
|
||||
self.eta = 0
|
||||
|
||||
def update_stats(self):
|
||||
self.progress = min(1, self.index / self.max)
|
||||
self.percent = self.progress * 100
|
||||
self.remaining = self.max - self.index
|
||||
@property
|
||||
def eta(self):
|
||||
return int(ceil(self.avg * self.remaining))
|
||||
|
||||
# Calculate moving average
|
||||
now = time()
|
||||
if self.delta:
|
||||
dt = (now - self._ts) / self.delta
|
||||
self.avg = (dt + self.index * self.avg) / (self.index + 1) if self.avg else dt
|
||||
self.eta = int(ceil(self.avg * self.remaining))
|
||||
self._ts = now
|
||||
@property
|
||||
def eta_td(self):
|
||||
return timedelta(seconds=self.eta)
|
||||
|
||||
kv = [(key, val) for key, val in self.__dict__.items()
|
||||
if not key.startswith('_')]
|
||||
self.ctx.update(kv)
|
||||
@property
|
||||
def percent(self):
|
||||
return self.progress * 100
|
||||
|
||||
@property
|
||||
def progress(self):
|
||||
return min(1, self.index / self.max)
|
||||
|
||||
@property
|
||||
def remaining(self):
|
||||
return max(self.max - self.index, 0)
|
||||
|
||||
def start(self):
|
||||
self.delta = 0
|
||||
self.update_stats()
|
||||
self.update()
|
||||
|
||||
def next(self, n=1):
|
||||
prev = self.index
|
||||
self.index = min(self.index + n, self.max)
|
||||
self.delta = self.index - prev
|
||||
self.update_stats()
|
||||
self.update()
|
||||
|
||||
def goto(self, index):
|
||||
index = min(index, self.max)
|
||||
delta = index - self.index
|
||||
if delta <= 0 and not self.backtrack:
|
||||
return
|
||||
|
||||
self.index = index
|
||||
self.delta = delta
|
||||
self.update_stats()
|
||||
self.update()
|
||||
incr = index - self.index
|
||||
self.next(incr)
|
||||
|
||||
def iter(self, it):
|
||||
try:
|
||||
@@ -123,7 +119,9 @@ class Progress(Infinite):
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
for x in it:
|
||||
yield x
|
||||
self.next()
|
||||
self.finish()
|
||||
try:
|
||||
for x in it:
|
||||
yield x
|
||||
self.next()
|
||||
finally:
|
||||
self.finish()
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import sys
|
||||
|
||||
from . import Progress
|
||||
from .helpers import WritelnMixin
|
||||
|
||||
@@ -32,10 +36,10 @@ class Bar(WritelnMixin, Progress):
|
||||
filled_length = int(self.width * self.progress)
|
||||
empty_length = self.width - filled_length
|
||||
|
||||
message = self.message % self.ctx
|
||||
message = self.message % self
|
||||
bar = self.fill * filled_length
|
||||
empty = self.empty_fill * empty_length
|
||||
suffix = self.suffix % self.ctx
|
||||
suffix = self.suffix % self
|
||||
line = ''.join([message, self.bar_prefix, bar, empty, self.bar_suffix,
|
||||
suffix])
|
||||
self.writeln(line)
|
||||
@@ -45,39 +49,46 @@ class ChargingBar(Bar):
|
||||
suffix = '%(percent)d%%'
|
||||
bar_prefix = ' '
|
||||
bar_suffix = ' '
|
||||
empty_fill = u'∙'
|
||||
fill = u'█'
|
||||
empty_fill = '∙'
|
||||
fill = '█'
|
||||
|
||||
|
||||
class FillingSquaresBar(ChargingBar):
|
||||
empty_fill = u'▢'
|
||||
fill = u'▣'
|
||||
empty_fill = '▢'
|
||||
fill = '▣'
|
||||
|
||||
|
||||
class FillingCirclesBar(ChargingBar):
|
||||
empty_fill = u'◯'
|
||||
fill = u'◉'
|
||||
empty_fill = '◯'
|
||||
fill = '◉'
|
||||
|
||||
|
||||
class IncrementalBar(Bar):
|
||||
phases = (u' ', u'▏', u'▎', u'▍', u'▌', u'▋', u'▊', u'▉', u'█')
|
||||
if sys.platform.startswith('win'):
|
||||
phases = (u' ', u'▌', u'█')
|
||||
else:
|
||||
phases = (' ', '▏', '▎', '▍', '▌', '▋', '▊', '▉', '█')
|
||||
|
||||
def update(self):
|
||||
nphases = len(self.phases)
|
||||
expanded_length = int(nphases * self.width * self.progress)
|
||||
filled_length = int(self.width * self.progress)
|
||||
empty_length = self.width - filled_length
|
||||
phase = expanded_length - (filled_length * nphases)
|
||||
filled_len = self.width * self.progress
|
||||
nfull = int(filled_len) # Number of full chars
|
||||
phase = int((filled_len - nfull) * nphases) # Phase of last char
|
||||
nempty = self.width - nfull # Number of empty chars
|
||||
|
||||
message = self.message % self.ctx
|
||||
bar = self.phases[-1] * filled_length
|
||||
message = self.message % self
|
||||
bar = self.phases[-1] * nfull
|
||||
current = self.phases[phase] if phase > 0 else ''
|
||||
empty = self.empty_fill * max(0, empty_length - len(current))
|
||||
suffix = self.suffix % self.ctx
|
||||
empty = self.empty_fill * max(0, nempty - len(current))
|
||||
suffix = self.suffix % self
|
||||
line = ''.join([message, self.bar_prefix, bar, current, empty,
|
||||
self.bar_suffix, suffix])
|
||||
self.writeln(line)
|
||||
|
||||
|
||||
class PixelBar(IncrementalBar):
|
||||
phases = ('⡀', '⡄', '⡆', '⡇', '⣇', '⣧', '⣷', '⣿')
|
||||
|
||||
|
||||
class ShadyBar(IncrementalBar):
|
||||
phases = (u' ', u'░', u'▒', u'▓', u'█')
|
||||
phases = (' ', '░', '▒', '▓', '█')
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from . import Infinite, Progress
|
||||
from .helpers import WriteMixin
|
||||
|
||||
@@ -34,7 +35,7 @@ class Countdown(WriteMixin, Progress):
|
||||
|
||||
|
||||
class Stack(WriteMixin, Progress):
|
||||
phases = (u' ', u'▁', u'▂', u'▃', u'▄', u'▅', u'▆', u'▇', u'█')
|
||||
phases = (' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█')
|
||||
hide_cursor = True
|
||||
|
||||
def update(self):
|
||||
@@ -44,4 +45,4 @@ class Stack(WriteMixin, Progress):
|
||||
|
||||
|
||||
class Pie(Stack):
|
||||
phases = (u'○', u'◔', u'◑', u'◕', u'●')
|
||||
phases = ('○', '◔', '◑', '◕', '●')
|
||||
|
||||
@@ -28,22 +28,22 @@ class WriteMixin(object):
|
||||
if message:
|
||||
self.message = message
|
||||
|
||||
if self.file.isatty():
|
||||
if self.file and self.file.isatty():
|
||||
if self.hide_cursor:
|
||||
print(HIDE_CURSOR, end='', file=self.file)
|
||||
print(self.message, end='', file=self.file)
|
||||
self.file.flush()
|
||||
|
||||
def write(self, s):
|
||||
if self.file.isatty():
|
||||
if self.file and self.file.isatty():
|
||||
b = '\b' * self._width
|
||||
c = s.encode('utf8').ljust(self._width)
|
||||
c = s.ljust(self._width)
|
||||
print(b + c, end='', file=self.file)
|
||||
self._width = max(self._width, len(s))
|
||||
self.file.flush()
|
||||
|
||||
def finish(self):
|
||||
if self.file.isatty() and self.hide_cursor:
|
||||
if self.file and self.file.isatty() and self.hide_cursor:
|
||||
print(SHOW_CURSOR, end='', file=self.file)
|
||||
|
||||
|
||||
@@ -55,21 +55,21 @@ class WritelnMixin(object):
|
||||
if message:
|
||||
self.message = message
|
||||
|
||||
if self.file.isatty() and self.hide_cursor:
|
||||
if self.file and self.file.isatty() and self.hide_cursor:
|
||||
print(HIDE_CURSOR, end='', file=self.file)
|
||||
|
||||
def clearln(self):
|
||||
if self.file.isatty():
|
||||
if self.file and self.file.isatty():
|
||||
print('\r\x1b[K', end='', file=self.file)
|
||||
|
||||
def writeln(self, line):
|
||||
if self.file.isatty():
|
||||
if self.file and self.file.isatty():
|
||||
self.clearln()
|
||||
print(line.encode('utf8'), end='', file=self.file)
|
||||
print(line, end='', file=self.file)
|
||||
self.file.flush()
|
||||
|
||||
def finish(self):
|
||||
if self.file.isatty():
|
||||
if self.file and self.file.isatty():
|
||||
print(file=self.file)
|
||||
if self.hide_cursor:
|
||||
print(SHOW_CURSOR, end='', file=self.file)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from . import Infinite
|
||||
from .helpers import WriteMixin
|
||||
|
||||
@@ -29,12 +30,15 @@ class Spinner(WriteMixin, Infinite):
|
||||
|
||||
|
||||
class PieSpinner(Spinner):
|
||||
phases = [u'◷', u'◶', u'◵', u'◴']
|
||||
phases = ['◷', '◶', '◵', '◴']
|
||||
|
||||
|
||||
class MoonSpinner(Spinner):
|
||||
phases = [u'◑', u'◒', u'◐', u'◓']
|
||||
phases = ['◑', '◒', '◐', '◓']
|
||||
|
||||
|
||||
class LineSpinner(Spinner):
|
||||
phases = [u'⎺', u'⎻', u'⎼', u'⎽', u'⎼', u'⎻']
|
||||
phases = ['⎺', '⎻', '⎼', '⎽', '⎼', '⎻']
|
||||
|
||||
class PixelSpinner(Spinner):
|
||||
phases = ['⣾','⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
|
||||
|
||||
5
setup.py
5
setup.py
@@ -21,6 +21,9 @@ setup(
|
||||
'License :: OSI Approved :: ISC License (ISCL)',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3.3'
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
]
|
||||
)
|
||||
|
||||
@@ -2,35 +2,47 @@
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
from random import randint
|
||||
from time import sleep
|
||||
import random
|
||||
import time
|
||||
|
||||
from progress.bar import (Bar, ChargingBar, FillingSquaresBar,
|
||||
FillingCirclesBar, IncrementalBar, ShadyBar)
|
||||
from progress.spinner import Spinner, PieSpinner, MoonSpinner, LineSpinner
|
||||
FillingCirclesBar, IncrementalBar, PixelBar,
|
||||
ShadyBar)
|
||||
from progress.spinner import (Spinner, PieSpinner, MoonSpinner, LineSpinner,
|
||||
PixelSpinner)
|
||||
from progress.counter import Counter, Countdown, Stack, Pie
|
||||
|
||||
|
||||
for bar in (Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar):
|
||||
for i in bar(bar.__name__).iter(range(100)):
|
||||
sleep(0.04)
|
||||
def sleep():
|
||||
t = 0.01
|
||||
t += t * random.uniform(-0.1, 0.1) # Add some variance
|
||||
time.sleep(t)
|
||||
|
||||
for bar in (IncrementalBar, ShadyBar):
|
||||
for i in bar(bar.__name__).iter(range(200)):
|
||||
sleep(0.02)
|
||||
|
||||
for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner):
|
||||
for i in spin(spin.__name__ + ' ').iter(range(30)):
|
||||
sleep(0.1)
|
||||
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]'
|
||||
bar = bar_cls(bar_cls.__name__, suffix=suffix)
|
||||
for i in bar.iter(range(200)):
|
||||
sleep()
|
||||
|
||||
for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
|
||||
for i in spin(spin.__name__ + ' ').iter(range(100)):
|
||||
sleep()
|
||||
print()
|
||||
|
||||
for singleton in (Counter, Countdown, Stack, Pie):
|
||||
for i in singleton(singleton.__name__ + ' ').iter(range(100)):
|
||||
sleep(0.04)
|
||||
sleep()
|
||||
print()
|
||||
|
||||
bar = IncrementalBar('Random', backtrack=True, suffix='')
|
||||
bar = IncrementalBar('Random', suffix='%(index)d')
|
||||
for i in range(100):
|
||||
bar.goto(randint(0, 100))
|
||||
sleep(0.1)
|
||||
bar.goto(random.randint(0, 100))
|
||||
sleep()
|
||||
bar.finish()
|
||||
|
||||
Reference in New Issue
Block a user