42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
try:
|
|
from tqdm import tqdm
|
|
except ImportError:
|
|
tqdm = None
|
|
|
|
|
|
class Progress:
|
|
def __init__(self):
|
|
self.hide_inner = False
|
|
|
|
|
|
def __call__(self, iterable, description, infile=False, outfile=False):
|
|
show_progress = True
|
|
if True in (infile, outfile):
|
|
assert False in (infile, outfile)
|
|
show_progress = outfile == self.hide_inner
|
|
|
|
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.hide_inner = args.hide_inner_progress
|
|
|
|
|
|
progress = Progress()
|
|
|