Python voluptuous 模块,Range() 实例源码
我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用voluptuous.Range()。
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
def page(self, start, end):
s_schema = Schema({ Required('start'): All(int, Range(min=0))})
e_schema = Schema({ Required('end'): All(int, Range(min=0))})
s_status, s_result = self.try_schema('start', start, s_schema)
if not s_status:
return [s_status, s_result]
e_status, e_result = self.try_schema('end', end, e_schema)
if not e_status:
return [e_status, e_result]
if end < start:
return [False, 'ending page cannot be lower than starting page']
total_pages = end - start
if total_pages > 50:
return [False, 'you cannot request more than 50 pages']
return [True, None]
def __init__(self, server):
self.server = server
self.guild_man = server.guild_man
self.channel_edit_base = Schema({
'name': All(str, Length(min=2, max=100)),
'position': int,
Optional('nsfw'): bool,
}, required=True)
self.textchan_editschema = self.channel_edit_base.extend({
'topic': All(str, Length(min=0, max=1024))
})
self.voicechan_editschema = self.channel_edit_base.extend({
'bitrate': All(int, Range(min=8000, max=96000)),
'user_limit': All(int, Range(min=0, max=99)),
})
self.register()
def _state_variable_create_schema(self, type_info):
# construct validators
validators = []
data_type = type_info['data_type_python']
validators.append(data_type)
if 'allowed_values' in type_info:
allowed_values = type_info['allowed_values']
in_ = vol.In(allowed_values) # coerce allowed values? assume always string for now
validators.append(in_)
if 'allowed_value_range' in type_info:
min_ = type_info['allowed_value_range'].get('min', None)
max_ = type_info['allowed_value_range'].get('max', None)
min_ = data_type(min_)
max_ = data_type(max_)
range_ = vol.Range(min=min_, max=max_)
validators.append(range_)
# construct key
key = vol.Required('value')
if 'default_value' in type_info:
default_value = type_info['default_value']
if data_type == bool:
default_value = default_value == '1'
else:
default_value = data_type(default_value)
key.default = default_value
return vol.Schema({key: vol.All(*validators)})
def vote(self,
identifier,
weight,
account=None):
""" Vote for a post
:param str identifier: Identifier for the post to upvote Takes
the form ``@author/permlink``
:param float weight: Voting weight. Range: -100.0 - +100.0. May
not be 0.0
:param str account: Voter to use for voting. (Optional)
If ``voter`` is not defines, the ``default_account`` will be taken or
a ValueError will be raised
.. code-block:: python
steempy set default_account <account>
"""
if not account:
account = configStorage.get("default_account")
if not account:
raise ValueError("You need to provide a voter account")
post_author, post_permlink = resolve_identifier(identifier)
op = operations.Vote(
**{"voter": account,
"author": post_author,
"permlink": post_permlink,
"weight": int(weight * STEEMIT_1_PERCENT)}
)
return self.finalizeOp(op, account, "posting")
def validate_config(_config):
source_schema = voluptuous.Schema({
"module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"params": {
"zk_host": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"zk_port": int,
"group_id": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"topics": {
voluptuous.And(basestring, vu.NoSpaceCharacter()):
voluptuous.And(int, voluptuous.Range(min=1))
}
}
}, required=True)
return source_schema(_config)
def validate_config(_config):
markov_schema = voluptuous.Schema({
"module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"sleep": voluptuous.And(
float, voluptuous.Range(
min=0, max=1, min_included=False, max_included=False)),
}, required=True)
return markov_schema(_config)
def validate_config(_config):
source_schema = voluptuous.Schema({
"module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"params": {
"host": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"port": int,
"model": {
"name": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"params": {
"origin_types": voluptuous.And([
{
"origin_type": voluptuous.And(
basestring, vu.NoSpaceCharacter()),
"weight": voluptuous.And(
voluptuous.Or(int, float),
voluptuous.Range(
min=0, min_included=False)),
}
], vu.NotEmptyArray()),
voluptuous.Optional("key_causes"): dict
}
},
"alerts_per_burst": voluptuous.And(
int, voluptuous.Range(min=1)),
"idle_time_between_bursts": voluptuous.And(
voluptuous.Or(int, float),
voluptuous.Range(min=0, min_included=False))
}
}, required=True)
return source_schema(_config)
def validate_config(_config):
source_schema = voluptuous.Schema({
"module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"sleep": voluptuous.And(
float,
voluptuous.Range(
min=0, max=1, min_included=False, max_included=False)),
}, required=True)
return source_schema(_config)
def schema_ext(self):
return voluptuous.All(numbers.Real,
voluptuous.Range(min=self.min,
max=self.max))
def vote(self, direction):
schema = Schema({ Required('vote'): All(int, Range(min=-1, max=1))})
return self.try_schema('vote', direction, schema)
def vote(self,
identifier,
weight,
account=None):
""" Vote for a post
:param str identifier: Identifier for the post to upvote Takes
the form ``@author/permlink``
:param float weight: Voting weight. Range: -100.0 - +100.0. May
not be 0.0
:param str account: Voter to use for voting. (Optional)
If ``voter`` is not defines, the ``default_account`` will be taken or
a ValueError will be raised
.. code-block:: python
steempy set default_account <account>
"""
if not account:
account = configStorage.get("default_account")
if not account:
raise ValueError("You need to provide a voter account")
post_author, post_permlink = resolve_identifier(identifier)
op = operations.Vote(
**{"voter": account,
"author": post_author,
"permlink": post_permlink,
"weight": int(weight * STEEMIT_1_PERCENT)}
)
return self.finalizeOp(op, account, "posting")
def __init__(self, server):
self.server = server
self.guild_man = server.guild_man
self.invite_create_schema = Schema({
Required('max_age', default=86400): All(int, Range(min=10, max=86400)),
Required('max_uses', default=0): All(int, Range(min=0, max=50)),
Required('temporary', default=False): bool,
Required('unique', default=True): bool,
}, extra=REMOVE_EXTRA)
self.register(server.app)
def validate_config(_config):
source_schema = voluptuous.Schema({
"module": voluptuous.And(
basestring, vu.NoSpaceCharacter()),
"min_event_per_burst": voluptuous.Or(float, int),
"sleep": voluptuous.And(
float, voluptuous.Range(
min=0, max=1, min_included=False, max_included=False)),
"transitions": {
"web_service": {
"run=>slow": {
voluptuous.And(vu.NumericString()): voluptuous.And(
voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
},
"slow=>run": {
voluptuous.And(vu.NumericString()): voluptuous.And(
voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
},
"stop=>run": voluptuous.And(
voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
},
"switch": {
"on=>off": voluptuous.And(voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
"off=>on": voluptuous.And(voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
},
"host": {
"on=>off": voluptuous.And(voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
"off=>on": voluptuous.And(voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
},
},
"triggers": {
"support": {
"get_called": {
voluptuous.And(vu.NumericString()): voluptuous.And(
voluptuous.Or(int, float),
voluptuous.Range(min=0, max=1)),
},
},
},
"graph": {
voluptuous.And(basestring, vu.ValidMarkovGraph()): [basestring]
}
}, required=True)
return source_schema(_config)