PEP8: Have two space lines before functions. Closes issue #1.

This commit is contained in:
Noam Yorav-Raphael 2014-01-11 23:53:36 +02:00
parent 0bd6dc8401
commit 2ad667d338
1 changed files with 12 additions and 7 deletions

19
tqdm.py
View File

@ -3,6 +3,7 @@ __all__ = ['tqdm', 'trange']
import sys import sys
import time import time
def format_interval(t): def format_interval(t):
mins, s = divmod(int(t), 60) mins, s = divmod(int(t), 60)
h, m = divmod(mins, 60) h, m = divmod(mins, 60)
@ -11,6 +12,7 @@ def format_interval(t):
else: else:
return '%02d:%02d' % (m, s) return '%02d:%02d' % (m, s)
def format_meter(n, total, elapsed): def format_meter(n, total, elapsed):
# n - number of finished iterations # n - number of finished iterations
# total - total number of iterations, or None # total - total number of iterations, or None
@ -34,10 +36,11 @@ def format_meter(n, total, elapsed):
return '|%s| %d/%d %s [elapsed: %s left: %s, %s iters/sec]' % ( return '|%s| %d/%d %s [elapsed: %s left: %s, %s iters/sec]' % (
bar, n, total, percentage, elapsed_str, left_str, rate) bar, n, total, percentage, elapsed_str, left_str, rate)
else: else:
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, file): def __init__(self, file):
self.file = file self.file = file
@ -48,6 +51,7 @@ class StatusPrinter(object):
self.file.flush() self.file.flush()
self.last_printed_len = len(s) self.last_printed_len = len(s)
def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr, def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr,
mininterval=0.5, miniters=1): mininterval=0.5, miniters=1):
""" """
@ -59,8 +63,8 @@ def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr,
'total' can give the number of expected iterations. If not given, 'total' can give the number of expected iterations. If not given,
len(iterable) is used if it is defined. len(iterable) is used if it is defined.
'file' can be a file-like object to output the progress message to. 'file' can be a file-like object to output the progress message to.
If leave is False, tqdm deletes its traces from screen after it has finished If leave is False, tqdm deletes its traces from screen after it has
iterating over all elements. finished 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.
""" """
@ -69,12 +73,12 @@ def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr,
total = len(iterable) total = len(iterable)
except TypeError: except TypeError:
total = None total = None
prefix = desc+': ' if desc else '' prefix = desc+': ' if desc else ''
sp = StatusPrinter(file) sp = StatusPrinter(file)
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()
last_print_n = 0 last_print_n = 0
n = 0 n = 0
@ -83,7 +87,7 @@ def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr,
# Now the object was created and processed, so we can print the meter. # Now the object was created and processed, so we can print the meter.
n += 1 n += 1
if n - last_print_n >= miniters: if n - last_print_n >= miniters:
# We check the counter first, to reduce the overhead of time.time(). # We check the counter first, to reduce the overhead of time.time()
cur_t = time.time() cur_t = time.time()
if cur_t - last_print_t >= mininterval: if cur_t - last_print_t >= mininterval:
sp.print_status(prefix + format_meter(n, total, cur_t-start_t)) sp.print_status(prefix + format_meter(n, total, cur_t-start_t))
@ -98,11 +102,12 @@ def tqdm(iterable, desc='', total=None, leave=False, file=sys.stderr,
cur_t = time.time() cur_t = time.time()
sp.print_status(prefix + format_meter(n, total, cur_t-start_t)) sp.print_status(prefix + format_meter(n, total, cur_t-start_t))
def trange(*args, **kwargs): def trange(*args, **kwargs):
"""A shortcut for writing tqdm(range()) on py3 or tqdm(xrange()) on py2""" """A shortcut for writing tqdm(range()) on py3 or tqdm(xrange()) on py2"""
try: try:
f = xrange f = xrange
except NameError: except NameError:
f = range f = range
return tqdm(f(*args), **kwargs) return tqdm(f(*args), **kwargs)