try: from tqdm import tqdm except ImportError: tqdm = None class Progress: def __init__(self): self.infile = False def __call__(self, iterable, description, infile=False, outfile=False): show_progress = True if infile and not self.infile: show_progress = False elif outfile and self.infile: show_progress = False if not show_progress: yield from iterable return if tqdm is None: iterlist = list(iterable) proc = -1 for n, el in enumerate(iterlist): nxt_proc = int(n / len(iterlist) * 100) if nxt_proc > proc: print("\r{}: {:02d}% ({}/{})".format(description, nxt_proc, n, len(iterlist)), end="") proc = nxt_proc yield el print("") else: yield from tqdm(iterable, desc=description) def init(self, args): self.infile = not args.hide_inner_progress progress = Progress()