本文整理汇总了Python中schema.Regex方法的典型用法代码示例。如果您正苦于以下问题:Python schema.Regex方法的具体用法?Python schema.Regex怎么用?Python schema.Regex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类schema
的用法示例。
在下文中一共展示了schema.Regex方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_ebs_volume_parameters
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def validate_ebs_volume_parameters(params: dict):
from spotty.providers.aws.deployment.project_resources.ebs_volume import EbsVolume
schema = Schema({
Optional('volumeName', default=''): And(str, Regex(r'^[\w-]{1,255}$')),
Optional('mountDir', default=''): str, # all the checks happened in the base configuration
Optional('size', default=0): And(int, lambda x: x > 0),
# TODO: add the "iops" parameter to support the "io1" EBS volume type
Optional('type', default='gp2'): lambda x: x in ['gp2', 'sc1', 'st1', 'standard'],
Optional('deletionPolicy', default=EbsVolume.DP_CREATE_SNAPSHOT): And(
str,
lambda x: x in [EbsVolume.DP_CREATE_SNAPSHOT,
EbsVolume.DP_UPDATE_SNAPSHOT,
EbsVolume.DP_RETAIN,
EbsVolume.DP_DELETE], error='Incorrect value for "deletionPolicy".'
),
})
return validate_config(schema, params)
示例2: validate_disk_volume_parameters
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def validate_disk_volume_parameters(params: dict):
from spotty.providers.gcp.deployment.project_resources.disk_volume import DiskVolume
schema = Schema({
Optional('diskName', default=''): And(str, Regex(r'^[\w-]{1,255}$')),
Optional('mountDir', default=''): str, # all the checks happened in the base configuration
Optional('size', default=0): And(int, lambda x: x > 0),
Optional('deletionPolicy', default=DiskVolume.DP_CREATE_SNAPSHOT): And(
str,
lambda x: x in [DiskVolume.DP_CREATE_SNAPSHOT,
DiskVolume.DP_UPDATE_SNAPSHOT,
DiskVolume.DP_RETAIN,
DiskVolume.DP_DELETE], error='Incorrect value for "deletionPolicy".'
),
})
return validate_config(schema, params)
示例3: validate_instance_parameters
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def validate_instance_parameters(params: dict):
from spotty.providers.aws.config.instance_config import VOLUME_TYPE_EBS
instance_parameters = {
'region': And(str, Regex(r'^[a-z0-9-]+$')),
Optional('availabilityZone', default=''): And(str, Regex(r'^[a-z0-9-]+$')),
Optional('subnetId', default=''): And(str, Regex(r'^subnet-[a-z0-9]+$')),
'instanceType': str,
Optional('onDemandInstance', default=False): bool,
Optional('amiName', default=None): And(str, len, Regex(r'^[\w\(\)\[\]\s\.\/\'@-]{3,128}$')),
Optional('amiId', default=None): And(str, len, Regex(r'^ami-[a-z0-9]+$')),
Optional('rootVolumeSize', default=0): And(Or(int, str), Use(str),
Regex(r'^\d+$', error='Incorrect value for "rootVolumeSize".'),
Use(int),
And(lambda x: x > 0,
error='"rootVolumeSize" should be greater than 0 or should '
'not be specified.'),
),
Optional('maxPrice', default=0): And(Or(float, int, str), Use(str),
Regex(r'^\d+(\.\d{1,6})?$', error='Incorrect value for "maxPrice".'),
Use(float),
And(lambda x: x > 0, error='"maxPrice" should be greater than 0 or '
'should not be specified.'),
),
Optional('managedPolicyArns', default=[]): [str],
}
volumes_checks = [
And(lambda x: len(x) < 12, error='Maximum 11 volumes are supported at the moment.'),
]
instance_checks = [
And(lambda x: not (x['onDemandInstance'] and x['maxPrice']),
error='"maxPrice" cannot be specified for on-demand instances.'),
And(lambda x: not (x['amiName'] and x['amiId']),
error='"amiName" and "amiId" parameters cannot be used together.'),
]
schema = get_instance_parameters_schema(instance_parameters, VOLUME_TYPE_EBS, instance_checks, volumes_checks)
return validate_config(schema, params)
示例4: validate_instance_parameters
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def validate_instance_parameters(params: dict):
from spotty.providers.gcp.config.instance_config import VOLUME_TYPE_DISK
instance_parameters = {
'zone': And(str, Regex(r'^[a-z0-9-]+$')),
'machineType': And(str, And(is_valid_machine_type, error='Invalid instance type.')),
Optional('gpu', default=None): {
'type': str,
Optional('count', default=1): int,
},
Optional('onDemandInstance', default=False): bool,
Optional('imageName', default=None): And(str, len, Regex(r'^[\w-]+$')),
Optional('imageUrl', default=None): And(str, len, Regex(IMAGE_URL_REGEX)),
Optional('bootDiskSize', default=0): And(Or(int, str), Use(str),
Regex(r'^\d+$', error='Incorrect value for "bootDiskSize".'),
Use(int),
And(lambda x: x > 0,
error='"rootVolumeSize" should be greater than 0 or should '
'not be specified.'),
),
}
instance_checks = [
And(lambda x: not x['gpu'] or is_gpu_machine_type(x['machineType']),
error='GPU cannot be attached to shared-core or memory-optimized machine types.'),
And(lambda x: not (x['imageName'] and x['imageUrl']),
error='"imageName" and "imageUrl" parameters cannot be used together.'),
]
schema = get_instance_parameters_schema(instance_parameters, VOLUME_TYPE_DISK, instance_checks, [])
return validate_config(schema, params)
示例5: get_instance_parameters_schema
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def get_instance_parameters_schema(instance_parameters: dict, default_volume_type: str,
instance_checks: list = None, volumes_checks: list = None):
if not instance_checks:
instance_checks = []
if not volumes_checks:
volumes_checks = []
schema = Schema(And(
{
**instance_parameters,
Optional('dockerDataRoot', default=''): And(
str,
And(os.path.isabs, error='Use an absolute path when specifying a Docker data root directory'),
Use(lambda x: x.rstrip('/')),
),
Optional('volumes', default=[]): And(
[{
'name': And(Or(int, str), Use(str), Regex(r'^[\w-]+$')),
Optional('type', default=default_volume_type): str,
Optional('parameters', default={}): {
Optional('mountDir', default=''): And(
str,
And(os.path.isabs, error='Use absolute paths for mount directories'),
Use(lambda x: x.rstrip('/'))
),
And(str, Regex(r'^[\w]+$')): object,
},
}],
And(lambda x: is_unique_value(x, 'name'), error='Each instance volume must have a unique name.'),
And(lambda x: not has_prefix([(volume['parameters']['mountDir'] + '/') for volume in x
if volume['parameters']['mountDir']]),
error='Mount directories cannot be prefixes for each other.'),
*volumes_checks,
),
Optional('localSshPort', default=None): Or(None, And(int, lambda x: 0 <= x <= 65535)),
Optional('commands', default=''): str,
},
And(lambda x: not x['dockerDataRoot'] or any([True for v in x['volumes'] if v['parameters']['mountDir'] and
is_subdir(x['dockerDataRoot'], v['parameters']['mountDir'])]),
error='The "mountDir" of one of the volumes must be a prefix for the "dockerDataRoot" path.'),
*instance_checks
))
return schema
示例6: validate_check_config
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def validate_check_config(check_config):
class PrettyReprAnd(schema.And):
def __repr__(self):
return self._error
check_name = PrettyReprAnd(
str,
lambda val: len(val) > 0,
lambda val: not any(w in val for w in string.whitespace),
error='Check name must be a nonzero length string with no whitespace')
timeout_units = ['ns', 'us', 'µs', 'ms', 's', 'm', 'h']
timeout = schema.Regex(
r'^\d+(\.\d+)?({})$'.format('|'.join(timeout_units)),
error='Timeout must be a string containing an integer or float followed by a unit: {}'.format(
', '.join(timeout_units)))
check_config_schema = schema.Schema({
schema.Optional('cluster_checks'): {
check_name: {
'description': str,
'cmd': [str],
'timeout': timeout,
},
},
schema.Optional('node_checks'): {
'checks': {
check_name: {
'description': str,
'cmd': [str],
'timeout': timeout,
schema.Optional('roles'): schema.Schema(
['master', 'agent'],
error='roles must be a list containing master or agent or both',
),
},
},
schema.Optional('prestart'): [check_name],
schema.Optional('poststart'): [check_name],
},
})
check_config_obj = validate_json_dictionary(check_config)
try:
check_config_schema.validate(check_config_obj)
except schema.SchemaError as exc:
raise AssertionError(str(exc).replace('\n', ' ')) from exc
if 'node_checks' in check_config_obj.keys():
node_checks = check_config_obj['node_checks']
assert any(k in node_checks.keys() for k in ['prestart', 'poststart']), (
'At least one of prestart or poststart must be defined in node_checks')
assert node_checks['checks'].keys() == set(
node_checks.get('prestart', []) + node_checks.get('poststart', [])), (
'All node checks must be referenced in either prestart or poststart, or both')
return check_config_obj
示例7: query_top_spreaders
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def query_top_spreaders():
"""Handle API request '/top-user'.
API Request Parameters
----------------------
upper_day : string formatted datetime
most_recent : bool
API Response Keys
-----------------
status : string
num_of_entries : int
spreaders : dict
bot_score : float
number_of_tweets : int
site_type : {'claim', 'fact_checking'}
spreading_type : {'active', 'influencial'}
upper_day : string formatted datetime
user_id : int
user_raw_id : string
user_screen_name : string
"""
lucene.getVMEnv().attachCurrentThread()
yesterday = datetime.utcnow().date() - timedelta(days=1)
yesterday = yesterday.strftime('%Y-%m-%d')
q_top_spreaders_schema = Schema({
Optional('upper_day', default=yesterday):
And(Regex('^\d{4}-\d{2}-\d{2}$'),
Use(dateutil.parser.parse),
error='Invalid date, should be yyyy-mm-dd format'),
Optional('most_recent', default=True):
And(str,
Use(lambda s: s.lower()), lambda s: s in ('true', 'false'),
Use(lambda s: True if s == 'true' else False)),
})
q_kwargs = copy_req_args(request.args)
try:
q_kwargs = q_top_spreaders_schema.validate(q_kwargs)
df = db_query_top_spreaders(engine, **q_kwargs)
if len(df) == 0:
raise APINoResultError('No top spreader found!')
response = dict(
status='OK',
num_of_entries=len(df),
spreaders=flask.json.loads(df.to_json(**TO_JSON_KWARGS)))
except SchemaError as e:
response = dict(status='ERROR', error=str(e))
except APINoResultError as e:
response = dict(status='No result error', error=str(e))
except Exception as e:
logger.exception(e)
response = dict(status='ERROR', error='Server error, query failed')
return flask.jsonify(response)
示例8: query_top_articles
# 需要导入模块: import schema [as 别名]
# 或者: from schema import Regex [as 别名]
def query_top_articles():
"""Handle API request 'top-articles'
API Request Parameters
----------------------
upper_day : string formatted datetime
most_recent : bool
API Response Keys
-----------------
status : string
num_of_entries : int
articles : dict
canonical_url : string
date_captured : string formatted datetime
number_of_tweets : int
site_type : {'claim', 'fact_checking'}
title : string
upper_day : string formatted datetime
"""
lucene.getVMEnv().attachCurrentThread()
yesterday = datetime.utcnow().date() - timedelta(days=1)
yesterday = yesterday.strftime('%Y-%m-%d')
q_top_article_schema = Schema({
Optional('upper_day', default=yesterday):
And(Regex('^\d{4}-\d{2}-\d{2}$'),
Use(dateutil.parser.parse),
error='Invalid date, shoul be yyyy-mm-dd format'),
Optional('most_recent', default=True):
And(str,
Use(lambda s: s.lower()), lambda s: s in ('true', 'false'),
Use(lambda s: True if s == 'true' else False)),
Optional('exclude_tags', default=[]):
And(Use(eval), error='Invalid exclude_tags input format'),
})
q_kwargs = copy_req_args(request.args)
try:
q_kwargs = q_top_article_schema.validate(q_kwargs)
df = db_query_top_articles(engine, **q_kwargs)
if len(df) == 0:
raise APINoResultError('No top article found!')
response = dict(
status='OK',
num_of_entries=len(df),
articles=flask.json.loads(df.to_json(**TO_JSON_KWARGS)))
except SchemaError as e:
response = dict(status='ERROR', error=str(e))
except APINoResultError as e:
response = dict(status='No result error', error=str(e))
except Exception as e:
logger.exception(e)
response = dict(status='ERROR', error='Server error, query failed')
return flask.jsonify(response)