Exemplo n.º 1
0
    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
Exemplo n.º 2
0
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'}
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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'}
Exemplo n.º 5
0
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'}
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
def test_gather_coroutinefunction():
    results = run_in_loop(gather(partial(coro, 1), partial(coro, 2), limit=1))
    assert results == [2, 4]