mirror of
https://github.com/verigak/progress.git
synced 2025-12-11 12:43:01 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efeb57282b | ||
|
|
61627a8642 | ||
|
|
c1e07104e1 | ||
|
|
326413a271 | ||
|
|
1b326504ce | ||
|
|
b95b764e37 | ||
|
|
2eeff94083 | ||
|
|
8cf7faa4e3 | ||
|
|
4b7c9388f4 | ||
|
|
d9d40736d6 | ||
|
|
d407334bcf | ||
|
|
086cfd5599 | ||
|
|
6553b7b207 | ||
|
|
1f19b5b61c |
@@ -1 +1,2 @@
|
|||||||
include README.rst LICENSE
|
include README.rst LICENSE
|
||||||
|
include test_*.py
|
||||||
|
|||||||
21
README.rst
21
README.rst
@@ -6,6 +6,7 @@ Easy progress reporting for Python
|
|||||||
|demo|
|
|demo|
|
||||||
|
|
||||||
.. |pypi| image:: https://img.shields.io/pypi/v/progress.svg
|
.. |pypi| image:: https://img.shields.io/pypi/v/progress.svg
|
||||||
|
:target: https://pypi.org/project/progress/
|
||||||
.. |demo| image:: https://raw.github.com/verigak/progress/master/demo.gif
|
.. |demo| image:: https://raw.github.com/verigak/progress/master/demo.gif
|
||||||
:alt: Demo
|
:alt: Demo
|
||||||
|
|
||||||
@@ -34,6 +35,17 @@ To use them, just call ``next`` to advance and ``finish`` to finish:
|
|||||||
bar.next()
|
bar.next()
|
||||||
bar.finish()
|
bar.finish()
|
||||||
|
|
||||||
|
or use any bar of this class as a context manager:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
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: ::
|
The result will be a bar like the following: ::
|
||||||
|
|
||||||
Processing |############# | 42/100
|
Processing |############# | 42/100
|
||||||
@@ -117,6 +129,15 @@ There are 5 predefined spinners:
|
|||||||
- ``LineSpinner``
|
- ``LineSpinner``
|
||||||
- ``PixelSpinner``
|
- ``PixelSpinner``
|
||||||
|
|
||||||
|
Installation
|
||||||
|
============
|
||||||
|
|
||||||
|
Download from PyPi
|
||||||
|
|
||||||
|
.. code-block:: shell
|
||||||
|
|
||||||
|
pip install progress
|
||||||
|
|
||||||
|
|
||||||
Other
|
Other
|
||||||
=====
|
=====
|
||||||
|
|||||||
@@ -12,31 +12,49 @@
|
|||||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
from __future__ import division
|
from __future__ import division, print_function
|
||||||
|
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from math import ceil
|
from math import ceil
|
||||||
from sys import stderr
|
from sys import stderr
|
||||||
from time import time
|
try:
|
||||||
|
from time import monotonic
|
||||||
|
except ImportError:
|
||||||
|
from time import time as monotonic
|
||||||
|
|
||||||
|
|
||||||
__version__ = '1.4'
|
__version__ = '1.5'
|
||||||
|
|
||||||
|
HIDE_CURSOR = '\x1b[?25l'
|
||||||
|
SHOW_CURSOR = '\x1b[?25h'
|
||||||
|
|
||||||
|
|
||||||
class Infinite(object):
|
class Infinite(object):
|
||||||
file = stderr
|
file = stderr
|
||||||
sma_window = 10 # Simple Moving Average window
|
sma_window = 10 # Simple Moving Average window
|
||||||
|
check_tty = True
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, message='', **kwargs):
|
||||||
self.index = 0
|
self.index = 0
|
||||||
self.start_ts = time()
|
self.start_ts = monotonic()
|
||||||
self.avg = 0
|
self.avg = 0
|
||||||
|
self._avg_update_ts = self.start_ts
|
||||||
self._ts = self.start_ts
|
self._ts = self.start_ts
|
||||||
self._xput = deque(maxlen=self.sma_window)
|
self._xput = deque(maxlen=self.sma_window)
|
||||||
for key, val in kwargs.items():
|
for key, val in kwargs.items():
|
||||||
setattr(self, key, val)
|
setattr(self, key, val)
|
||||||
|
|
||||||
|
self._width = 0
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
if self.hide_cursor:
|
||||||
|
print(HIDE_CURSOR, end='', file=self.file)
|
||||||
|
print(self.message, end='', file=self.file)
|
||||||
|
self.file.flush()
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
if key.startswith('_'):
|
if key.startswith('_'):
|
||||||
return None
|
return None
|
||||||
@@ -44,7 +62,7 @@ class Infinite(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def elapsed(self):
|
def elapsed(self):
|
||||||
return int(time() - self.start_ts)
|
return int(monotonic() - self.start_ts)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def elapsed_td(self):
|
def elapsed_td(self):
|
||||||
@@ -52,8 +70,14 @@ class Infinite(object):
|
|||||||
|
|
||||||
def update_avg(self, n, dt):
|
def update_avg(self, n, dt):
|
||||||
if n > 0:
|
if n > 0:
|
||||||
|
xput_len = len(self._xput)
|
||||||
self._xput.append(dt / n)
|
self._xput.append(dt / n)
|
||||||
self.avg = sum(self._xput) / len(self._xput)
|
now = monotonic()
|
||||||
|
# update when we're still filling _xput, then after every second
|
||||||
|
if (xput_len < self.sma_window or
|
||||||
|
now - self._avg_update_ts > 1):
|
||||||
|
self.avg = sum(self._xput) / len(self._xput)
|
||||||
|
self._avg_update_ts = now
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
pass
|
pass
|
||||||
@@ -61,11 +85,34 @@ class Infinite(object):
|
|||||||
def start(self):
|
def start(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def clearln(self):
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
print('\r\x1b[K', end='', file=self.file)
|
||||||
|
|
||||||
|
def write(self, s):
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
line = self.message + s.ljust(self._width)
|
||||||
|
print('\r' + line, end='', file=self.file)
|
||||||
|
self._width = max(self._width, len(s))
|
||||||
|
self.file.flush()
|
||||||
|
|
||||||
|
def writeln(self, line):
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
self.clearln()
|
||||||
|
print(line, end='', file=self.file)
|
||||||
|
self.file.flush()
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
pass
|
if self.file and self.is_tty():
|
||||||
|
print(file=self.file)
|
||||||
|
if self.hide_cursor:
|
||||||
|
print(SHOW_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
|
def is_tty(self):
|
||||||
|
return self.file.isatty() if self.check_tty else True
|
||||||
|
|
||||||
def next(self, n=1):
|
def next(self, n=1):
|
||||||
now = time()
|
now = monotonic()
|
||||||
dt = now - self._ts
|
dt = now - self._ts
|
||||||
self.update_avg(n, dt)
|
self.update_avg(n, dt)
|
||||||
self._ts = now
|
self._ts = now
|
||||||
@@ -73,12 +120,17 @@ class Infinite(object):
|
|||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def iter(self, it):
|
def iter(self, it):
|
||||||
try:
|
with self:
|
||||||
for x in it:
|
for x in it:
|
||||||
yield x
|
yield x
|
||||||
self.next()
|
self.next()
|
||||||
finally:
|
|
||||||
self.finish()
|
def __enter__(self):
|
||||||
|
self.start()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.finish()
|
||||||
|
|
||||||
|
|
||||||
class Progress(Infinite):
|
class Progress(Infinite):
|
||||||
@@ -119,9 +171,7 @@ class Progress(Infinite):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
with self:
|
||||||
for x in it:
|
for x in it:
|
||||||
yield x
|
yield x
|
||||||
self.next()
|
self.next()
|
||||||
finally:
|
|
||||||
self.finish()
|
|
||||||
|
|||||||
@@ -19,18 +19,15 @@ from __future__ import unicode_literals
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import Progress
|
from . import Progress
|
||||||
from .helpers import WritelnMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Bar(WritelnMixin, Progress):
|
class Bar(Progress):
|
||||||
width = 32
|
width = 32
|
||||||
message = ''
|
|
||||||
suffix = '%(index)d/%(max)d'
|
suffix = '%(index)d/%(max)d'
|
||||||
bar_prefix = ' |'
|
bar_prefix = ' |'
|
||||||
bar_suffix = '| '
|
bar_suffix = '| '
|
||||||
empty_fill = ' '
|
empty_fill = ' '
|
||||||
fill = '#'
|
fill = '#'
|
||||||
hide_cursor = True
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
filled_length = int(self.width * self.progress)
|
filled_length = int(self.width * self.progress)
|
||||||
|
|||||||
@@ -16,27 +16,20 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from . import Infinite, Progress
|
from . import Infinite, Progress
|
||||||
from .helpers import WriteMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Counter(WriteMixin, Infinite):
|
class Counter(Infinite):
|
||||||
message = ''
|
|
||||||
hide_cursor = True
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.write(str(self.index))
|
self.write(str(self.index))
|
||||||
|
|
||||||
|
|
||||||
class Countdown(WriteMixin, Progress):
|
class Countdown(Progress):
|
||||||
hide_cursor = True
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.write(str(self.remaining))
|
self.write(str(self.remaining))
|
||||||
|
|
||||||
|
|
||||||
class Stack(WriteMixin, Progress):
|
class Stack(Progress):
|
||||||
phases = (' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█')
|
phases = (' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█')
|
||||||
hide_cursor = True
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
nphases = len(self.phases)
|
nphases = len(self.phases)
|
||||||
|
|||||||
@@ -1,91 +0,0 @@
|
|||||||
# Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
|
|
||||||
#
|
|
||||||
# Permission to use, copy, modify, and distribute this software for any
|
|
||||||
# purpose with or without fee is hereby granted, provided that the above
|
|
||||||
# copyright notice and this permission notice appear in all copies.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
# 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 print_function
|
|
||||||
|
|
||||||
|
|
||||||
HIDE_CURSOR = '\x1b[?25l'
|
|
||||||
SHOW_CURSOR = '\x1b[?25h'
|
|
||||||
|
|
||||||
|
|
||||||
class WriteMixin(object):
|
|
||||||
hide_cursor = False
|
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
|
||||||
super(WriteMixin, self).__init__(**kwargs)
|
|
||||||
self._width = 0
|
|
||||||
if message:
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
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 and self.file.isatty():
|
|
||||||
b = '\b' * 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 and self.file.isatty() and self.hide_cursor:
|
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
|
||||||
|
|
||||||
|
|
||||||
class WritelnMixin(object):
|
|
||||||
hide_cursor = False
|
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
|
||||||
super(WritelnMixin, self).__init__(**kwargs)
|
|
||||||
if message:
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
if self.file and self.file.isatty() and self.hide_cursor:
|
|
||||||
print(HIDE_CURSOR, end='', file=self.file)
|
|
||||||
|
|
||||||
def clearln(self):
|
|
||||||
if self.file and self.file.isatty():
|
|
||||||
print('\r\x1b[K', end='', file=self.file)
|
|
||||||
|
|
||||||
def writeln(self, line):
|
|
||||||
if self.file and self.file.isatty():
|
|
||||||
self.clearln()
|
|
||||||
print(line, end='', file=self.file)
|
|
||||||
self.file.flush()
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
if self.file and self.file.isatty():
|
|
||||||
print(file=self.file)
|
|
||||||
if self.hide_cursor:
|
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
|
||||||
|
|
||||||
|
|
||||||
from signal import signal, SIGINT
|
|
||||||
from sys import exit
|
|
||||||
|
|
||||||
|
|
||||||
class SigIntMixin(object):
|
|
||||||
"""Registers a signal handler that calls finish on SIGINT"""
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super(SigIntMixin, self).__init__(*args, **kwargs)
|
|
||||||
signal(SIGINT, self._sigint_handler)
|
|
||||||
|
|
||||||
def _sigint_handler(self, signum, frame):
|
|
||||||
self.finish()
|
|
||||||
exit(0)
|
|
||||||
@@ -16,11 +16,9 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from . import Infinite
|
from . import Infinite
|
||||||
from .helpers import WriteMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Spinner(WriteMixin, Infinite):
|
class Spinner(Infinite):
|
||||||
message = ''
|
|
||||||
phases = ('-', '\\', '|', '/')
|
phases = ('-', '\\', '|', '/')
|
||||||
hide_cursor = True
|
hide_cursor = True
|
||||||
|
|
||||||
@@ -40,5 +38,6 @@ class MoonSpinner(Spinner):
|
|||||||
class LineSpinner(Spinner):
|
class LineSpinner(Spinner):
|
||||||
phases = ['⎺', '⎻', '⎼', '⎽', '⎼', '⎻']
|
phases = ['⎺', '⎻', '⎼', '⎽', '⎼', '⎻']
|
||||||
|
|
||||||
|
|
||||||
class PixelSpinner(Spinner):
|
class PixelSpinner(Spinner):
|
||||||
phases = ['⣾','⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
|
phases = ['⣾', '⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
|
||||||
|
|||||||
@@ -27,19 +27,18 @@ for bar_cls in (Bar, ChargingBar, FillingSquaresBar, FillingCirclesBar):
|
|||||||
|
|
||||||
for bar_cls in (IncrementalBar, PixelBar, ShadyBar):
|
for bar_cls in (IncrementalBar, PixelBar, ShadyBar):
|
||||||
suffix = '%(percent)d%% [%(elapsed_td)s / %(eta)d / %(eta_td)s]'
|
suffix = '%(percent)d%% [%(elapsed_td)s / %(eta)d / %(eta_td)s]'
|
||||||
bar = bar_cls(bar_cls.__name__, suffix=suffix)
|
with bar_cls(bar_cls.__name__, suffix=suffix, max=200) as bar:
|
||||||
for i in bar.iter(range(200)):
|
for i in range(200):
|
||||||
sleep()
|
bar.next()
|
||||||
|
sleep()
|
||||||
|
|
||||||
for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
|
for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
|
||||||
for i in spin(spin.__name__ + ' ').iter(range(100)):
|
for i in spin(spin.__name__ + ' ').iter(range(100)):
|
||||||
sleep()
|
sleep()
|
||||||
print()
|
|
||||||
|
|
||||||
for singleton in (Counter, Countdown, Stack, Pie):
|
for singleton in (Counter, Countdown, Stack, Pie):
|
||||||
for i in singleton(singleton.__name__ + ' ').iter(range(100)):
|
for i in singleton(singleton.__name__ + ' ').iter(range(100)):
|
||||||
sleep()
|
sleep()
|
||||||
print()
|
|
||||||
|
|
||||||
bar = IncrementalBar('Random', suffix='%(index)d')
|
bar = IncrementalBar('Random', suffix='%(index)d')
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
|
|||||||
Reference in New Issue
Block a user