From 6e32222d3cac9bd79535f5e347fc2262b0760518 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Fri, 8 Aug 2014 23:33:30 -0400 Subject: [PATCH 1/2] Display elapsed time when the progress bar is complete --- clint/textui/progress.py | 76 ++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/clint/textui/progress.py b/clint/textui/progress.py index 998bb92..f44c563 100644 --- a/clint/textui/progress.py +++ b/clint/textui/progress.py @@ -21,26 +21,28 @@ except AttributeError: # output does not support isatty() HIDE_DEFAULT = True BAR_TEMPLATE = '%s[%s%s] %i/%i - %s\r' -MILL_TEMPLATE = '%s %s %i/%i\r' +MILL_TEMPLATE = '%s %s %i/%i\r' DOTS_CHAR = '.' BAR_FILLED_CHAR = '#' BAR_EMPTY_CHAR = ' ' MILL_CHARS = ['|', '/', '-', '\\'] -#How long to wait before recalculating the ETA +# How long to wait before recalculating the ETA ETA_INTERVAL = 1 -#How many intervals (excluding the current one) to calculate the simple moving average +# How many intervals (excluding the current one) to calculate the simple moving +# average ETA_SMA_WINDOW = 9 + class Bar(object): def __enter__(self): return self - + def __exit__(self, exc_type, exc_val, exc_tb): self.done() - return False # we're not surpressing exceptions - + return False # we're not suppressing exceptions + def __init__(self, label='', width=32, hide=None, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR, expected_size=None, every=1): self.label = label @@ -50,7 +52,7 @@ class Bar(object): try: self.hide = not STREAM.isatty() except AttributeError: # output does not support isatty() - self.hide = True + self.hide = True self.empty_char = empty_char self.filled_char = filled_char self.expected_size = expected_size @@ -59,52 +61,65 @@ class Bar(object): self.ittimes = [] self.eta = 0 self.etadelta = time.time() - self.etadisp = time.strftime('%H:%M:%S', time.gmtime(self.eta)) + self.etadisp = self.format_time(self.eta) + self.last_progress = 0 if (self.expected_size): self.show(0) - + def show(self, progress, count=None): if count is not None: self.expected_size = count if self.expected_size is None: raise Exception("expected_size not initialized") + self.last_progress = progress if (time.time() - self.etadelta) > ETA_INTERVAL: self.etadelta = time.time() self.ittimes = \ - self.ittimes[-ETA_SMA_WINDOW:]+\ - [-(self.start-time.time())/(progress+1)] + self.ittimes[-ETA_SMA_WINDOW:] + \ + [-(self.start - time.time()) / (progress+1)] self.eta = \ - sum(self.ittimes)/float(len(self.ittimes)) * \ - (self.expected_size-progress) - self.etadisp = time.strftime('%H:%M:%S', time.gmtime(self.eta)) - x = int(self.width*progress/self.expected_size) + sum(self.ittimes) / float(len(self.ittimes)) * \ + (self.expected_size - progress) + self.etadisp = self.format_time(self.eta) + x = int(self.width * progress / self.expected_size) if not self.hide: - if ((progress % self.every)==0 or # True every "every" updates - (progress == self.expected_size)): # And when we're done + if ((progress % self.every) == 0 or # True every "every" updates + (progress == self.expected_size)): # And when we're done STREAM.write(BAR_TEMPLATE % ( - self.label, self.filled_char*x, - self.empty_char*(self.width-x), progress, + self.label, self.filled_char * x, + self.empty_char * (self.width - x), progress, self.expected_size, self.etadisp)) STREAM.flush() def done(self): + self.elapsed = time.time() - self.start + elapsed_disp = self.format_time(self.elapsed) if not self.hide: + # Print completed bar with elapsed time + STREAM.write(BAR_TEMPLATE % ( + self.label, self.filled_char * self.width, + self.empty_char * 0, self.last_progress, + self.expected_size, elapsed_disp)) STREAM.write('\n') STREAM.flush() -def bar(it, label='', width=32, hide=HIDE_DEFAULT, empty_char=BAR_EMPTY_CHAR, filled_char=BAR_FILLED_CHAR, expected_size=None, every=1): + def format_time(self, seconds): + return time.strftime('%H:%M:%S', time.gmtime(seconds)) + + +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.""" count = len(it) if expected_size is None else expected_size - - with Bar(label=label, width=width, hide=hide, empty_char=BAR_EMPTY_CHAR, - filled_char=BAR_FILLED_CHAR, expected_size=count, every=every) \ - as bar: + with Bar(label=label, width=width, hide=hide, empty_char=BAR_EMPTY_CHAR, + filled_char=BAR_FILLED_CHAR, expected_size=count, every=every) \ + as bar: for i, item in enumerate(it): - yield item - bar.show(i+1) + bar.show(i + 1) + def dots(it, label='', hide=HIDE_DEFAULT, every=1): """Progress iterator. Prints a dot for each item being iterated""" @@ -114,9 +129,9 @@ def dots(it, label='', hide=HIDE_DEFAULT, every=1): if not hide: STREAM.write(label) - for (i, item) in enumerate(it): + for i, item in enumerate(it): if not hide: - if (i % every)==0: # True every "every" updates + if i % every == 0: # True every "every" updates STREAM.write(DOTS_CHAR) sys.stderr.flush() @@ -139,7 +154,7 @@ def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None, every=1): def _show(_i): if not hide: - if ((_i % every)==0 or # True every "every" updates + if ((_i % every) == 0 or # True every "every" updates (_i == count)): # And when we're done STREAM.write(MILL_TEMPLATE % ( @@ -152,9 +167,8 @@ def mill(it, label='', hide=HIDE_DEFAULT, expected_size=None, every=1): _show(0) for i, item in enumerate(it): - yield item - _show(i+1) + _show(i + 1) if not hide: STREAM.write('\n') From e93a6a5ee4579b885085a4a841bfa53170affdfe Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Fri, 8 Aug 2014 23:46:31 -0400 Subject: [PATCH 2/2] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 4fe6b97..af8b8e7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -30,3 +30,4 @@ Patches and Suggestions - Dmitry Medvinsky - Eric Anderson - Joshua Richardson +- Brandon Liu