Add a mill progress indicator to progress.py

I've created a progress indicator that outputs a "mill" and added it to
the `progress.py` file.

Very simple stuff but its more compact than the other progress bars and
it can be useful when using long labels.
This commit is contained in:
Alejandro Gómez
2012-01-14 19:01:23 +01:00
parent 73a9cd836b
commit a95e803268
3 changed files with 35 additions and 1 deletions
+1
View File
@@ -18,3 +18,4 @@ Patches and Suggestions
- Miguel Araujo <maraujop>
- takluyver
- kracekumar
- Alejandro Gómez <alejandrogomez>
+31
View File
@@ -15,10 +15,12 @@ import sys
STREAM = sys.stderr
BAR_TEMPLATE = '%s[%s%s] %i/%i\r'
MILL_TEMPLATE = '%s %s %i/%i\r'
DOTS_CHAR = '.'
BAR_FILLED_CHAR = '#'
BAR_EMPTY_CHAR = ' '
MILL_CHARS = ['|', '/', '-', '\\']
def bar(it, label='', width=32, hide=False, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR):
"""Progress iterator. Wrap your iterables with it."""
@@ -65,3 +67,32 @@ def dots(it, label='', hide=False):
STREAM.write('\n')
STREAM.flush()
def mill(it, label='', hide=False,):
"""Progress iterator. Prints a mill while iterating over the items."""
def _mill_char(_i):
if _i == 100:
return ' '
else:
return MILL_CHARS[_i % len(MILL_CHARS)]
def _show(_i):
if not hide:
STREAM.write(MILL_TEMPLATE % (
label, _mill_char(_i), _i, count))
STREAM.flush()
count = len(it)
if count:
_show(0)
for i, item in enumerate(it):
yield item
_show(i+1)
if not hide:
STREAM.write('\n')
STREAM.flush()
+3 -1
View File
@@ -17,4 +17,6 @@ if __name__ == '__main__':
for i in progress.dots(range(100)):
sleep(random() * 0.2)
for i in progress.mill(range(100)):
sleep(random() * 0.2)