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

add keyword 'repeat' for tqdm.contrib.itertools.product #1552

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/tests_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ def test_product():
assert list(product(a, a[::-1], file=our_file)) == list(it.product(a, a[::-1]))

assert list(product(a, NoLenIter(a), file=our_file)) == list(it.product(a, NoLenIter(a)))

assert list(product(a, repeat=2, file=our_file)) == list(it.product(a, repeat=2))
4 changes: 2 additions & 2 deletions tqdm/contrib/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ['product']


def product(*iterables, **tqdm_kwargs):
def product(*iterables, repeat=1, **tqdm_kwargs):
"""
Equivalent of `itertools.product`.

Expand All @@ -29,7 +29,7 @@ def product(*iterables, **tqdm_kwargs):
total *= i

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from some minor differences (in the tests for example), I only found one difference that seems to matter. I believe that the influence of actually using the repeat parameter is not yet reflected in the total in your version. On my branch I do this to get a correct total:

Suggested change
total *= i
total *= i
total = total ** repeat

Without this fix I believe the progress bar will exceed 100% (after which it just starts displaying the amount of iterations).

kwargs.setdefault("total", total)
with tqdm_class(**kwargs) as t:
it = itertools.product(*iterables)
it = itertools.product(*iterables, repeat=repeat)
for i in it:
yield i
t.update()