Skip to content

Commit fddeb63

Browse files
committed
-
1 parent 4624d17 commit fddeb63

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

source_py3/python_toolbox/cute_iter_tools.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ def _fill(iterable, fill_value, fill_value_maker, length):
331331
yield fill_value_maker()
332332

333333

334+
def call_until_exception(function, exception, lazy_tuple=False):
335+
'''Iterate on values returned from `function` until getting `exception`.'''
336+
iterator = _call_until_exception(function, exception)
337+
if lazy_tuple:
338+
from python_toolbox import nifty_collections # Avoiding circular import.
339+
return nifty_collections.LazyTuple(iterator)
340+
else:
341+
return iterator
342+
343+
344+
def _call_until_exception(function, exception):
345+
from python_toolbox import sequence_tools
346+
exceptions = sequence_tools.to_tuple(exception, item_type=BaseException)
347+
try:
348+
yield function()
349+
except exceptions:
350+
raise StopIteration
351+
334352
def get_single_if_any(iterable,
335353
exception_on_multiple=Exception('More than one value '
336354
'not allowed.')):

0 commit comments

Comments
 (0)