Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError thrown if no iterables are sized #27

Closed
cthoyt opened this issue Sep 15, 2020 · 0 comments · Fixed by #28
Closed

ValueError thrown if no iterables are sized #27

cthoyt opened this issue Sep 15, 2020 · 0 comments · Fixed by #28

Comments

@cthoyt
Copy link
Contributor

cthoyt commented Sep 15, 2020

The following code throws a ValueError:

from p_tqdm import p_uimap
def increment(x):
    return x + 1
it = (i for i in range(5))  # don't use range directly, because it is Sized
for x in p_uimap(f, it):
    print(x)

the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cthoyt/.virtualenvs/integrator/lib/python3.8/site-packages/p_tqdm/p_tqdm.py", line 42, in _parallel
    length = min(len(iterable) for iterable in iterables if isinstance(iterable, Sized))
ValueError: min() arg is an empty sequence

This happens because there are no sized iterables, and it's trying to take the min() of an empty sequence. This could be solved a few ways:

  1. Surrounding this line with try/except, then setting length=None. This is an optional argument to tqdm() so this is okay, but will not longer be able to give an estimate
try:
    # Determine length of tqdm (equal to length of shortest iterable)
    length = min(len(iterable) for iterable in iterables if isinstance(iterable, Sized))
except ValueError:
    length = None
  1. Save the list of iterables (this won't be so long) that are sized and check it explicitly for not being empty. If it is, set length=None
# Determine length of tqdm (equal to length of shortest iterable), if possible
lengths = [len(iterable) for iterable in iterables if isinstance(iterable, Sized)]
length = min(lengths) if lengths else None

I'm not sure which you would prefer, but they effectively accomplish the same thing. I made a PR #28 that uses the second solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant