I want to write a wrapper for a generator which checks, if the generator yields anything and (e.g.) raises an exception if not.
I could write:
def my_wrapper(input):
if input is None:
return
found = False
for elem in my_yielding_function(input):
found = True
yield elem
if not found:
raise MyException("Empty Generator")
Is there a more pythonic way to do that?
There is one very similar question but it's more than 10 years old - maybe things have changed?
Context:
Hard to explain - I'm using a given API function which might yield nothing but in this case my function has distinguish from empty input.