Skip to content

Commit 073d234

Browse files
committed
Fix support for iterators using "repeat"
1 parent b110a77 commit 073d234

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

more_itertools/more.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,7 +4198,7 @@ def nth_product(index, *iterables, repeat=1):
41984198
41994199
``IndexError`` will be raised if the given *index* is invalid.
42004200
"""
4201-
pools = tuple(map(tuple, reversed(iterables * repeat)))
4201+
pools = tuple(map(tuple, reversed(iterables))) * repeat
42024202
ns = tuple(map(len, pools))
42034203

42044204
c = reduce(mul, ns)
@@ -4362,7 +4362,7 @@ def product_index(element, *iterables, repeat=1):
43624362
of *args*.
43634363
"""
43644364
elements = tuple(element)
4365-
pools = tuple(map(tuple, iterables * repeat))
4365+
pools = tuple(map(tuple, iterables)) * repeat
43664366
if len(elements) != len(pools):
43674367
raise ValueError('element is not a product of args')
43684368

tests/test_more.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,6 +4864,10 @@ def test_repeat(self):
48644864
mi.nth_product(123, 'AB', 'CD', 'EFG', repeat=2),
48654865
mi.nth_product(123, 'AB', 'CD', 'EFG', 'AB', 'CD', 'EFG'),
48664866
)
4867+
self.assertEqual(
4868+
mi.nth_product(123, iter('AB'), iter('CD'), iter('EFG'), repeat=2),
4869+
mi.nth_product(123, 'AB', 'CD', 'EFG', 'AB', 'CD', 'EFG'),
4870+
)
48674871

48684872

48694873
class NthCombinationWithReplacementTests(TestCase):
@@ -4982,6 +4986,12 @@ def test_repeat(self):
49824986
mi.product_index(target, 'AB', 'CD', 'EFG', repeat=2),
49834987
mi.product_index(target, 'AB', 'CD', 'EFG', 'AB', 'CD', 'EFG'),
49844988
)
4989+
self.assertEqual(
4990+
mi.product_index(
4991+
iter(target), iter('AB'), iter('CD'), iter('EFG'), repeat=2
4992+
),
4993+
mi.product_index(target, 'AB', 'CD', 'EFG', 'AB', 'CD', 'EFG'),
4994+
)
49854995

49864996

49874997
class CombinationIndexTests(TestCase):

0 commit comments

Comments
 (0)