I have a bunch of code with the same pattern:
def process(iterable):
if inspect.isasyncgen(iterable):
return process_async(iterable)
else:
return process_normal(iterable)
def process_normal(iterable):
for i in iterable:
do_something
yield something
async def process_async(iterable):
async for i in iterable:
do_something
yield something
This allows me to process normal and async iterables with the same code, as process() returns the same type of iterable as it gets. However, when the processing code inside the loops is non-trivial, this leads to ugly code duplication which is not always possible to extract, so I wonder, is it possible to write this more concisely, or maybe replace some of this with some stdlib things?
from How can I write a higher-order generator generic over synchronousness?
No comments:
Post a Comment