当前位置: 首页>>代码示例>>Python>>正文


Python singledispatch.singledispatch方法代码示例

本文整理汇总了Python中singledispatch.singledispatch方法的典型用法代码示例。如果您正苦于以下问题:Python singledispatch.singledispatch方法的具体用法?Python singledispatch.singledispatch怎么用?Python singledispatch.singledispatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在singledispatch的用法示例。


在下文中一共展示了singledispatch.singledispatch方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: to_dict

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def to_dict(obj, **kwargs):
    """
    Convert an object into dictionary. Uses singledispatch to allow for
    clean extensions for custom class types.

    Reference: https://pypi.python.org/pypi/singledispatch

    :param obj: object instance
    :param kwargs: keyword arguments such as suppress_private_attr,
                   suppress_empty_values, dict_factory
    :return: converted dictionary.
    """

    # if is_related, then iterate attrs.
    if is_model(obj.__class__):
        return related_obj_to_dict(obj, **kwargs)

    # else, return obj directly. register a custom to_dict if you need to!
    #   reference: https://pypi.python.org/pypi/singledispatch
    else:
        return obj 
开发者ID:genomoncology,项目名称:related,代码行数:23,代码来源:functions.py

示例2: import_single_dispatch

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def import_single_dispatch():
    try:
        from functools import singledispatch
    except ImportError:
        singledispatch = None

    if not singledispatch:
        try:
            from singledispatch import singledispatch
        except ImportError:
            pass

    if not singledispatch:
        raise Exception(
            "It seems your python version does not include "
            "functools.singledispatch. Please install the 'singledispatch' "
            "package. More information here: "
            "https://pypi.python.org/pypi/singledispatch"
        )

    return singledispatch


# noqa 
开发者ID:graphql-python,项目名称:graphene-mongo,代码行数:26,代码来源:utils.py

示例3: import_single_dispatch

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def import_single_dispatch():
    try:
        from functools import singledispatch
    except ImportError:
        singledispatch = None

    if not singledispatch:
        try:
            from singledispatch import singledispatch
        except ImportError:
            pass

    if not singledispatch:
        raise Exception(
            "It seems your python version does not include "
            "functools.singledispatch. Please install the 'singledispatch' "
            "package. More information here: "
            "https://pypi.python.org/pypi/singledispatch"
        )

    return singledispatch 
开发者ID:graphql-python,项目名称:graphene-django,代码行数:23,代码来源:utils.py

示例4: is_registered_in_singledispatch_function

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def is_registered_in_singledispatch_function(node):
    """Check if the given function node is a singledispatch function."""

    singledispatch_qnames = (
        'functools.singledispatch',
        'singledispatch.singledispatch'
    )

    if not isinstance(node, astroid.FunctionDef):
        return False

    decorators = node.decorators.nodes if node.decorators else []
    for decorator in decorators:
        # func.register are function calls
        if not isinstance(decorator, astroid.Call):
            continue

        func = decorator.func
        if not isinstance(func, astroid.Attribute) or func.attrname != 'register':
            continue

        try:
            func_def = next(func.expr.infer())
        except astroid.InferenceError:
            continue

        if isinstance(func_def, astroid.FunctionDef):
            return decorated_with(func_def, singledispatch_qnames)

    return False 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:32,代码来源:utils.py

示例5: hash_identifier

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def hash_identifier(identified_with, identifier):
        """Create hash from identifier to be used as a part of user lookup.

        This method is a ``singledispatch`` function. It allows to register
        new implementations for specific authentication middleware classes:

        .. code-block:: python

            from hashlib import sha1

            from graceful.authentication import KeyValueUserStorage, Basic

            @KeyValueUserStorage.hash_identifier.register(Basic)
            def _(identified_with, identifier):
                return ":".join((
                    identifier[0],
                    sha1(identifier[1].encode()).hexdigest(),
                ))

        Args:
            identified_with (str): name of the authentication middleware used
                to identify the user.
            identifier (str): user identifier string

        Return:
            str: hashed identifier string
        """
        if isinstance(identifier, str):
            return identifier
        else:
            raise TypeError(
                "User storage does not support this kind of identifier"
            ) 
开发者ID:swistakm,项目名称:graceful,代码行数:35,代码来源:authentication.py

示例6: methdispatch

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def methdispatch(func):
    dispatcher = singledispatch(func)

    def wrapper(*args, **kw):
        return dispatcher.dispatch(args[1].__class__)(*args, **kw)
    wrapper.register = dispatcher.register
    update_wrapper(wrapper, func)
    return wrapper 
开发者ID:dongweiming,项目名称:web_develop,代码行数:10,代码来源:json_singledispatch.py

示例7: sdmethod

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def sdmethod(meth):
    """
    This is a hack to monkey patch sdproperty to work as expected with instance methods.
    """
    sd = singledispatch(meth)

    def wrapper(obj, *args, **kwargs):
        return sd.dispatch(args[0].__class__)(obj, *args, **kwargs)

    wrapper.register = sd.register
    wrapper.dispatch = sd.dispatch
    wrapper.registry = sd.registry
    wrapper._clear_cache = sd._clear_cache
    functools.update_wrapper(wrapper, meth)
    return wrapper 
开发者ID:SecurityInnovation,项目名称:PGPy,代码行数:17,代码来源:decorators.py

示例8: methdispatch

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def methdispatch(func):
    dispatcher = singledispatch(func)
    def wrapper(*args, **kw):
        return dispatcher.dispatch(args[1].__class__)(*args, **kw)
    wrapper.register = dispatcher.register
    update_wrapper(wrapper, func)
    return wrapper 
开发者ID:whimian,项目名称:pyGeoPressure,代码行数:9,代码来源:utils.py

示例9: methdispatch

# 需要导入模块: import singledispatch [as 别名]
# 或者: from singledispatch import singledispatch [as 别名]
def methdispatch(func):
    dispatcher = singledispatch(func)

    def wrapper(*args, **kw):
        try:
            return dispatcher.dispatch(args[1].__class__)(*args, **kw)
        except IndexError:
            return dispatcher.dispatch(args[0].__class__)(*args, **kw)
    wrapper.register = dispatcher.register
    update_wrapper(wrapper, func)
    return wrapper 
开发者ID:travispavek,项目名称:testrail-python,代码行数:13,代码来源:helper.py


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