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