luscenje_struktur/src/progress_bar.py

34 lines
839 B
Python
Raw Permalink Normal View History

import time
2019-06-17 15:30:51 +00:00
try:
from tqdm import tqdm
except ImportError:
tqdm = None
REPORT_ON = 0.3
2019-06-17 15:30:51 +00:00
class Progress:
def __call__(self, iterable, description, total=None):
2019-06-17 15:30:51 +00:00
if tqdm is None:
try:
total = len(iterable)
except TypeError:
total = -1
start_time = time.time()
last_report = start_time - REPORT_ON
for n, el in enumerate(iterable):
now = time.time()
if now - last_report > REPORT_ON:
print("\r{}: {}/{}".format(description, n, total), end="")
last_report = now
2019-06-17 15:30:51 +00:00
yield el
print(" -> {}".format(time.time() - start_time))
2019-06-17 15:30:51 +00:00
else:
yield from tqdm(iterable, desc=description, total=total)
2019-06-17 15:30:51 +00:00
progress = Progress()