mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 06:46:16 +00:00
Merge pull request #88 from ewa/develop
Added optional "every" argument to progress.{bar,dots,mill}
This commit is contained in:
@@ -24,3 +24,4 @@ Patches and Suggestions
|
||||
- Don Spaulding <donspauldingii@gmail.com>
|
||||
- Justin Barber <barber.justin (at) gmail>
|
||||
- Dmitry Medvinsky
|
||||
- Eric Anderson
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ except ImportError:
|
||||
import collections
|
||||
collections.OrderedDict = OrderedDict
|
||||
|
||||
from arguments import *
|
||||
from .arguments import *
|
||||
from . import textui
|
||||
from . import utils
|
||||
from .pipes import piped_in
|
||||
|
||||
+20
-14
@@ -33,7 +33,7 @@ ETA_INTERVAL = 1
|
||||
#How many intervals (excluding the current one) to calculate the simple moving average
|
||||
ETA_SMA_WINDOW = 9
|
||||
|
||||
def bar(it, label='', width=32, hide=HIDE_DEFAULT, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR, expected_size=None):
|
||||
def bar(it, label='', width=32, hide=HIDE_DEFAULT, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR, expected_size=None, every=1):
|
||||
"""Progress iterator. Wrap your iterables with it."""
|
||||
|
||||
def _show(_i):
|
||||
@@ -44,9 +44,11 @@ def bar(it, label='', width=32, hide=HIDE_DEFAULT, empty_char=BAR_EMPTY_CHAR, fi
|
||||
bar.etadisp = time.strftime('%H:%M:%S', time.gmtime(bar.eta))
|
||||
x = int(width*_i/count)
|
||||
if not hide:
|
||||
STREAM.write(BAR_TEMPLATE % (
|
||||
label, filled_char*x, empty_char*(width-x), _i, count, bar.etadisp))
|
||||
STREAM.flush()
|
||||
if ((_i % every)==0 or # True every "every" updates
|
||||
(_i == count)): # And when we're done
|
||||
STREAM.write(BAR_TEMPLATE % (
|
||||
label, filled_char*x, empty_char*(width-x), _i, count, bar.etadisp))
|
||||
STREAM.flush()
|
||||
|
||||
count = len(it) if expected_size is None else expected_size
|
||||
|
||||
@@ -65,11 +67,11 @@ def bar(it, label='', width=32, hide=HIDE_DEFAULT, empty_char=BAR_EMPTY_CHAR, fi
|
||||
_show(i+1)
|
||||
|
||||
if not hide:
|
||||
STREAM.write('\n')
|
||||
STREAM.flush()
|
||||
STREAM.write('\n')
|
||||
STREAM.flush()
|
||||
|
||||
|
||||
def dots(it, label='', hide=HIDE_DEFAULT):
|
||||
def dots(it, label='', hide=HIDE_DEFAULT, every=1):
|
||||
"""Progress iterator. Prints a dot for each item being iterated"""
|
||||
|
||||
count = 0
|
||||
@@ -77,10 +79,11 @@ def dots(it, label='', hide=HIDE_DEFAULT):
|
||||
if not hide:
|
||||
STREAM.write(label)
|
||||
|
||||
for item in it:
|
||||
for (i, item) in enumerate(it):
|
||||
if not hide:
|
||||
STREAM.write(DOTS_CHAR)
|
||||
sys.stderr.flush()
|
||||
if (i % every)==0: # True every "every" updates
|
||||
STREAM.write(DOTS_CHAR)
|
||||
sys.stderr.flush()
|
||||
|
||||
count += 1
|
||||
|
||||
@@ -90,7 +93,7 @@ def dots(it, label='', hide=HIDE_DEFAULT):
|
||||
STREAM.flush()
|
||||
|
||||
|
||||
def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None):
|
||||
def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None, every=1):
|
||||
"""Progress iterator. Prints a mill while iterating over the items."""
|
||||
|
||||
def _mill_char(_i):
|
||||
@@ -101,9 +104,12 @@ def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None):
|
||||
|
||||
def _show(_i):
|
||||
if not hide:
|
||||
STREAM.write(MILL_TEMPLATE % (
|
||||
label, _mill_char(_i), _i, count))
|
||||
STREAM.flush()
|
||||
if ((_i % every)==0 or # True every "every" updates
|
||||
(_i == count)): # And when we're done
|
||||
|
||||
STREAM.write(MILL_TEMPLATE % (
|
||||
label, _mill_char(_i), _i, count))
|
||||
STREAM.flush()
|
||||
|
||||
count = len(it) if expected_size is None else expected_size
|
||||
|
||||
|
||||
Reference in New Issue
Block a user