mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 06:46:16 +00:00
Updated averaging method
Calculates a simple moving average based on the number of iterations done in (1) second intervals over the last (10) seconds
This commit is contained in:
@@ -23,21 +23,34 @@ BAR_FILLED_CHAR = '#'
|
||||
BAR_EMPTY_CHAR = ' '
|
||||
MILL_CHARS = ['|', '/', '-', '\\']
|
||||
|
||||
#How long to wait before recalculating the ETA
|
||||
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=False, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR):
|
||||
"""Progress iterator. Wrap your iterables with it."""
|
||||
|
||||
def _show(_i):
|
||||
ETA = -((start-time.time())/(_i+1)) * (count-_i)
|
||||
ETADisp = time.strftime('%H:%M:%S', time.gmtime(ETA))
|
||||
if (time.time() - bar.etadelta) > ETA_INTERVAL:
|
||||
bar.etadelta = time.time()
|
||||
bar.ittimes = bar.ittimes[-ETA_SMA_WINDOW:]+[-(bar.start-time.time())/(_i+1)]
|
||||
bar.eta = sum(bar.ittimes)/float(len(bar.ittimes)) * (count-_i)
|
||||
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, ETADisp))
|
||||
label, filled_char*x, empty_char*(width-x), _i, count, bar.etadisp))
|
||||
STREAM.flush()
|
||||
|
||||
count = len(it)
|
||||
|
||||
start = time.time()
|
||||
bar.start = time.time()
|
||||
bar.ittimes = []
|
||||
bar.eta = 0
|
||||
bar.etadelta = time.time()
|
||||
bar.etadisp = time.strftime('%H:%M:%S', time.gmtime(bar.eta))
|
||||
|
||||
if count:
|
||||
_show(0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user