本文整理汇总了Python中schematics.exceptions.ConversionError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ConversionError方法的具体用法?Python exceptions.ConversionError怎么用?Python exceptions.ConversionError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类schematics.exceptions
的用法示例。
在下文中一共展示了exceptions.ConversionError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: monkeypatch_listtype
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def monkeypatch_listtype():
"""
Replace ListType list conversion method to avoid errors
"""
from schematics.transforms import EMPTY_LIST
from schematics.types.compound import ListType
from schematics.exceptions import ConversionError
def _force_list(self, value):
if value is None or value == EMPTY_LIST:
return []
try:
return list(value)
except Exception as e:
raise ConversionError("Invalid list")
ListType._force_list = _force_list
# Apply monkeypatches
示例2: to_native
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def to_native(self, value, context=None):
value = super(HashType, self).to_native(value, context)
if ':' not in value:
raise ValidationError(self.messages['hash_invalid'])
hash_type, hash_value = value.split(':', 1)
if hash_type not in algorithms:
raise ValidationError(self.messages['hash_invalid'])
if len(hash_value) != hash_new(hash_type).digest_size * 2:
raise ValidationError(self.messages['hash_length'])
try:
int(hash_value, 16)
except ValueError:
raise ConversionError(self.messages['hash_hex'])
return value
示例3: to_native
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def to_native(self, value, context=None):
return self._to_name(value, ConversionError)
示例4: _apply_precision
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def _apply_precision(self, value):
try:
value = Decimal(value).quantize(self.precision, rounding=ROUND_HALF_UP).normalize()
except (TypeError, InvalidOperation):
raise ConversionError(self.messages['number_coerce'].format(value))
return value
示例5: to_native
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def to_native(self, value, context=None):
if isinstance(value, (memoryview, bytes)):
return value
if isinstance(value, str):
is_hex = all(c in string.hexdigits for c in value)
if not is_hex:
raise ConversionError(self.messages['hash_hex'])
if len(value) % 2 == 1:
value = '0' + value
return to_bin(value)
示例6: _json_try_convert
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def _json_try_convert(value, err, no_throw=False):
if isinstance(value, (bytes, str)):
try:
return json.loads(value)
except json.JSONDecodeError:
if not no_throw:
raise ConversionError(err)
return value
示例7: to_native
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def to_native(self, value, context=None):
if not value:
return dict()
if not isinstance(value, dict):
try:
value = json.loads(value)
except (AttributeError, TypeError, ValueError):
raise ConversionError(self.messages['convert'].format(value))
return value
示例8: load
# 需要导入模块: from schematics import exceptions [as 别名]
# 或者: from schematics.exceptions import ConversionError [as 别名]
def load(self, model, value):
model = _enforce_type_instance_or_model_class(model)
try:
context = get_import_context(oo=True)
model = self._translate_to_model(model)
result = model(value, context=context)
if isinstance(model, ModelType) or not isinstance(model, BaseType):
result.validate()
else:
model.validate(result, context=context)
return result
except BaseError as e:
raise SerializationException(str(e))
except ConversionError as e:
raise SerializationException(str(e))