10000 Make the iter_except() recipe more compact. by rhettinger · Pull Request #116132 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Make the iter_except() recipe more compact. #116132

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 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
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
10B58
Diff view
21 changes: 1 addition & 20 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -983,36 +983,17 @@ which incur interpreter overhead.
""" Call a function repeatedly until an exception is raised.

Converts a call-until-exception interface to an iterator interface.
Like builtins.iter(func, sentinel) but uses an exception instead
of a sentinel to end the loop.

Priority queue iterator:
iter_except(functools.partial(heappop, h), IndexError)

Non-blocking dictionary iterator:
iter_except(d.popitem, KeyError)

Non-blocking deque iterator:
iter_except(d.popleft, IndexError)

Non-blocking iterator over a producer Queue:
iter_except(q.get_nowait, Queue.Empty)

Non-blocking set iterator:
iter_except(s.pop, KeyError)

"""
# iter_except(d.popitem, KeyError) --> non-blocking dictionary iterator
try:
if first is not None:
# For database APIs needing an initial call to db.first()
yield first()
while True:
yield func()
except exception:
pass



The following recipes have a more mathematical flavor:

.. testcode::
Expand Down
0