本文整理汇总了Python中schema_utils.normalize_against_schema函数的典型用法代码示例。如果您正苦于以下问题:Python normalize_against_schema函数的具体用法?Python normalize_against_schema怎么用?Python normalize_against_schema使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了normalize_against_schema函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _validate_ui_config
def _validate_ui_config(obj_type, ui_config):
"""Validates the value of a UI configuration."""
reference_dict = UI_CONFIG_SPECS[obj_type]
assert set(ui_config.keys()) <= set(reference_dict.keys())
for key, value in ui_config.iteritems():
schema_utils.normalize_against_schema(
value, reference_dict[key])
示例2: validate_customization_arg_values
def validate_customization_arg_values(self, customization_args):
"""Validates customization arg values. The input is a dict whose
keys are the names of the customization args.
"""
for ca_spec in self.customization_arg_specs:
schema_utils.normalize_against_schema(
customization_args[ca_spec.name]['value'],
ca_spec.schema)
示例3: _validate_customization_arg_specs
def _validate_customization_arg_specs(self, customization_arg_specs):
for ca_spec in customization_arg_specs:
self.assertEqual(set(ca_spec.keys()), set([
'name', 'description', 'schema', 'default_value']))
self.assertTrue(isinstance(ca_spec['name'], basestring))
self.assertTrue(self._is_alphanumeric_string(ca_spec['name']))
self.assertTrue(isinstance(ca_spec['description'], basestring))
self.assertGreater(len(ca_spec['description']), 0)
# The default value might not pass validation checks (e.g. the
# Image component has a required field whose default value is
# empty). Thus, when checking the default value schema, we don't
# apply the custom validators.
schema_utils_test.validate_schema(ca_spec['schema'])
self.assertEqual(
ca_spec['default_value'],
schema_utils.normalize_against_schema(
ca_spec['default_value'], ca_spec['schema'],
apply_custom_validators=False))
if ca_spec['schema']['type'] == 'custom':
obj_class = obj_services.Registry.get_object_class_by_type(
ca_spec['schema']['obj_type'])
self.assertIsNotNone(obj_class.edit_html_filename)
self.assertIsNotNone(obj_class.edit_js_filename)
self.assertEqual(
ca_spec['default_value'],
obj_class.normalize(ca_spec['default_value']))
示例4: normalize
def normalize(cls, raw):
"""Validates and normalizes a raw Python object."""
"""
Checks that there are no self-loops or multiple edges.
Checks that unlabeled graphs have all labels empty.
Checks that unweighted graphs have all weights set to 1.
TODO(czx): Think about support for multigraphs?
"""
try:
raw = schema_utils.normalize_against_schema(raw, cls.SCHEMA)
if not raw['isLabeled']:
for vertex in raw['vertices']:
assert (vertex['label'] == '')
for edge in raw['edges']:
assert (edge['src'] != edge['dst'])
if not raw['isWeighted']:
assert (edge['weight'] == 1.0)
if raw['isDirected']:
edge_pairs = [
(edge['src'], edge['dst']) for edge in raw['edges']]
else:
edge_pairs = (
[(edge['src'], edge['dst']) for edge in raw['edges']] +
[(edge['dst'], edge['src']) for edge in raw['edges']]
)
assert len(set(edge_pairs)) == len(edge_pairs)
except Exception:
raise TypeError('Cannot convert to graph %s' % raw)
return raw
示例5: normalize
def normalize(cls, raw):
"""Validates and normalizes a raw Python object."""
"""
Checks that there are no self-loops or multiple edges.
Checks that unlabeled graphs have all labels empty.
Checks that unweighted graphs have all weights set to 1.
TODO(czx): Think about support for multigraphs?
"""
try:
raw = schema_utils.normalize_against_schema(raw, cls.SCHEMA)
if not raw["isLabeled"]:
for vertex in raw["vertices"]:
assert vertex["label"] == ""
for edge in raw["edges"]:
assert edge["src"] != edge["dst"]
if not raw["isWeighted"]:
assert edge["weight"] == 1.0
if raw["isDirected"]:
edge_pairs = [(edge["src"], edge["dst"]) for edge in raw["edges"]]
else:
edge_pairs = [(edge["src"], edge["dst"]) for edge in raw["edges"]] + [
(edge["dst"], edge["src"]) for edge in raw["edges"]
]
assert len(set(edge_pairs)) == len(edge_pairs)
except Exception:
raise TypeError("Cannot convert to graph %s" % raw)
return raw
示例6: check_normalization
def check_normalization(self, schema, mappings, invalid_items):
"""Validates the schema and tests that values are normalized correctly.
Args:
schema: the schema to normalize the value against.
mappings: a list of 2-element tuples. The first element of
each item is expected to be normalized to the second.
invalid_items: a list of values. Each of these is expected to raise
an AssertionError when normalized.
"""
validate_schema(schema)
for raw_value, expected_value in mappings:
self.assertEqual(
schema_utils.normalize_against_schema(raw_value, schema),
expected_value)
for value in invalid_items:
with self.assertRaises(Exception):
schema_utils.normalize_against_schema(value, schema)
示例7: __init__
def __init__(self, name, schema, description, default_value):
if Registry.get_config_property(name):
raise Exception('Property with name %s already exists' % name)
self._name = name
self._schema = schema
self._description = description
self._default_value = schema_utils.normalize_against_schema(
default_value, self._schema)
Registry.init_config_property(self.name, self)
示例8: __init__
def __init__(self, name, schema, description, default_value):
if name in Registry._config_registry:
raise Exception('Property with name %s already exists' % name)
self._name = name
# TODO(sll): Validate the schema.
self._schema = schema
self._description = description
self._default_value = schema_utils.normalize_against_schema(
default_value, self._schema)
Registry._config_registry[self.name] = self
示例9: _validate_validator
def _validate_validator(obj_type, validator):
"""Validates the value of a 'validator' field."""
reference_dict = VALIDATOR_SPECS[obj_type]
assert 'id' in validator and validator['id'] in reference_dict
customization_keys = validator.keys()
customization_keys.remove('id')
assert (set(customization_keys) ==
set(reference_dict[validator['id']].keys()))
for key in customization_keys:
value = validator[key]
schema = reference_dict[validator['id']][key]
try:
schema_utils.normalize_against_schema(value, schema)
except Exception as e:
raise AssertionError(e)
# Check that the id corresponds to a valid normalizer function.
validator_fn = schema_utils._Validators.get(validator['id'])
assert set(inspect.getargspec(validator_fn).args) == set(
customization_keys + ['obj'])
示例10: validate
def validate(self):
"""Validates a visualization object.
This is only used in tests for the validity of interactions.
"""
# Check that the calculation id exists.
calculation_registry.Registry.get_calculation_by_id(self.calculation_id)
# Check that the options_dict is valid.
expected_option_names = sorted([
spec['name'] for spec in self._OPTIONS_SPECS])
actual_option_names = sorted(self.options.keys())
if actual_option_names != expected_option_names:
raise utils.ValidationError(
'For visualization %s, expected option names %s; received '
'names %s' %
(self.id, expected_option_names, actual_option_names))
# Check that the schemas are correct.
for spec in self._OPTIONS_SPECS:
schema_utils.normalize_against_schema(
self.options[spec['name']], spec['schema'])
示例11: __init__
def __init__(self, name, schema, description, default_value,
post_set_hook=None, is_directly_settable=True):
if Registry.get_config_property(name):
raise Exception('Property with name %s already exists' % name)
self._name = name
self._schema = schema
self._description = description
self._default_value = schema_utils.normalize_against_schema(
default_value, self._schema)
self._post_set_hook = post_set_hook
self._is_directly_settable = is_directly_settable
Registry.init_config_property(self.name, self)
示例12: normalize
def normalize(cls, raw):
# Moves cur_value to the nearest available value in the range [min_value, max_value]
def clamp(min_value, current_value, max_value):
return min(max_value, max(min_value, current_value))
try:
raw = schema_utils.normalize_against_schema(raw, cls.SCHEMA)
raw[0][0] = clamp(0.0, raw[0][0], 1.0)
raw[0][1] = clamp(0.0, raw[0][1], 1.0)
raw[1][0] = clamp(0.0, raw[1][0], 1.0)
raw[1][1] = clamp(0.0, raw[1][1], 1.0)
except Exception:
raise TypeError('Cannot convert to Normalized Rectangle %s' % raw)
return raw
示例13: _validate_customization_arg_specs
def _validate_customization_arg_specs(self, customization_args):
for ca_spec in customization_args:
self.assertEqual(set(ca_spec.keys()), set([
'name', 'description', 'schema', 'default_value']))
self.assertTrue(isinstance(ca_spec['name'], basestring))
self.assertTrue(self._is_alphanumeric_string(ca_spec['name']))
self.assertTrue(isinstance(ca_spec['description'], basestring))
self.assertGreater(len(ca_spec['description']), 0)
schema_utils_test.validate_schema(ca_spec['schema'])
self.assertEqual(
ca_spec['default_value'],
schema_utils.normalize_against_schema(
ca_spec['default_value'], ca_spec['schema']))
if ca_spec['schema']['type'] == 'custom':
obj_class = obj_services.Registry.get_object_class_by_type(
ca_spec['schema']['obj_type'])
self.assertIsNotNone(obj_class.edit_html_filename)
self.assertIsNotNone(obj_class.edit_js_filename)
self.assertEqual(
ca_spec['default_value'],
obj_class.normalize(ca_spec['default_value']))
示例14: normalize
def normalize(cls, raw):
"""Reads `raw` as a unicode string representing a tar file and returns
the base64-encoded contents."""
raw = schema_utils.normalize_against_schema(raw, cls._schema)
raw = base64.b64decode(raw)
return tarfile.open(fileobj=StringIO.StringIO(raw), mode='r:gz')
示例15: normalize
def normalize(self, value):
return schema_utils.normalize_against_schema(value, self._schema)