mirror of
https://github.com/explosion/spaCy.git
synced 2024-11-13 13:17:06 +03:00
f37863093a
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉 See here: https://github.com/explosion/srsly Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place. At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel. srsly currently includes forks of the following packages: ujson msgpack msgpack-numpy cloudpickle * WIP: replace json/ujson with srsly * Replace ujson in examples Use regular json instead of srsly to make code easier to read and follow * Update requirements * Fix imports * Fix typos * Replace msgpack with srsly * Fix warning
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
# coding: utf-8
|
|
from __future__ import unicode_literals
|
|
|
|
from pathlib import Path
|
|
from jsonschema import Draft4Validator
|
|
import srsly
|
|
|
|
from ...errors import Errors
|
|
|
|
|
|
SCHEMAS = {}
|
|
|
|
|
|
def get_schema(name):
|
|
"""Get the JSON schema for a given name. Looks for a .json file in
|
|
spacy.cli.schemas, validates the schema and raises ValueError if not found.
|
|
|
|
EXAMPLE:
|
|
>>> schema = get_schema('training')
|
|
|
|
name (unicode): The name of the schema.
|
|
RETURNS (dict): The JSON schema.
|
|
"""
|
|
if name not in SCHEMAS:
|
|
schema_path = Path(__file__).parent / "{}.json".format(name)
|
|
if not schema_path.exists():
|
|
raise ValueError(Errors.E104.format(name=name))
|
|
schema = srsly.read_json(schema_path)
|
|
# TODO: replace with (stable) Draft6Validator, if available
|
|
validator = Draft4Validator(schema)
|
|
validator.check_schema(schema)
|
|
SCHEMAS[name] = schema
|
|
return SCHEMAS[name]
|
|
|
|
|
|
def validate_json(data, schema):
|
|
"""Validate data against a given JSON schema (see https://json-schema.org).
|
|
|
|
data: JSON-serializable data to validate.
|
|
schema (dict): The JSON schema.
|
|
RETURNS (list): A list of error messages, if available.
|
|
"""
|
|
validator = Draft4Validator(schema)
|
|
errors = []
|
|
for err in sorted(validator.iter_errors(data), key=lambda e: e.path):
|
|
if err.path:
|
|
err_path = "[{}]".format(" -> ".join([str(p) for p in err.path]))
|
|
else:
|
|
err_path = ""
|
|
errors.append(err.message + " " + err_path)
|
|
return errors
|