def __init__(self, *args, **kwargs): if not args: raise TypeError('__init__() takes at least 2 arguments (1 given)') func, args = args[0], args[1:] if not callable(func): raise TypeError("Input must be callable") # curry- or functools.partial-like object? Unpack and merge arguments if (hasattr(func, 'func') and hasattr(func, 'args') and hasattr(func, 'keywords') and isinstance(func.args, tuple)): _kwargs = {} if func.keywords: _kwargs.update(func.keywords) _kwargs.update(kwargs) kwargs = _kwargs args = func.args + args func = func.func if kwargs: self._partial = paco.partial(func, *args, **kwargs) else: self._partial = paco.partial(func, *args) self.__doc__ = getattr(func, '__doc__', None) self.__name__ = getattr(func, '__name__', '<curry>') self.__module__ = getattr(func, '__module__', None) self.__qualname__ = getattr(func, '__qualname__', None) self._sigspec = None self._has_unknown_args = None
def test_partial_keyword_params_overwrite(): task = partial(coro, foo='bar', bar='baz') args, kw = run_in_loop(task, bar='foo') assert len(args) == 0 assert len(kw) == 2 assert kw == {'foo': 'bar', 'bar': 'foo'}
def test_partial_variadic_arguments(): task = partial(coro, 1) args, kw = run_in_loop(task, *(2, 3, 4)) assert len(args) == 4 assert len(kw) == 0 assert args == (1, 2, 3, 4)
def test_partial_no_extra_arguments(): task = partial(coro, *(1, 2, 3, 4), foo='bar') args, kw = run_in_loop(task) assert len(args) == 4 assert len(kw) == 1 assert args == (1, 2, 3, 4) assert kw == {'foo': 'bar'}
def test_partial(): task = partial(coro, 1, 2, foo='bar') args, kw = run_in_loop(task, 3, 4, bar='baz') assert len(args) == 4 assert len(kw) == 2 assert args == (1, 2, 3, 4) assert kw == {'foo': 'bar', 'bar': 'baz'}
async def test_memoize_partial(): async def f(x, y=0): return x + y f2 = paco.partial(f, y=1) fm2 = memoize(f2) assert await fm2(3) == await f2(3) assert await fm2(3) == await f2(3)
def test_compose(): task = compose(partial(coro, 1), partial(coro, 2), partial(coro, 3)) now = time.time() assert run_in_loop(task, (0,)) == (0, 3, 2, 1) assert time.time() - now >= 0.3
def test_gather_coroutinefunction(): results = run_in_loop(gather(partial(coro, 1), partial(coro, 2), limit=1)) assert results == [2, 4]