status printer with closure over a mutable variable as per #22

This commit is contained in:
Casper da Costa-Luis 2015-07-16 02:04:40 +01:00 committed by Casper da Costa-Luis
parent ee6c3ce95c
commit 6c0b7541b3
1 changed files with 15 additions and 16 deletions

View File

@ -168,21 +168,20 @@ def format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False,
n_fmt, unit, elapsed_str, rate_fmt) n_fmt, unit, elapsed_str, rate_fmt)
class StatusPrinter(object): def StatusPrinter(file):
""" """
Manage the printing and in-place updating of a line of characters. Manage the printing and in-place updating of a line of characters.
Note that if the string is longer than a line, then in-place updating Note that if the string is longer than a line, then in-place updating
may not work (it will print a new line at each refresh). may not work (it will print a new line at each refresh).
""" """
def __init__(self, file): fp = file
self.file = file last_printed_len = [0]
self.last_printed_len = 0 def print_status(s):
def print_status(self, s):
len_s = len(s) len_s = len(s)
self.file.write('\r' + s + ' '*max(self.last_printed_len - len_s, 0)) fp.write('\r' + s + ' '*max(last_printed_len[0] - len_s, 0))
self.file.flush() fp.flush()
self.last_printed_len = len_s last_printed_len[0] = len_s
return print_status
class tqdm(object): class tqdm(object):
@ -278,7 +277,7 @@ class tqdm(object):
# Initialize the screen printer # Initialize the screen printer
self.sp = StatusPrinter(self.file) self.sp = StatusPrinter(self.file)
if not disable: if not disable:
self.sp.print_status(format_meter( self.sp(format_meter(
0, total, 0, ncols, self.prefix, ascii, unit, unit_scale)) 0, total, 0, ncols, self.prefix, ascii, unit, unit_scale))
# Init the time/iterations counters # Init the time/iterations counters
@ -332,7 +331,7 @@ class tqdm(object):
if delta_it >= miniters: if delta_it >= miniters:
cur_t = time() cur_t = time()
if cur_t - last_print_t >= mininterval: if cur_t - last_print_t >= mininterval:
sp.print_status(format_meter( sp(format_meter(
n, total, cur_t-start_t, ncols, n, total, cur_t-start_t, ncols,
prefix, ascii, unit, unit_scale)) prefix, ascii, unit, unit_scale))
if dynamic_miniters: if dynamic_miniters:
@ -344,12 +343,12 @@ class tqdm(object):
if leave: if leave:
if last_print_n < n: if last_print_n < n:
cur_t = time() cur_t = time()
sp.print_status(format_meter( sp(format_meter(
n, total, cur_t-start_t, ncols, n, total, cur_t-start_t, ncols,
prefix, ascii, unit, unit_scale)) prefix, ascii, unit, unit_scale))
file.write('\n') file.write('\n')
else: else:
sp.print_status('') sp('')
file.write('\r') file.write('\r')
def update(self, n=1): def update(self, n=1):
@ -376,7 +375,7 @@ class tqdm(object):
# We check the counter first, to reduce the overhead of time() # We check the counter first, to reduce the overhead of time()
cur_t = time() cur_t = time()
if cur_t - self.last_print_t >= self.mininterval: if cur_t - self.last_print_t >= self.mininterval:
self.sp.print_status(format_meter( self.sp(format_meter(
self.n, self.total, cur_t-self.start_t, self.ncols, self.n, self.total, cur_t-self.start_t, self.ncols,
self.prefix, self.ascii, self.unit, self.unit_scale)) self.prefix, self.ascii, self.unit, self.unit_scale))
if self.dynamic_miniters: if self.dynamic_miniters:
@ -392,12 +391,12 @@ class tqdm(object):
if self.leave: if self.leave:
if self.last_print_n < self.n: if self.last_print_n < self.n:
cur_t = time() cur_t = time()
self.sp.print_status(format_meter( self.sp(format_meter(
self.n, self.total, cur_t-self.start_t, self.ncols, self.n, self.total, cur_t-self.start_t, self.ncols,
self.prefix, self.ascii, self.unit, self.unit_scale)) self.prefix, self.ascii, self.unit, self.unit_scale))
self.file.write('\n') self.file.write('\n')
else: else:
self.sp.print_status('') self.sp('')
self.file.write('\r') self.file.write('\r')