10000 Small improvements to the itertools docs by rhettinger · Pull Request #123885 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Small improvements to the itertools docs #123885

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

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve the fidelity of the tee() rough equivalent
  • Loading branch information
rhettinger committed Sep 8, 2024
commit ac18af9513bd85c15a54adac947abb67a045d2a6
2 changes: 2 additions & 0 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ loops that truncate the stream.
Roughly equivalent to::

def tee(iterable, n=2):
if n < 0:
raise ValueError('n must be >= 0')
iterator = iter(iterable)
shared_link = [None, None]
return tuple(_tee(iterator, shared_link) for _ in range(n))
Expand Down
10 changes: 5 additions & 5 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,8 @@ def test_tee_recipe(self):
# Begin tee() recipe ###########################################

def tee(iterable, n=2):
if n < 0:
raise ValueError('n must be >= 0')
iterator = iter(iterable)
shared_link = [None, None]
return tuple(_tee(iterator, shared_link) for _ in range(n))
Expand Down Expand Up @@ -1829,11 +1831,9 @@ def _tee(iterator, link):
self.assertEqual(list(a), list(range(100,2000)))
self.assertEqual(list(c), list(range(2,2000)))

# Tests not applicable to the tee() recipe
if False:
# test invalid values of n
self.assertRaises(TypeError, tee, 'abc', 'invalid')
self.assertRaises(ValueError, tee, [], -1)
# test invalid values of n
self.assertRaises(TypeError, tee, 'abc', 'invalid')
self.assertRaises(ValueError, tee, [], -1)

for n in range(5):
result = tee('abc', n)
Expand Down
0