8000 Small improvements to the itertools docs (GH-123885) · python/cpython@2afba5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2afba5c

Browse files
authored
Small improvements to the itertools docs (GH-123885)
1 parent d359a76 commit 2afba5c

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

Doc/library/itertools.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ loops that truncate the stream.
474474
If *start* is zero or ``None``, iteration starts at zero. Otherwise,
475475
elements from the iterable are skipped until *start* is reached.
476476

477-
If *stop* is ``None``, iteration continues until the iterator is
477+
If *stop* is ``None``, iteration continues until the iterable is
478478
exhausted, if at all. Otherwise, it stops at the specified position.
479479

480480
If *step* is ``None``, the step defaults to one. Elements are returned
@@ -503,6 +503,10 @@ loops that truncate the stream.
503503
yield element
504504
next_i += step
505505

506+
If the input is an iterator, then fully consuming the *islice*
507+
advances the input iterator by ``max(start, stop)`` steps regardless
508+
of the *step* value.
509+
506510

507511
.. function:: pairwise(iterable)
508512

@@ -601,6 +605,8 @@ loops that truncate the stream.
601605
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
602606
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111
603607

608+
if repeat < 0:
609+
raise ValueError('repeat argument cannot be negative')
604610
pools = [tuple(pool) for pool in iterables] * repeat
605611

606612
result = [[]]
@@ -684,6 +690,8 @@ loops that truncate the stream.
684690
Roughly equivalent to::
685691

686692
def tee(iterable, n=2):
693+
if n < 0:
694+
raise ValueError('n must be >= 0')
687695
iterator = iter(iterable)
688696
shared_link = [None, None]
689697
return tuple(_tee(iterator, shared_link) for _ in range(n))
@@ -703,6 +711,12 @@ loops that truncate the stream.
703711
used anywhere else; otherwise, the *iterable* could get advanced without
704712
the tee objects being informed.
705713

714+
When the input *iterable* is already a tee iterator object, all
715+
members of the return tuple are constructed as if they had been
716+
produced by the upstream :func:`tee` call. This "flattening step"
717+
allows nested :func:`tee` calls to share the same underlying data
718+
chain and to have a single update step rather than a chain of calls.
719+
706720
``tee`` iterators are not threadsafe. A :exc:`RuntimeError` may be
707721
raised when simultaneously using iterators returned by the same :func:`tee`
708722
call, even if the original *iterable* is threadsafe.

Lib/test/test_itertools.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,16 @@ def product1(*args, **kwds):
992992
else:
993993
return
994994

995-
def product2(*args, **kwds):
995+
def product2(*iterables, repeat=1):
996996
'Pure python version used in docs'
997-
pools = list(map(tuple, args)) * kwds.get('repeat', 1)
997+
if repeat < 0:
998+
raise ValueError('repeat argument cannot be negative')
999+
pools = [tuple(pool) for pool in iterables] * repeat
1000+
9981001
result = [[]]
9991002
for pool in pools:
10001003
result = [x+[y] for x in result for y in pool]
1004+
10011005
for prod in result:
10021006
yield tuple(prod)
10031007

@@ -1754,6 +1758,8 @@ def test_tee_recipe(self):
17541758
# Begin tee() recipe ###########################################
17551759

17561760
def tee(iterable, n=2):
1761+
if n < 0:
1762+
raise ValueError('n must be >= 0')
17571763
iterator = iter(iterable)
17581764
shared_link = [None, None]
17591765
return tuple(_tee(iterator, shared_link) for _ in range(n))
@@ -1829,11 +1835,9 @@ def _tee(iterator, link):
18291835
self.assertEqual(list(a), list(range(100,2000)))
18301836
self.assertEqual(list(c), list(range(2,2000)))
18311837

1832-
# Tests not applicable to the tee() recipe
1833-
if False:
1834-
# test invalid values of n
1835-
self.assertRaises(TypeError, tee, 'abc', 'invalid')
1836-
self.assertRaises(ValueError, tee, [], -1)
1838+
# test invalid values of n
1839+
self.assertRaises(TypeError, tee, 'abc', 'invalid')
1840+
self.assertRaises(ValueError, tee, [], -1)
18371841

18381842
for n in range(5):
18391843
result = tee('abc', n)

0 commit comments

Comments
 (0)
0