def vectorize(f):
"""
Allows a traditionnal python function to be called with
iterables arguments.
So when you call func([array]) it will do the iteration for you,
call func() for each element in array, and return an array.
All arguments must be iterable, if you want to pass something else,
use a keyword argument.
"""
def _vectorize(*args, **kwargs):
if not isiterable(args[0]):
return f(*args, **kwargs)
else:
return [f(*arg, **kwargs) for arg in zip(*args)]
return _vectorize
# to use it:
@vectorize
def my_func(arguments):
# do something interesting, on ONE element.
my_func(my_args)
my_func(my_super_list_of_many_values)
2008-06-26
Auto Vectorize Decorator
Subscribe to:
Posts (Atom)
