mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
Make the iter_except() recipe more compact. (gh-116132)
Only one example is needed
This commit is contained in:
parent
f0df35eeca
commit
a81d9509ee
1 changed files with 1 additions and 20 deletions
|
@ -983,28 +983,10 @@ which incur interpreter overhead.
|
||||||
""" Call a function repeatedly until an exception is raised.
|
""" Call a function repeatedly until an exception is raised.
|
||||||
|
|
||||||
Converts a call-until-exception interface to an iterator interface.
|
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:
|
try:
|
||||||
if first is not None:
|
if first is not None:
|
||||||
# For database APIs needing an initial call to db.first()
|
|
||||||
yield first()
|
yield first()
|
||||||
while True:
|
while True:
|
||||||
yield func()
|
yield func()
|
||||||
|
@ -1012,7 +994,6 @@ which incur interpreter overhead.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
The following recipes have a more mathematical flavor:
|
The following recipes have a more mathematical flavor:
|
||||||
|
|
||||||
.. testcode::
|
.. testcode::
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue