Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unpack decorator #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion funcy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .cross import PY2


__all__ = ['decorator', 'wraps', 'unwrap', 'ContextDecorator', 'contextmanager']
__all__ = ['decorator', 'wraps', 'unwrap', 'ContextDecorator', 'contextmanager', 'unpack']


def decorator(deco):
Expand Down Expand Up @@ -313,3 +313,23 @@ def unwrap(func):
raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
memo.add(id_func)
return func


def unpack(func):
"""
The decorator can be used for unpack received arguments.

It can be helpful with :map: function:
>>> def process(foo, bar):
... return foo + bar
>>>
>>> from itertools import product
>>> list(map(unpack(process), product(['foo1', 'foo2'], ['bar1', 'bar2'])))
['foo1bar1', 'foo1bar2', 'foo2bar1', 'foo2bar2']
` """

@wraps(func)
def wrapper(arguments):
return func(*arguments)

return wrapper
8 changes: 8 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ def func(x):

assert double_decorated.__wrapped__ is decorated
assert double_decorated.__original__ is func


def test_unpack():
@unpack
def process(foo, bar):
return foo + bar

assert process(('foo', 'bar')) == 'foobar'