import time try: from tqdm import tqdm except ImportError: tqdm = None REPORT_ON = 0.3 class Progress: def __call__(self, iterable, description): if tqdm is None: 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 yield el print("") else: yield from tqdm(iterable, desc=description) progress = Progress()