本文整理汇总了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
示例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
示例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
示例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
示例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"
)
示例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
示例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
示例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
示例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