本文整理汇总了Python中sqlalchemy.ext.mutable.Mutable.coerce方法的典型用法代码示例。如果您正苦于以下问题:Python Mutable.coerce方法的具体用法?Python Mutable.coerce怎么用?Python Mutable.coerce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.mutable.Mutable
的用法示例。
在下文中一共展示了Mutable.coerce方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: coerce
# 需要导入模块: from sqlalchemy.ext.mutable import Mutable [as 别名]
# 或者: from sqlalchemy.ext.mutable.Mutable import coerce [as 别名]
def coerce(cls, key, value):
"""Given a value, coerce it into the target type.
Can be overridden by custom subclasses to coerce incoming
data into a particular type.
By default, raises ``ValueError``.
This method is called in different scenarios depending on if
the parent class is of type :class:`.Mutable` or of type
:class:`.MutableComposite`. In the case of the former, it is called
for both attribute-set operations as well as during ORM loading
operations. For the latter, it is only called during attribute-set
operations; the mechanics of the :func:`.composite` construct
handle coercion during load operations.
:param key: string name of the ORM-mapped attribute being set.
:param value: the incoming value.
:return: the method should return the coerced value, or raise
``ValueError`` if the coercion cannot be completed.
"""
if value is None:
return None
msg = "Attribute '%s' does not accept objects of type %s"
raise ValueError(msg % (key, type(value)))
示例2: coerce
# 需要导入模块: from sqlalchemy.ext.mutable import Mutable [as 别名]
# 或者: from sqlalchemy.ext.mutable.Mutable import coerce [as 别名]
def coerce(cls, key, value):
"Convert plain dictionaries to MutableDict."
if not isinstance(value, MutableDict):
if isinstance(value, dict):
return MutableDict(value)
# this call will raise ValueError
return Mutable.coerce(key, value)
return value
示例3: coerce
# 需要导入模块: from sqlalchemy.ext.mutable import Mutable [as 别名]
# 或者: from sqlalchemy.ext.mutable.Mutable import coerce [as 别名]
def coerce(cls, index, value):
"""Convert plain list to instance of this class."""
if not isinstance(value, cls):
if isinstance(value, list):
return cls(value)
return Mutable.coerce(index, value)
else:
return value
示例4: coerce
# 需要导入模块: from sqlalchemy.ext.mutable import Mutable [as 别名]
# 或者: from sqlalchemy.ext.mutable.Mutable import coerce [as 别名]
def coerce(cls, key, value):
"Convert plain dictionaries to MutableDict."
if not isinstance(value, MutableDict):
if isinstance(value, dict):
return MutableDict(value)
# this call will raise ValueError
return Mutable.coerce(key, value)
else:
return value