mirror of https://github.com/tqdm/tqdm.git
Merge branch 'output_to_arbitrary' of https://github.com/Auv5/tqdm into Auv5-output_to_arbitrary
Conflicts: tqdm.py
This commit is contained in:
commit
4a2ab5b8af
13
tqdm.py
13
tqdm.py
|
@ -39,15 +39,16 @@ def format_meter(n, total, elapsed):
|
||||||
return '%d [elapsed: %s, %s iters/sec]' % (n, elapsed_str, rate)
|
return '%d [elapsed: %s, %s iters/sec]' % (n, elapsed_str, rate)
|
||||||
|
|
||||||
class StatusPrinter(object):
|
class StatusPrinter(object):
|
||||||
def __init__(self):
|
def __init__(self, output_to=sys.stdout):
|
||||||
|
self.output_to = output_to
|
||||||
self.last_printed_len = 0
|
self.last_printed_len = 0
|
||||||
|
|
||||||
def print_status(self, s):
|
def print_status(self, s):
|
||||||
sys.stdout.write('\r'+s+' '*max(self.last_printed_len-len(s), 0))
|
self.output_to.write('\r'+s+' '*max(self.last_printed_len-len(s), 0))
|
||||||
sys.stdout.flush()
|
self.output_to.flush()
|
||||||
self.last_printed_len = len(s)
|
self.last_printed_len = len(s)
|
||||||
|
|
||||||
def tqdm(iterable, desc='', total=None, leave=False, mininterval=0.5, miniters=1):
|
def tqdm(iterable, desc='', total=None, leave=False, mininterval=0.5, miniters=1, output_to=sys.stdout):
|
||||||
"""
|
"""
|
||||||
Get an iterable object, and return an iterator which acts exactly like the
|
Get an iterable object, and return an iterator which acts exactly like the
|
||||||
iterable, but prints a progress meter and updates it every time a value is
|
iterable, but prints a progress meter and updates it every time a value is
|
||||||
|
@ -60,6 +61,8 @@ def tqdm(iterable, desc='', total=None, leave=False, mininterval=0.5, miniters=1
|
||||||
iterating over all elements.
|
iterating over all elements.
|
||||||
If less than mininterval seconds or miniters iterations have passed since
|
If less than mininterval seconds or miniters iterations have passed since
|
||||||
the last progress meter update, it is not updated again.
|
the last progress meter update, it is not updated again.
|
||||||
|
'output_to' can be a file-like object to output the progress message to
|
||||||
|
If not specified, prints to sys.stdout.
|
||||||
"""
|
"""
|
||||||
if total is None:
|
if total is None:
|
||||||
try:
|
try:
|
||||||
|
@ -69,7 +72,7 @@ def tqdm(iterable, desc='', total=None, leave=False, mininterval=0.5, miniters=1
|
||||||
|
|
||||||
prefix = desc+': ' if desc else ''
|
prefix = desc+': ' if desc else ''
|
||||||
|
|
||||||
sp = StatusPrinter()
|
sp = StatusPrinter(output_to)
|
||||||
sp.print_status(prefix + format_meter(0, total, 0))
|
sp.print_status(prefix + format_meter(0, total, 0))
|
||||||
|
|
||||||
start_t = last_print_t = time.time()
|
start_t = last_print_t = time.time()
|
||||||
|
|
Loading…
Reference in New Issue