Skip to content

Commit

Permalink
add itertools stub
Browse files Browse the repository at this point in the history
- addresses tqdm#225
  • Loading branch information
casperdcl committed Jan 24, 2020
1 parent f8f06a9 commit eb9377f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tqdm/contrib/itertools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import absolute_import
from tqdm.auto import tqdm as tqdm_auto
from copy import deepcopy
import itertools


def product(*iterables, **tqdm_kwargs):
"""
Equivalent of `itertools.product`.
Parameters
----------
tqdm_class : [default: tqdm.auto.tqdm].
"""
kwargs = deepcopy(tqdm_kwargs)
tqdm_class = kwargs.pop("tqdm_class", tqdm_auto)
try:
lens = list(map(len, iterables))
except AttributeError: # missing py2 len
total = None
except TypeError: # missing py3 len
total = None
else:
total = 1
for i in lens:
total *= i
kwargs.setdefault("total", total)
with tqdm_class(**kwargs) as t:
for i in itertools.product(*iterables):
yield i
t.update()

0 comments on commit eb9377f

Please sign in to comment.