本文整理汇总了Python中schema.Schema类的典型用法代码示例。如果您正苦于以下问题:Python Schema类的具体用法?Python Schema怎么用?Python Schema使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Schema类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_args
def validate_args(args):
""" Validate arguments.
Checks:
that the specified file exists and is readable
that LEVEL is between 1 and 7
"""
schema = Schema({
"FILE": Use(open,
error="FILE doesn't exist or isn't readable!"),
"--level": Or(None,
And(Use(int), lambda n: 1 <= n <= 7),
error="LEVEL should be between 1 and 7"),
Optional("--help"): Or(True, False),
})
try:
# Don't return the validated args here, just make sure they are valid.
# Schema will return an object containing an open file object,
# when we just wanted to make sure the file was readable.
schema.validate(args)
return args
except SchemaError as e:
exit(e)
示例2: __init__
def __init__(self):
self.status_ok_schema = Schema({'status' :And(str)})
self.error = {"login_syntax" : '{"type":"login", "nick":"4 <= len(str) <= 10", "passwd":"str"}',
"register_syntax" : '{"type":"register", "nick":"4 <= len(str) <= 10", "passwd":"str", "email":"str"}',
"pub_public_msg_syntax" : '{"type":"public", "nick":"4 <= len(str) <= 10", "token_id":"str", "msg":"str"}',
"pub_public_msg_syntax" : '{"type":"public", "nick":"4 <= len(str) <= 10", "token_id":"str", "msg":"str"}'}
self.user_join_left_schema = Schema({'user_join_left' :And(str), 'user_list': And(list), 'user' :And(str)})
self.login_ok_schema = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'login'),
'nick': And(str, Use(str.lower), lambda n: 4 <= len(n) <= 10),
'token_id':And(str), 'user_list': And(list)})
self.login_nok_schema = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'err_login'),
'msg': And(str)})
self.register_ok_schema = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'register'),
'succ':And(str)})
self.msg_ok_schema = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'public'),
'from_': And(str, Use(str.lower), lambda n: 4 <= len(n) <= 10),
'msg':And(str)})
self.prv_msg_ok_schema = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'private'),
'to_':And(str), 'from_': And(str, Use(str.lower), lambda n: 4 <= len(n) <= 10),
'msg':And(str)})
self.access_folder_schema = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'folder'),
'to_':And(str), 'from_': And(str)})
self.status_ = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'status'),
'status':And(str), 'nick': And(str)})
self.access_folder_valid = Schema({'type' : And(str, Use(str.lower), lambda n: n == 'access_folder_valid'),
'from_': And(str, Use(str.lower), lambda n: 4 <= len(n) <= 10),
'to_':And(str), 'passwd':And(str)})
示例3: test_generate_signature_schema
def test_generate_signature_schema():
"""Test generate_signature_schema() function."""
def f(a, b, camelCase=True, none=None, quantity=3.0*unit.angstroms):
pass
f_schema = generate_signature_schema(f)
assert len(f_schema) == 3
for k in f_schema.keys():
assert isinstance(k, Optional)
# Remove Optional() marker for comparison
stripped_schema = {k._schema: v for k, v in f_schema.items() if k._schema != 'quantity'}
assert {'camel_case': bool, 'none': object} == stripped_schema
# Check conversion
f_schema = Schema(f_schema)
assert f_schema.validate({'quantity': '5*angstrom'}) == {'quantity': 5*unit.angstrom}
# Check update
optional_instance = Optional('camel_case')
updated_schema = generate_signature_schema(f, update_keys={'none': float, optional_instance: int},
exclude_keys={'quantity'})
assert len(updated_schema) == 2
assert updated_schema['none'] == float
assert updated_schema[optional_instance] == int
示例4: validate_args
def validate_args(args):
"""
Validates command line arguments
"""
schema = Schema({
'--user': And(str, len, Use(str.lower), error="missing or invalid user"),
'--repo': And(str, len, Use(str.lower), error="missing or invalid repo"),
'--type': And(str, len, Use(str.lower), lambda s: s in ('deb', 'rpm'), error="type must be one of deb or rpm"),
'--distro': And(str, len, Use(str.lower), error="missing or invalid distro"),
'--distro_version': And(str, len, Use(str.lower), error="missing or invalid distro_version"),
'--arch': And(str, len, Use(str.lower), error="missing or invalid arch"),
'--pkg_name': And(str, len, Use(str.lower), error="missing or invalid pkg_name"),
'--filename': And(str, len, Use(str.lower), error="missing or invalid filename"),
'--timeout': And(Use(float), lambda f: f > 0, lambda f, i=float(args['--poll_interval']): f > i,
error="timeout must be a number greater than 1, and greater than poll_interval (default 30)"),
'--poll_interval': And(Use(float), lambda f: f > 0, error="poll_interval must be a number greater than 0"),
'--page_interval': And(Use(float), lambda f: f > 0, error="page_interval must be a number greater than 0"),
'--help': bool,
'--version': bool,
'--log-level': And(str, len, Use(str.upper),
lambda s: s in ('DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL'),
error="invalid log level specified")
})
try:
return schema.validate(args)
except SchemaError as ex:
utils.abort("Invalid argument: {}. ".format(ex.message))
示例5: _validate_neighbor
def _validate_neighbor(self):
"""Validate neighbor against Schema."""
neighbor_schema = Schema({
'remote_ip': basestring,
'remote_as': And(basestring, lambda n: 0 <= int(n) <= 4294967295),
Optional('password'): basestring,
Optional('maximum_hops'): And(basestring,
lambda n: 1 <= int(n) <= 255),
Optional('timer_keepalive'): And(basestring,
lambda n: 1 <= int(n) <= 65535),
Optional('timer_timeout'): And(basestring,
lambda n: 3 <= int(n) <= 65536),
Optional('description'): basestring,
Optional('soft_reconfiguration'): bool,
Optional('community'): bool,
Optional('remove_private_as'): bool,
Optional('next_hop_self'): bool
})
try:
neighbor_schema.validate(self.neighbor)
except SchemaWrongKeyError:
# It doesn't matter if neighbor dict has other keys besides these.
pass
except SchemaError as e:
raise InvalidNeighborException(e.code)
示例6: test_json_schema_object_or_array_of_object
def test_json_schema_object_or_array_of_object():
# Complex test where "test" accepts either an object or an array of that object
o = {"param1": "test1", Optional("param2"): "test2"}
s = Schema({"test": Or(o, [o])})
assert s.json_schema("my-id") == {
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "my-id",
"properties": {
"test": {
"anyOf": [
{
"additionalProperties": False,
"properties": {"param1": {}, "param2": {}},
"required": ["param1"],
"type": "object",
},
{
"type": "array",
"items": {
"additionalProperties": False,
"properties": {"param1": {}, "param2": {}},
"required": ["param1"],
"type": "object",
},
},
]
}
},
"required": ["test"],
"additionalProperties": False,
"type": "object",
}
示例7: is_valid
def is_valid(self):
"""Checks if the input dictionary contains all valid types
Checks the __dict__ attribute and ensure it follows the correct
schema
Args:
None
Returns:
A Boolean if dictionary follows schema
"""
schema = Schema({
'region': unicode,
'subnet': unicode,
'purchase_type': And(unicode, lambda x: x in ["on_demand", "spot"]),
'image': unicode,
'price': unicode,
'num_instances': int,
'key_name': unicode,
'security_group_ids': list,
'instance_type': unicode,
'tag_name': unicode,
'vol_size': int,
'bdm': dict})
try:
schema.validate(self.__dict__)
return True
except Exception as exc:
print exc
print "Invalid instance template"
return False
示例8: run
def run(raw_args):
'''
Validates the arguments by converting the VCF files into lists of VCFRecords, converting
threshold to int, and calling validate_value on <VALUE>.
returns the command string for dispatching; the new args dictionary after validating, and the first
filename so that that file can later be used for a vcf.VCFWriter object.
:param dict raw_args: the result of docopt(__doc__)
:return str, dict, str: the command string, the new args dict, and the first filename.
'''
commands = ['vcall', 'filter', 'diff', 'stat', 'statdiff', 'exists', 'ambiguous']
schema_dict= {
'<FILE1>' : Use(validate_vcf_file),
Optional('<FILE2>') : Use(validate_vcf_file),
Optional('<VALUE>') : Use(validate_value),
'--count' : bool,
'--threshold' : Use(int, error='Threshold must be integer'),
Optional('--tag') : lambda t: True,
'--csv' : bool
#tags.__contains__ # error='Tag was not valid, should be one of {0}'.format(' '.join(tags)))
}
schema_dict.update( dict( (arg, bool) for arg in commands + ops))
_args = Schema(schema_dict).validate(raw_args)
cmd_str = [k for (k, arg) in _args.items() if k in commands and arg][0]
filename = raw_args['<FILE1>']
return cmd_str, _args, filename
示例9: test_optional_key_convert_failed_randomly_while_with_another_optional_object
def test_optional_key_convert_failed_randomly_while_with_another_optional_object():
"""
In this test, created_at string "2015-10-10 00:00:00" is expected to be converted
to a datetime instance.
- it works when the schema is
s = Schema({
'created_at': _datetime_validator,
Optional(basestring): object,
})
- but when wrapping the key 'created_at' with Optional, it fails randomly
:return:
"""
import datetime
fmt = '%Y-%m-%d %H:%M:%S'
_datetime_validator = Or(None, Use(lambda i: datetime.datetime.strptime(i, fmt)))
# FIXME given tests enough
for i in range(1024):
s = Schema({
Optional('created_at'): _datetime_validator,
Optional('updated_at'): _datetime_validator,
Optional('birth'): _datetime_validator,
Optional(basestring): object,
})
data = {
'created_at': '2015-10-10 00:00:00'
}
validated_data = s.validate(data)
# is expected to be converted to a datetime instance, but fails randomly
# (most of the time)
assert isinstance(validated_data['created_at'], datetime.datetime)
示例10: main
def main(args=None):
# The console script created by setuptools takes the cwd off the path.
sys.path.insert(0, os.getcwd())
scheme = Schema({'--debug': bool,
'--delim': str,
'--demo': bool,
'--help': bool,
'--float': Or(None, str),
'--from': Or(None, Use(lambda x: list(map(int, x.split(','))))),
'--nan': Or(None, str),
'--next': Or(None, str),
'--no-index-label': bool,
'--not-finished': bool,
'-o': Or(None, str),
'--skip': Or(None, Use(lambda x: x.split(','))),
'--skip-parents': bool,
'--version': bool,
'<data-file>': Or(None, str),
'<exp-file>': Or(lambda x: x is None, os.path.exists, error='Invalid <exp-file>'),
'<level>': [str],
'<n>': [And(Use(int), lambda n: n > 0)],
'export': bool,
'resume': bool,
'run': bool,
})
options = scheme.validate(docopt(__doc__, argv=args, version=__version__))
if options['--debug']:
logging.basicConfig(level=logging.DEBUG)
if options['run'] or options['resume']:
exp = Experiment.load(options['<exp-file>'])
kwargs = {'demo': options['--demo'],
'parent_callbacks': not options['--skip-parents'],
'resume': options['resume'],
'session_options': options['-o'],
'from_section': options['--from'],
}
if options['--next']:
kwargs.update(section_obj=exp.find_first_not_run(
options['--next'], by_started=not options['--not-finished']))
elif options['resume'] and not options['<n>']:
kwargs.update(section_obj=exp.find_first_partially_run(options['<level>'][0]))
else:
kwargs.update(zip(options['<level>'], options['<n>']))
run_experiment_section(exp, **kwargs)
elif options['export']:
export_experiment_data(options['<exp-file>'], options['<data-file>'],
float_format=options['--float'],
skip_columns=options['--skip'],
index_label=False if options['--no-index-label'] else None,
na_rep=options['--nan'],
sep=options['--delim'])
示例11: main
def main():
args = docopt(__doc__)
schema = Schema({
'--help': bool,
'--headless': bool,
'--width': Use(int),
'--height': Use(int),
'<save_path>': str,
})
try:
args = schema.validate(args)
except SchemaError as e:
exit(e)
model_path = 'models'
loadPrcFileData('', 'window-title Babble')
loadPrcFileData('', 'win-size %d %d' % (args['--width'], args['--height']))
loadPrcFileData('', 'audio-library-name null') # suppress warning
loadPrcFileData('', 'model-path %s' % model_path)
loadPrcFileData('', 'bullet-filter-algorithm groups-mask')
if args['--headless']:
loadPrcFileData('', 'window-type none')
app = App(args)
app.run()
示例12: parse_args
def parse_args():
opts = docopt(__doc__, version='.'.join(VERSION))
schema = Schema({
Optional('PACK_TYPE'):
Or(None, lambda s: s.lower() in PACKS_TYPE,
error="PACK_TYPE should be either %s" % (', '.join(PACKS_TYPE))),
Optional('--config'):
Or(None, Use(open),
error="--config must be a readable file"),
Optional('--attempts'):
And(Use(int), lambda n: n > 0,
error='--attempts must be a strictly positive integer'),
Optional('--score'):
And(Use(int),
error='--score must be an integer'),
Optional('--threshold'):
And(Use(int), lambda n: n >= 0,
error='--threshold must be a positive integer'),
Optional('--low-threshold'):
And(Use(int), lambda n: n > 0,
error='--low-threshold must be a strictly positive integer'),
Optional('--wait'):
And(Use(int), lambda n: n >= 0,
error='--wait must be a positive integer'),
object: object,
})
opts = schema.validate(opts)
opts['PACK_TYPE'] = opts['PACK_TYPE'].lower() if opts['PACK_TYPE'] else "wild"
if opts['--config']:
config = simplejson.loads(opts['--config'].read())
opts.update(config)
return opts
示例13: main
def main():
"""
Main function
"""
try:
name = indexfile.__name__
version = indexfile.__version__
log = indexfile.getLogger(__name__)
# local variables
index = None
# load commands
commands = load_commands()
helpstr = __doc__ % (name, name) + get_commands_help(commands)
# create validation schema
sch = Schema({
'index': Or(None,
And(Or('-', 'stdin'),
Use(lambda x: sys.stdin)),
open),
Optional('format'): open,
Optional('loglevel'): And(str,
Use(str.lower),
Or('error',
'warn',
'info',
'debug')),
'<command>': Command(commands=commands),
str: object
})
# parse args and remove dashes
args = docopt(helpstr, version="%s v%s" % (name, version), options_first=True)
args = dict([(k.replace('-', ''), v) for k, v in args.iteritems()])
# validate args
args = sch.validate(args)
# deal with 'help' command
if args.get('<command>') == 'help':
docopt(helpstr, version="%s v%s" % (name, version), argv=['--help'])
# load the index and delegate command
config = load_config(os.getcwd(), args)
indexfile.setLogLevel(config.get('loglevel'))
index = open_index(config)
command_ = get_command(args.get('<command>'), commands)
argv = [name, command_] + args['<args>']
sys.argv = argv
module_ = "indexfile.cli.indexfile_%s" % command_
runpy.run_module(module_,
run_name="__main__",
init_globals={'index': index, 'command': '{0} {1}'.format(name, command_)})
except KeyboardInterrupt, e:
sys.exit(1)
示例14: _fit_first
def _fit_first(self, first):
# Check for a tuples of numbers, strings or "sequences".
schema = Schema((int, float, basestring, SequenceValidator()))
schema.validate(first)
if not first:
raise ValueError("Cannot fit with no empty features")
# Build validation schema using the first data point
self.indexes = {} # Tuple index to matrix column mapping
self.reverse = [] # Matrix column to tuple index mapping
self.schema = [None] * len(first)
self.str_tuple_indexes = []
for i, data in enumerate(first):
if isinstance(data, (int, float)):
type_ = Use(float) # ints and floats are all mapped to float
self._add_column(i, None)
elif isinstance(data, basestring):
type_ = basestring # One-hot encoded indexes are added last
self.str_tuple_indexes.append(i)
else:
type_ = SequenceValidator(data)
for j in xrange(type_.size):
self._add_column(i, j)
self.schema[i] = type_
assert None not in self.schema
self.schema = tuple(self.schema)
self.validator = TupleValidator(self.schema)
示例15: optparser
def optparser(argv=sys.argv[1:]):
__usage__ = """
Bayesian analysis of ITC data. Uses MicroCal .itc files, or custom format .yml files for analysing experiments.
Usage:
ITC.py <datafiles>... [-w <workdir> | --workdir=<workdir>] [-n <name> | --name=<name>] [-q <file> | --heats=<file>] [-i <ins> | --instrument=<ins> ] [-v | -vv | -vvv] [-r <file> | --report=<file>] [ -l <logfile> | --log=<logfile>]
ITC.py mcmc <datafiles>... (-m <model> | --model=<model>) [-w <workdir> | --workdir=<workdir>] [ -r <receptor> | --receptor=<receptor>] [-n <name> | --name=<name>] [-q <file> | --heats=<file>] [-i <ins> | --instrument=<ins> ] [ -l <logfile> | --log=<logfile>] [-v | -vv | -vvv] [--report=<file>] [options]
ITC.py (-h | --help)
ITC.py --license
ITC.py --version
Options:
-h, --help Show this screen
--version Show version
--license Show license
-l <logfile>, --log=<logfile> File to write logs to. Will be placed in workdir.
-v, Verbose output level. Multiple flags increase verbosity.
<datafiles> Datafile(s) to perform the analysis on, .itc, .yml
-w <workdir>, --workdir=<workdir> Directory for output files [default: ./]
-r <receptor> | --receptor=<receptor> The name of the receptor for a Competitive Binding model.
-n <name>, --name=<name> Name for the experiment. Will be used for output files. Defaults to inputfile name.
-i <ins>, --instrument=<ins> The name of the instrument used for the experiment. Overrides .itc file instrument.
-q <file>, --heats=<file> Origin format integrated heats file. (From NITPIC use .dat file)
-m <model>, --model=<model> Model to use for mcmc sampling [default: TwoComponent]
--nfit=<n> No. of iteration for maximum a posteriori fit [default: 20000]
--niters=<n> No. of iterations for mcmc sampling [default: 2000000]
--nburn=<n> No. of Burn-in iterations for mcmc sampling [default: 500000]
--nthin=<n> Thinning period for mcmc sampling [default: 250]
--report=<file> Output file with summary in markdown
"""
arguments = docopt(__usage__, argv=argv, version='ITC.py, pre-alpha')
schema = Schema({'--heats': Or(None, And(str, os.path.isfile, Use(os.path.abspath))), # str, verify that it exists
'--help': bool, # True or False are accepted
'--license': bool, # True or False are accepted
'-v': And(int, lambda n: 0 <= n <= 3), # integer between 0 and 3
'--model': And(str, lambda m: m in known_models), # str and found in this dict
'--nfit':And(Use(int), lambda n: n > 0),
# Convert str to int, make sure that it is larger than 0
'--nburn': And(Use(int), lambda n: n > 0),
# Convert str to int, make sure that it is larger than 0
'--niters': And(Use(int), lambda n: n > 0),
# Convert str to int, make sure that it is larger than 0
'--nthin': And(Use(int), lambda n: n > 0),
# Convert str to int, make sure that it is larger than 0
'--name': Or(None, And(str, len)), # Not an empty string
'--instrument': Or(None, And(str, lambda m: m in known_instruments)),
# None, or str and found in this dict
'--version': bool, # True or False are accepted
'--receptor': str, # str
'--workdir': str, # str
# list and ensure it contains existing files
'<datafiles>': And(list, lambda inpfiles : [os.path.isfile(inpfile) for inpfile in inpfiles], Use(lambda inpfiles: [os.path.abspath(inpfile) for inpfile in inpfiles])),
'mcmc': bool, # True or False are accepted
'--report': Or(None, Use(lambda f: open(f, 'w'))),
# Don't use, or open file with writing permissions
'--log': Or(None, str), # Don't use, or str
})
return schema.validate(arguments)