本文整理汇总了Python中toolz.functoolz.curry函数的典型用法代码示例。如果您正苦于以下问题:Python curry函数的具体用法?Python curry怎么用?Python curry使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了curry函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_curry_kwargs
def test_curry_kwargs():
def f(a, b, c=10):
return (a + b) * c
f = curry(f)
assert f(1, 2, 3) == 9
assert f(1)(2, 3) == 9
assert f(1, 2) == 30
assert f(1, c=3)(2) == 9
assert f(c=3)(1, 2) == 9
def g(a=1, b=10, c=0):
return a + b + c
cg = curry(g, b=2)
assert cg() == 3
assert cg(b=3) == 4
assert cg(a=0) == 2
assert cg(a=0, b=1) == 1
assert cg(0) == 2 # pass "a" as arg, not kwarg
assert raises(TypeError, lambda: cg(1, 2)) # pass "b" as arg AND kwarg
def h(x, func=int):
return func(x)
if platform.python_implementation() != 'PyPy'\
or platform.python_version_tuple()[0] != '3': # Bug on PyPy3<2.5
# __init__ must not pick func as positional arg
assert curry(h)(0.0) == 0
assert curry(h)(func=str)(0.0) == '0.0'
assert curry(h, func=str)(0.0) == '0.0'
示例2: test_curry_simple
def test_curry_simple():
cmul = curry(mul)
double = cmul(2)
assert callable(double)
assert double(10) == 20
assert repr(cmul) == repr(mul)
cmap = curry(map)
assert list(cmap(inc)([1, 2, 3])) == [2, 3, 4]
assert raises(TypeError, lambda: curry({1: 2}))
示例3: test_curry_is_idempotent
def test_curry_is_idempotent():
def foo(a, b, c=1):
return a + b + c
f = curry(foo, 1, c=2)
g = curry(f)
assert isinstance(f, curry)
assert isinstance(g, curry)
assert not isinstance(g.func, curry)
assert not hasattr(g.func, 'func')
assert f.func == g.func
assert f.args == g.args
assert f.keywords == g.keywords
示例4: split_cf_messages
def split_cf_messages(format_message, var_length_key, event, separator=', ',
max_length=255):
"""
Try to split cloud feed log events out into multiple events if the message
is too long (the variable-length variable would cause the message to be
too long.)
:param str format_message: The format string to use to format the event
:param str var_length_key: The key in the event dictionary that contains
the variable-length part of the formatted message.
:param dict event: The event dictionary
:param str separator: The separator to use to join the various elements
that should be varied. (e.g. if the elements in "var_length_key" are
["1", "2", "3"] and the separator is "; ", "var_length_key" will be
represented as "1; 2; 3")
:param int max_length: The maximum length of the formatted message.
:return: `list` of event dictionaries with the formatted message and
the split event field.
"""
def length_calc(e):
return len(format_message.format(**e))
render = compose(assoc(event, var_length_key), separator.join,
curry(map, str))
if length_calc(event) <= max_length:
return [(render(event[var_length_key]), format_message)]
events = split(render, event[var_length_key], max_length, length_calc)
return [(e, format_message) for e in events]
示例5: test_curry_attributes_readonly
def test_curry_attributes_readonly():
def foo(a, b, c=1):
return a + b + c
f = curry(foo, 1, c=2)
assert raises(AttributeError, lambda: setattr(f, 'args', (2,)))
assert raises(AttributeError, lambda: setattr(f, 'keywords', {'c': 3}))
assert raises(AttributeError, lambda: setattr(f, 'func', f))
示例6: test_curry_docstring
def test_curry_docstring():
def f(x, y):
""" A docstring """
return x
g = curry(f)
assert g.__doc__ == f.__doc__
assert str(g) == str(f)
示例7: test_curry_comparable
def test_curry_comparable():
def foo(a, b, c=1):
return a + b + c
f1 = curry(foo, 1, c=2)
f2 = curry(foo, 1, c=2)
g1 = curry(foo, 1, c=3)
h1 = curry(foo, c=2)
h2 = h1(c=2)
h3 = h1()
assert f1 == f2
assert not (f1 != f2)
assert f1 != g1
assert not (f1 == g1)
assert f1 != h1
assert h1 == h2
assert h1 == h3
# test function comparison works
def bar(a, b, c=1):
return a + b + c
b1 = curry(bar, 1, c=2)
assert b1 != f1
assert set([f1, f2, g1, h1, h2, h3, b1, b1()]) == set([f1, g1, h1, b1])
# test unhashable input
unhash1 = curry(foo, [])
assert raises(TypeError, lambda: hash(unhash1))
unhash2 = curry(foo, c=[])
assert raises(TypeError, lambda: hash(unhash2))
示例8: test_curry_doesnot_transmogrify
def test_curry_doesnot_transmogrify():
# Early versions of `curry` transmogrified to `partial` objects if
# only one positional argument remained even if keyword arguments
# were present. Now, `curry` should always remain `curry`.
def f(x, y=0):
return x + y
cf = curry(f)
assert cf(y=1)(y=2)(y=3)(1) == f(1, 3)
示例9: test_curry_wrapped
def test_curry_wrapped():
def foo(a):
"""
Docstring
"""
pass
curried_foo = curry(foo)
assert curried_foo.__wrapped__ is foo
示例10: test_curry_is_like_partial
def test_curry_is_like_partial():
def foo(a, b, c=1):
return a + b + c
p, c = partial(foo, 1, c=2), curry(foo)(1, c=2)
assert p.keywords == c.keywords
assert p.args == c.args
assert p(3) == c(3)
p, c = partial(foo, 1), curry(foo)(1)
assert p.keywords == c.keywords
assert p.args == c.args
assert p(3) == c(3)
assert p(3, c=2) == c(3, c=2)
p, c = partial(foo, c=1), curry(foo)(c=1)
assert p.keywords == c.keywords
assert p.args == c.args
assert p(1, 2) == c(1, 2)
示例11: test_curry_kwargs
def test_curry_kwargs():
def f(a, b, c=10):
return (a + b) * c
f = curry(f)
assert f(1, 2, 3) == 9
assert f(1)(2, 3) == 9
assert f(1, 2) == 30
assert f(1, c=3)(2) == 9
assert f(c=3)(1, 2) == 9
示例12: test_curry_attributes_writable
def test_curry_attributes_writable():
def foo(a, b, c=1):
return a + b + c
f = curry(foo, 1, c=2)
f.__name__ = 'newname'
f.__doc__ = 'newdoc'
assert f.__name__ == 'newname'
assert f.__doc__ == 'newdoc'
if hasattr(f, 'func_name'):
assert f.__name__ == f.func_name
示例13: check_curry
def check_curry(func, args, kwargs, incomplete=True):
try:
curry(func)(*args, **kwargs)
curry(func, *args)(**kwargs)
curry(func, **kwargs)(*args)
curry(func, *args, **kwargs)()
if not isinstance(func, type(lambda: None)):
return None
return incomplete
except ValueError:
return True
except TypeError:
return False
示例14: test_curry_kwargs
def test_curry_kwargs():
def f(a, b, c=10):
return (a + b) * c
f = curry(f)
assert f(1, 2, 3) == 9
assert f(1)(2, 3) == 9
assert f(1, 2) == 30
assert f(1, c=3)(2) == 9
assert f(c=3)(1, 2) == 9
def g(a=1, b=10, c=0):
return a + b + c
cg = curry(g, b=2)
assert cg() == 3
assert cg(b=3) == 4
assert cg(a=0) == 2
assert cg(a=0, b=1) == 1
assert cg(0) == 2 # pass "a" as arg, not kwarg
assert raises(TypeError, lambda: cg(1, 2)) # pass "b" as arg AND kwarg
示例15: test_curry_attributes_writable
def test_curry_attributes_writable():
def foo(a, b, c=1):
return a + b + c
foo.__qualname__ = 'this.is.foo'
f = curry(foo, 1, c=2)
assert f.__qualname__ == 'this.is.foo'
f.__name__ = 'newname'
f.__doc__ = 'newdoc'
f.__module__ = 'newmodule'
f.__qualname__ = 'newqualname'
assert f.__name__ == 'newname'
assert f.__doc__ == 'newdoc'
assert f.__module__ == 'newmodule'
assert f.__qualname__ == 'newqualname'
if hasattr(f, 'func_name'):
assert f.__name__ == f.func_name