sknano.core.iter_except(func, exception, first=None)[source][source]

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.


func : callable

exception : Exception

first : callable

for database APIs needing an initial cast to db.first()


priority queue iterator:

>>> # iter_except(functools.partial(heappop, h), IndexError)

non-blocking dict iterator:

>>> # iter_except(d.popitem, KeyError)

non-blocking deque iterator:

>>> # iter_except(d.popleft, IndexError)

loop over a producer Queue:

>>> # iter_except(q.get_nowait, Queue.Empty)

non-blocking set iterator:

>>> # iter_except(s.pop, KeyError)