當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。