Skip to content

Make better loop performance more intuitive #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
language: python
sudo: false
addons:
apt:
sources:
- deadsnakes
packages:
- python3.5
python:
- "2.7"
- "3.5"
- "pypy"
- "pypy3"
install:
- pip install tox
- pip install tox-travis
script:
- tox
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ for Python (supporting 2.7+ including Python 3).

.. code-block:: python

>>> from jsonschema import validate
>>> from jsonschema import Schema

>>> # A sample schema, like what we'd get from json.load()
>>> schema = {
>>> schema = Schema({
... "type" : "object",
... "properties" : {
... "price" : {"type" : "number"},
... "name" : {"type" : "string"},
... },
... }
... })

>>> # If no exception is raised by validate(), the instance is valid.
>>> validate({"name" : "Eggs", "price" : 34.99}, schema)
>>> schema.validate({"name" : "Eggs", "price" : 34.99})

>>> validate(
... {"name" : "Eggs", "price" : "Invalid"}, schema
>>> schema.validate(
... {"name" : "Eggs", "price" : "Invalid"}
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
Expand Down
12 changes: 6 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ for Python (supporting 2.7+ including Python 3).

.. code-block:: python

>>> from jsonschema import validate
>>> from jsonschema import Schema

>>> # A sample schema, like what we'd get from json.load()
>>> schema = {
>>> schema = Schema({
... "type" : "object",
... "properties" : {
... "price" : {"type" : "number"},
... "name" : {"type" : "string"},
... },
... }
... })

>>> # If no exception is raised by validate(), the instance is valid.
>>> validate({"name" : "Eggs", "price" : 34.99}, schema)
>>> schema.validate({"name" : "Eggs", "price" : 34.99})

>>> validate(
... {"name" : "Eggs", "price" : "Invalid"}, schema
>>> schema.validate(
... {"name" : "Eggs", "price" : "Invalid"}
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
Expand Down
2 changes: 1 addition & 1 deletion jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
FormatChecker, draft3_format_checker, draft4_format_checker,
)
from jsonschema.validators import (
Draft3Validator, Draft4Validator, RefResolver, validate
Draft3Validator, Draft4Validator, RefResolver, validate, Schema
)

from jsonschema._version import __version__
Expand Down
6 changes: 3 additions & 3 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __unicode__(self):
__str__ = __unicode__
else:
def __str__(self):
return unicode(self).encode("utf-8")
return self.__unicode__().encode("utf-8")

@classmethod
def create_from(cls, other):
Expand Down Expand Up @@ -158,7 +158,7 @@ def __unicode__(self):
__str__ = __unicode__
else:
def __str__(self):
return unicode(self).encode("utf-8")
return self.__unicode__().encode("utf-8")


class FormatError(Exception):
Expand All @@ -174,7 +174,7 @@ def __unicode__(self):
__str__ = __unicode__
else:
def __str__(self):
return self.message.encode("utf-8")
return self.__unicode__().encode("utf-8")


class ErrorTree(object):
Expand Down
9 changes: 9 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from jsonschema.validators import (
RefResolutionError, UnknownType, Draft3Validator,
Draft4Validator, RefResolver, create, extend, validator_for, validate,
Schema,
)


Expand Down Expand Up @@ -759,6 +760,14 @@ def test_draft4_validator_is_the_default(self):
chk_schema.assert_called_once_with({})


class TestSchemaCls(unittest.TestCase):
def test_draft4_validator_is_the_default_for_schema_cls(self):
with mock.patch.object(Draft4Validator, "check_schema") as chk_schema:
schema = Schema({})
schema.validate({})
chk_schema.assert_called_once_with({})


class TestRefResolver(unittest.TestCase):

base_uri = ""
Expand Down
33 changes: 33 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,36 @@ def validate(instance, schema, cls=None, *args, **kwargs):
cls = validator_for(schema)
cls.check_schema(schema)
cls(schema, *args, **kwargs).validate(instance)


class Schema(object):
"""
Similar to :func:`validate`, except the :class:`IValidator` is initialized
only once when this class is initialized. This can be useful for improving
the performance of validation in loops.

:argument schema: the schema to validate with
:argument validator_cls: an :class:`IValidator` class that will be used by
:meth:`validate` to validate the instance.

:raises:
:exc:`SchemaError` if the schema itself is invalid
"""

def __init__(self, schema, validator_cls=None, *args, **kwargs):
if validator_cls is None:
validator_cls = validator_for(schema)
validator_cls.check_schema(schema)
self.validator = validator_cls(schema, *args, **kwargs)

def validate(self, instance):
"""
Validate an instance with the schema and :class:`IValidator` class used
in this instance of :class:`Schema`.

:argument instance: the instance to validate

:raises:
:exc:`ValidationError` if the instance is invalid
"""
self.validator.validate(instance)
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[tox]
envlist = py{27,35,py,py3}, docs, style

[tox:travis]
2.7 = py27, style
3.5 = py35, docs, style

[testenv]
setenv =
Expand Down