Skip to content

Commit

Permalink
perf: misc optimisations caught by perflint
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Mar 15, 2022
1 parent f3fb54e commit 2527b35
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
27 changes: 14 additions & 13 deletions tqdm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def posix_pipe(fin, fout, delim=b'\\n', buf_size=256,
# return

buf = b''
len_delim = len(delim)
# n = 0
while True:
tmp = fin.read(buf_size)
Expand All @@ -85,17 +86,15 @@ def posix_pipe(fin, fout, delim=b'\\n', buf_size=256,
return # n

while True:
try:
i = tmp.index(delim)
except ValueError:
i = tmp.find(delim)
if i < 0:
buf += tmp
break
else:
fp_write(buf + tmp[:i + len(delim)])
# n += 1
callback(1 if callback_len else (buf + tmp[:i]))
buf = b''
tmp = tmp[i + len(delim):]
fp_write(buf + tmp[:i + len(delim)])
# n += 1
callback(1 if callback_len else (buf + tmp[:i]))
buf = b''
tmp = tmp[i + len_delim:]


# ((opt, type), ... )
Expand Down Expand Up @@ -226,8 +225,9 @@ def main(fp=sys.stderr, argv=None):
raise TqdmKeyError("Can only have one of --bytes --update --update_to")
except Exception:
fp.write("\nError:\n" + help_short)
for i in sys.stdin:
sys.stdout.write(i)
stdin, stdout_write = sys.stdin, sys.stdout.write
for i in stdin:
stdout_write(i)
raise
else:
buf_size = tqdm_args.pop('buf_size', 256)
Expand Down Expand Up @@ -284,6 +284,7 @@ def write(x):
posix_pipe(stdin, stdout, '', buf_size, t.update)
elif delim == b'\\n':
log.debug(tqdm_args)
write = stdout.write
if update or update_to:
with tqdm(**tqdm_args) as t:
if update:
Expand All @@ -293,11 +294,11 @@ def callback(i):
def callback(i):
t.update(numeric(i.decode()) - t.n)
for i in stdin:
stdout.write(i)
write(i)
callback(i)
else:
for i in tqdm(stdin, **tqdm_args):
stdout.write(i)
write(i)
else:
log.debug(tqdm_args)
with tqdm(**tqdm_args) as t:
Expand Down
3 changes: 2 additions & 1 deletion tqdm/contrib/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def product(*iterables, **tqdm_kwargs):
total *= i
kwargs.setdefault("total", total)
with tqdm_class(**kwargs) as t:
for i in itertools.product(*iterables):
it = itertools.product(*iterables)
for i in it:
yield i
t.update()
3 changes: 2 additions & 1 deletion tqdm/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def __init__(self, *args, **kwargs):

def __iter__(self):
try:
for obj in super(tqdm_notebook, self).__iter__():
it = super(tqdm_notebook, self).__iter__()
for obj in it:
# return super(tqdm...) will not catch exception
yield obj
# NB: except ... [ as ...] breaks IPython async KeyboardInterrupt
Expand Down

0 comments on commit 2527b35

Please sign in to comment.