diff options
author | Colin Dunklau <colin.dunklau@gmail.com> | 2013-05-11 13:38:26 -0500 |
---|---|---|
committer | Colin Dunklau <colin.dunklau@gmail.com> | 2013-05-11 13:38:26 -0500 |
commit | 384fbcfef523e747b38dd2201f298098afd3f7e2 (patch) | |
tree | dc27a3381a4a4a9bb75ee336c1ea6fe760265084 /jsonschema | |
parent | fe450f221c74e117f572fd313081ab7165ebac26 (diff) | |
download | jsonschema-384fbcfef523e747b38dd2201f298098afd3f7e2.tar.gz |
Call _utils functions from the module
Diffstat (limited to 'jsonschema')
-rw-r--r-- | jsonschema/__init__.py | 39 | ||||
-rw-r--r-- | jsonschema/_utils.py | 28 |
2 files changed, 34 insertions, 33 deletions
diff --git a/jsonschema/__init__.py b/jsonschema/__init__.py index ba99618..64dbfb1 100644 --- a/jsonschema/__init__.py +++ b/jsonschema/__init__.py @@ -37,10 +37,7 @@ from .compat import ( basestring, unicode, long, iteritems, ) -from ._utils import ( - _indent, _format_as_index, _find_additional_properties, _extras_msg, - _types_msg, _flatten, _list, _unbool, _uniq, -) +from . import _utils FLOAT_TOLERANCE = 10 ** -15 @@ -104,8 +101,8 @@ class _Error(Exception): ): return self.message - path = _format_as_index(self.path) - schema_path = _format_as_index(list(self.schema_path)[:-1]) + path = _utils.format_as_index(self.path) + schema_path = _utils.format_as_index(list(self.schema_path)[:-1]) pschema = pprint.pformat(self.schema, width=72) pinstance = pprint.pformat(self.instance, width=72) @@ -120,9 +117,9 @@ class _Error(Exception): ) % ( self.validator, schema_path, - _indent(pschema), + _utils.indent(pschema), path, - _indent(pinstance), + _utils.indent(pinstance), ) if PY3: @@ -242,7 +239,7 @@ class ValidatorMixin(object): # bool inherits from int, so ensure bools aren't reported as integers if isinstance(instance, bool): - pytypes = _flatten(pytypes) + pytypes = _utils.flatten(pytypes) num = any(issubclass(pytype, numbers.Number) for pytype in pytypes) if num and bool not in pytypes: return False @@ -323,7 +320,7 @@ class _Draft34CommonMixin(object): if not self.is_type(instance, "object"): return - extras = set(_find_additional_properties(instance, schema)) + extras = set(_utils.find_additional_properties(instance, schema)) if self.is_type(aP, "object"): for extra in extras: @@ -331,7 +328,7 @@ class _Draft34CommonMixin(object): yield error elif not aP and extras: error = "Additional properties are not allowed (%s %s unexpected)" - yield ValidationError(error % _extras_msg(extras)) + yield ValidationError(error % _utils.extras_msg(extras)) def validate_items(self, items, instance, schema): if not self.is_type(instance, "array"): @@ -363,7 +360,7 @@ class _Draft34CommonMixin(object): elif not aI and len(instance) > len(schema.get("items", [])): error = "Additional items are not allowed (%s %s unexpected)" yield ValidationError( - error % _extras_msg(instance[len(schema.get("items", [])):]) + error % _utils.extras_msg(instance[len(schema.get("items", [])):]) ) def validate_minimum(self, minimum, instance, schema): @@ -424,7 +421,11 @@ class _Draft34CommonMixin(object): yield ValidationError("%r is too long" % (instance,)) def validate_uniqueItems(self, uI, instance, schema): - if uI and self.is_type(instance, "array") and not _uniq(instance): + if ( + uI and + self.is_type(instance, "array") and + not _utils.uniq(instance) + ): yield ValidationError("%r has non-unique elements" % instance) def validate_pattern(self, patrn, instance, schema): @@ -463,7 +464,7 @@ class _Draft34CommonMixin(object): ): yield error else: - dependencies = _list(dependency) + dependencies = _utils.mklist(dependency) for dependency in dependencies: if dependency not in instance: yield ValidationError( @@ -488,7 +489,7 @@ class Draft3Validator(ValidatorMixin, _Draft34CommonMixin, object): """ def validate_type(self, types, instance, schema): - types = _list(types) + types = _utils.mklist(types) all_errors = [] for index, type in enumerate(types): @@ -504,7 +505,7 @@ class Draft3Validator(ValidatorMixin, _Draft34CommonMixin, object): return else: yield ValidationError( - _types_msg(instance, types), context=all_errors, + _utils.types_msg(instance, types), context=all_errors, ) def validate_properties(self, properties, instance, schema): @@ -533,7 +534,7 @@ class Draft3Validator(ValidatorMixin, _Draft34CommonMixin, object): yield error def validate_disallow(self, disallow, instance, schema): - for disallowed in _list(disallow): + for disallowed in _utils.mklist(disallow): if self.is_valid(instance, {"type" : [disallowed]}): yield ValidationError( "%r is disallowed for %r" % (disallowed, instance) @@ -642,10 +643,10 @@ class Draft4Validator(ValidatorMixin, _Draft34CommonMixin, object): """ def validate_type(self, types, instance, schema): - types = _list(types) + types = _utils.mklist(types) if not any(self.is_type(instance, type) for type in types): - yield ValidationError(_types_msg(instance, types)) + yield ValidationError(_utils.types_msg(instance, types)) def validate_properties(self, properties, instance, schema): if not self.is_type(instance, "object"): diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py index 0a50803..0c79d2d 100644 --- a/jsonschema/_utils.py +++ b/jsonschema/_utils.py @@ -4,7 +4,7 @@ import re from .compat import basestring -def _indent(string, times=1): +def indent(string, times=1): """ A dumb version of :func:`textwrap.indent` from Python 3.3. @@ -13,7 +13,7 @@ def _indent(string, times=1): return "\n".join(" " * (4 * times) + line for line in string.splitlines()) -def _format_as_index(indices): +def format_as_index(indices): """ Construct a single string containing indexing operations for the indices. @@ -28,7 +28,7 @@ def _format_as_index(indices): return "[%s]" % "][".join(repr(index) for index in indices) -def _find_additional_properties(instance, schema): +def find_additional_properties(instance, schema): """ Return the set of additional properties for the given ``instance``. @@ -48,7 +48,7 @@ def _find_additional_properties(instance, schema): yield property -def _extras_msg(extras): +def extras_msg(extras): """ Create an error message for extra items or properties. @@ -61,7 +61,7 @@ def _extras_msg(extras): return ", ".join(repr(extra) for extra in extras), verb -def _types_msg(instance, types): +def types_msg(instance, types): """ Create an error message for a failure to match the given types. @@ -81,7 +81,7 @@ def _types_msg(instance, types): return "%r is not of type %s" % (instance, ", ".join(reprs)) -def _flatten(suitable_for_isinstance): +def flatten(suitable_for_isinstance): """ isinstance() can accept a bunch of really annoying different types: * a single type @@ -98,13 +98,13 @@ def _flatten(suitable_for_isinstance): suitable_for_isinstance = (suitable_for_isinstance,) for thing in suitable_for_isinstance: if isinstance(thing, tuple): - types.update(_flatten(thing)) + types.update(flatten(thing)) else: types.add(thing) return tuple(types) -def _list(thing): +def mklist(thing): """ Wrap ``thing`` in a list if it's a single str. @@ -117,9 +117,9 @@ def _list(thing): return thing -def _unbool(element, true=object(), false=object()): +def unbool(element, true=object(), false=object()): """ - A hack to make True and 1 and False and 0 unique for _uniq. + A hack to make True and 1 and False and 0 unique for ``uniq``. """ @@ -130,7 +130,7 @@ def _unbool(element, true=object(), false=object()): return element -def _uniq(container): +def uniq(container): """ Check if all of a container's elements are unique. @@ -141,10 +141,10 @@ def _uniq(container): """ try: - return len(set(_unbool(i) for i in container)) == len(container) + return len(set(unbool(i) for i in container)) == len(container) except TypeError: try: - sort = sorted(_unbool(i) for i in container) + sort = sorted(unbool(i) for i in container) sliced = itertools.islice(sort, 1, None) for i, j in zip(sort, sliced): if i == j: @@ -152,7 +152,7 @@ def _uniq(container): except (NotImplementedError, TypeError): seen = [] for e in container: - e = _unbool(e) + e = unbool(e) if e in seen: return False seen.append(e) |