def listify(gen):
"Convert a generator into a function which returns a list"
def patched(*args, **kwargs):
return list(gen(*args, **kwargs))
return patched
@listify
def f(x):
for i in range(x):
yield "item" + str(i)
assert f(5) == "item0 item1 item2 item3 item4".split()
One can also define a ‘dictate’ decorator as follows:
from functools import wraps
def dictate(func):
@wraps(func)
def patched(*args, **kwargs):
return dict(*func(*args, **kwargs))
return patched
def listify(gen):
"Convert a generator into a function which returns a list"
def patched(*args, **kwargs):
return list(gen(*args, **kwargs))
return patched
@listify
def f(x):
for i in range(x):
yield "item" + str(i)
assert f(5) == "item0 item1 item2 item3 item4".split()
One can also define a ‘dictate’ decorator as follows:
from functools import wraps
def dictate(func):
@wraps(func)
def patched(*args, **kwargs):
return dict(*func(*args, **kwargs))
return patched