本文整理汇总了Python中voluptuous.Coerce方法的典型用法代码示例。如果您正苦于以下问题:Python voluptuous.Coerce方法的具体用法?Python voluptuous.Coerce怎么用?Python voluptuous.Coerce使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类voluptuous
的用法示例。
在下文中一共展示了voluptuous.Coerce方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_pagination_options
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def get_pagination_options(params, default):
try:
opts = voluptuous.Schema({
voluptuous.Required(
"limit", default=pecan.request.conf.api.max_limit):
voluptuous.All(voluptuous.Coerce(int),
voluptuous.Range(min=1),
voluptuous.Clamp(
min=1, max=pecan.request.conf.api.max_limit)),
"marker": six.text_type,
voluptuous.Required("sort", default=default):
voluptuous.All(
voluptuous.Coerce(arg_to_list),
[six.text_type]),
}, extra=voluptuous.REMOVE_EXTRA)(params)
except voluptuous.Invalid as e:
abort(400, {"cause": "Argument value error",
"reason": str(e)})
opts['sorts'] = opts['sort']
del opts['sort']
return opts
示例2: test_typeDetection
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def test_typeDetection(self):
"""Ensure some of the type inference operations work."""
listSetting = setting.Setting(
"aList",
default=[],
label="label",
description="desc",
schema=vol.Schema([float]),
)
self.assertEqual(listSetting.containedType, float)
listSetting = setting.Setting(
"aList",
default=[],
label="label",
description="desc",
schema=vol.Schema([vol.Coerce(float)]),
)
self.assertEqual(listSetting.containedType, float)
示例3: _setSchema
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def _setSchema(self, schema):
"""Apply or auto-derive schema of the value."""
if schema:
self.schema = schema
elif self.options and self.enforcedOptions:
self.schema = vol.Schema(vol.In(self.options))
else:
# Coercion is needed to convert XML-read migrations (for old cases)
# as well as in some GUI instances where lists are getting set
# as strings.
if isinstance(self.default, list) and self.default:
# Non-empty default: assume the default has the desired contained type
# Coerce all values to the first entry in the default so mixed floats and ints work.
# Note that this will not work for settings that allow mixed
# types in their lists (e.g. [0, '10R']), so those all need custom schemas.
self.schema = vol.Schema([vol.Coerce(type(self.default[0]))])
else:
self.schema = vol.Schema(vol.Coerce(type(self.default)))
示例4: post
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def post(self):
enforce("create archive policy", {})
# NOTE(jd): Initialize this one at run-time because we rely on conf
conf = pecan.request.conf
valid_agg_methods = list(
archive_policy.ArchivePolicy.VALID_AGGREGATION_METHODS_VALUES
)
ArchivePolicySchema = voluptuous.Schema({
voluptuous.Required("name"): six.text_type,
voluptuous.Required("back_window", default=0): voluptuous.All(
voluptuous.Coerce(int),
voluptuous.Range(min=0),
),
voluptuous.Required(
"aggregation_methods",
default=list(conf.archive_policy.default_aggregation_methods)):
valid_agg_methods,
voluptuous.Required("definition"): ArchivePolicyDefinitionSchema,
})
body = deserialize_and_validate(ArchivePolicySchema)
# Validate the data
try:
ap = archive_policy.ArchivePolicy.from_dict(body)
except ValueError as e:
abort(400, six.text_type(e))
enforce("create archive policy", ap)
try:
ap = pecan.request.indexer.create_archive_policy(ap)
except indexer.ArchivePolicyAlreadyExists as e:
abort(409, six.text_type(e))
location = "/archive_policy/" + ap.name
set_resp_location_hdr(location)
pecan.response.status = 201
return ap
示例5: get_schema
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def get_schema(cls):
return voluptuous.All(cls.validator, voluptuous.Coerce(cls))
示例6: containedType
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def containedType(self):
"""The subtype for lists."""
# assume schema set to [int] or [str] or something similar
try:
containedSchema = self.schema.schema[0]
if isinstance(containedSchema, vol.Coerce):
# special case for Coerce objects, which
# store their underlying type as ``.type``.
return containedSchema.type
return containedSchema
except TypeError:
# cannot infer. fall back to str
return str
示例7: test_can_validate_and_coerce_multiple_provisioner_schemas
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def test_can_validate_and_coerce_multiple_provisioner_schemas(self, mock_Provisioner):
mock_Provisioner.provisioners = {
'mp1': MockProvisioner1,
'mp2': MockProvisioner2,
'mp3': MockProvisioner3}
schema = get_schema()
validated = schema({
'name': 'dummy-test',
'provisioning': [{
'type': 'mp1',
'a': 'dummy',
'b': '16'
}, {
'type': 'mp2',
'a': 'dummy',
}, {
'type': 'mp3',
'b': 'yes'
}]
})
assert validated == {
'name': 'dummy-test',
'provisioning': [{
'type': 'mp1',
'a': 'dummy',
'b': 16 # Check Coerce
}, {
'type': 'mp2',
'a': 'dummy',
}, {
'type': 'mp3',
'b': True # Check Boolean
}]
}
示例8: __init__
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def __init__(self, param_type):
self._validate = voluptuous.Coerce(param_type)
示例9: __init__
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def __init__(self, key_type, value_type, cast=True):
if cast:
self._kval = voluptuous.Coerce(key_type)
self._vval = voluptuous.Coerce(value_type)
else:
def __type_validator(type_, elem):
if not isinstance(elem, type_):
raise voluptuous.Invalid(
"{e} is not of type {t}".format(e=elem, t=type_))
return elem
self._kval = functools.partial(__type_validator, key_type)
self._vval = functools.partial(__type_validator, value_type)
示例10: is_heartbeat
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def is_heartbeat(value):
"""Validate that value is a valid heartbeat integer."""
try:
value = vol.Coerce(int)(value)
return value
except vol.Invalid:
_LOGGER.warning(
"%s is not a valid heartbeat value, falling back to heartbeat 0", value
)
return 0
示例11: validate_gps
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def validate_gps(value):
"""Validate GPS value."""
try:
latitude, longitude, altitude = value.split(",")
vol.Coerce(float)(latitude)
vol.Coerce(float)(longitude)
vol.Coerce(float)(altitude)
except (TypeError, ValueError, vol.Invalid):
raise vol.Invalid('GPS value should be of format "latitude,longitude,altitude"')
return value
示例12: __init__
# 需要导入模块: import voluptuous [as 别名]
# 或者: from voluptuous import Coerce [as 别名]
def __init__(self, allow_extra):
self.schema = v.Schema(
{
v.Required('id'): int,
v.Required('client_name'): v.All(str, v.Length(max=255)),
v.Required('sort_index'): float,
# v.Optional('client_email'): v.Maybe(v.Email),
v.Optional('client_phone'): v.Maybe(v.All(str, v.Length(max=255))),
v.Optional('location'): v.Maybe(
v.Schema(
{
'latitude': v.Maybe(float),
'longitude': v.Maybe(float)
},
required=True
)
),
v.Optional('contractor'): v.Maybe(v.All(v.Coerce(int), v.Range(min=1))),
v.Optional('upstream_http_referrer'): v.Maybe(v.All(str, v.Length(max=1023))),
v.Required('grecaptcha_response'): v.All(str, v.Length(min=20, max=1000)),
v.Optional('last_updated'): v.Maybe(parse_datetime),
v.Required('skills', default=[]): [
v.Schema(
{
v.Required('subject'): str,
v.Required('subject_id'): int,
v.Required('category'): str,
v.Required('qual_level'): str,
v.Required('qual_level_id'): int,
v.Required('qual_level_ranking', default=0): float,
}
)
],
},
extra=allow_extra,
)