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