• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python functoolz.curry函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中toolz.functoolz.curry函数的典型用法代码示例。如果您正苦于以下问题:Python curry函数的具体用法?Python curry怎么用?Python curry使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了curry函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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'
开发者ID:AndrewWalker,项目名称:toolz,代码行数:31,代码来源:test_functoolz.py


示例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}))
开发者ID:karansag,项目名称:toolz,代码行数:11,代码来源:test_functoolz.py


示例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
开发者ID:karansag,项目名称:toolz,代码行数:13,代码来源:test_functoolz.py


示例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]
开发者ID:stanzikratel,项目名称:otter,代码行数:31,代码来源:spec.py


示例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))
开发者ID:karansag,项目名称:toolz,代码行数:8,代码来源:test_functoolz.py


示例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)
开发者ID:JNRowe,项目名称:toolz,代码行数:8,代码来源:test_core.py


示例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))
开发者ID:karansag,项目名称:toolz,代码行数:30,代码来源:test_functoolz.py


示例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)
开发者ID:AndrewWalker,项目名称:toolz,代码行数:9,代码来源:test_functoolz.py


示例9: test_curry_wrapped

def test_curry_wrapped():

    def foo(a):
        """
        Docstring
        """
        pass
    curried_foo = curry(foo)
    assert curried_foo.__wrapped__ is foo
开发者ID:AndrewWalker,项目名称:toolz,代码行数:9,代码来源:test_functoolz.py


示例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)
开发者ID:karansag,项目名称:toolz,代码行数:19,代码来源:test_functoolz.py


示例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
开发者ID:JNRowe,项目名称:toolz,代码行数:10,代码来源:test_core.py


示例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
开发者ID:karansag,项目名称:toolz,代码行数:11,代码来源:test_functoolz.py


示例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
开发者ID:marcosptf,项目名称:fedora,代码行数:13,代码来源:test_inspect_args.py


示例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
开发者ID:karansag,项目名称:toolz,代码行数:21,代码来源:test_functoolz.py


示例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
开发者ID:eigenhombre,项目名称:toolz,代码行数:16,代码来源:test_functoolz.py


示例16: test_curry_bad_types

def test_curry_bad_types():
    assert raises(TypeError, lambda: curry(1))
开发者ID:Michael2011,项目名称:code-note,代码行数:2,代码来源:test_functoolz.py


示例17: curry

from toolz.functoolz import curry, known_numargs

from .include import get_include
from ._coiter import coiter
from ._comap import comap
from ._cozip import cozip
from ._emptycoroutine import emptycoroutine


__all__ = [
    'coiter',
    'comap',
    'cozip',
    'emptycoroutine',
    'get_include',
]


known_numargs[comap] = 2
comap = curry(comap)
del curry, known_numargs
开发者ID:pombredanne,项目名称:cotoolz,代码行数:21,代码来源:curried.py


示例18: should_curry

from __future__ import absolute_import

import operator

from toolz.functoolz import curry, num_required_args, has_keywords


def should_curry(f):
    num = num_required_args(f)
    return num is None or num > 1 or num == 1 and has_keywords(f) is not False


locals().update(
    dict((name, curry(f) if should_curry(f) else f)
         for name, f in vars(operator).items() if callable(f)),
)

# Clean up the namespace.
del curry
del num_required_args
del has_keywords
del operator
del should_curry
开发者ID:Michael2011,项目名称:code-note,代码行数:23,代码来源:operator.py


示例19: compose

"""
Format logs based on specification
"""
import json
import math

from toolz.curried import assoc
from toolz.dicttoolz import keyfilter
from toolz.functoolz import compose, curry

from twisted.python.failure import Failure

from otter.log.formatters import LoggingEncoder


_json_len = compose(len, curry(json.dumps, cls=LoggingEncoder))

# Maximum length of entire JSON-formatted event dictionary
event_max_length = 50000


def split_execute_convergence(event, max_length=event_max_length):
    """
    Try to split execute-convergence event out into multiple events if there
    are too many CLB nodes, too many servers, or too many steps.

    The problem is mainly the servers, since they take up the most space.

    Experimentally determined that probably logs cut off at around 75k,
    characters - we're going to limit it to 50k.
开发者ID:stanzikratel,项目名称:otter,代码行数:30,代码来源:spec.py


示例20: test_curry_simple

def test_curry_simple():
    cmul = curry(mul)
    double = cmul(2)
    assert callable(double)
    assert double(10) == 20
开发者ID:JNRowe,项目名称:toolz,代码行数:5,代码来源:test_core.py



注:本文中的toolz.functoolz.curry函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python functoolz.memoize函数代码示例发布时间:2022-05-27
下一篇:
Python dicttoolz.merge函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap