<commit_before>import json
import uuid


MAX_ITEMS_KEY = 'max_items'
MINIMUM_KEY = 'minimum'
EXCLUSIVE_MINIMUM_KEY = 'exclusive_minimum'


class DefinitionError(Exception):
    pass


class JSchema(object):
    FIELD_NAMES = {
        # meta
        'type': 'type',
        'title': 'title',
        'description': 'description',
        'default': 'default',
        # array
        'additional_items': 'additionalItems',
        'items': 'items',
        'max_items': 'maxItems',
        'min_items': 'minItems',
        'unique_items': 'uniqueItems',
        # integer, number
        'multiple_of': 'multipleOf',
        'maximum': 'maximum',
        'exclusive_maximum': 'exclusiveMaximum',
        'minimum': 'minimum',
        'exclusive_minimum': 'exclusiveMinimum',
        # object
        'max_properties': 'maxProperties',
        'min_properties': 'minProperties',
        'required': 'required',
        'additional_properties': 'additionalProperties',
        'properties': 'properties',
        'pattern_properties': 'patternProperties',
        'dependencies': 'dependencies',
        # string
        'max_length': 'maxLength',
        'min_length': 'minLength',
        'pattern': 'pattern',
        # all
        'definitions': 'definitions',
        'all_of': 'allOf',
        'any_of': 'anyOf',
        'one_of': 'oneOf'
    }

    def __init__(self, **kwargs):
        max_items = kwargs.get(MAX_ITEMS_KEY, None)
        self.validate_max_items(max_items)

        minimum = kwargs.get(MINIMUM_KEY, None)
        exclusive_minimum = kwargs.get(EXCLUSIVE_MINIMUM_KEY, None)
        self.validate_minimum(minimum, exclusive_minimum)

        self._optional = kwargs.pop('optional', False)
        schema = {}
        for field, field_name in self.FIELD_NAMES.items():
            if field in kwargs:
                schema[field_name] = kwargs[field]
        if 'ref' in kwargs:
            self._dict = {
                'definitions': schema.pop('definitions', {}),
                '$ref': '#/definitions/{0}'.format(kwargs['ref'])
            }
            self._dict['definitions'][kwargs['ref']] = schema
        else:
            self._dict = schema

    def validate_max_items(self, max_items):
        if max_items is None:
            return
        if not isinstance(max_items, int):
            raise DefinitionError("'max_items' must be an integer")
        if max_items < 0:
            raise DefinitionError("'max_items' must be gte zero")

    def validate_minimum(self, minimum, exclusive_minimum):
        if minimum is not None:
            if not isinstance(minimum, (int, float)):
                raise DefinitionError(
                    "'{0}' must be a number".format(MINIMUM_KEY)
                )
        if exclusive_minimum is not None:
            if not isinstance(exclusive_minimum, bool):
                raise DefinitionError(
                    "'{0}' must be a boolean".format(EXCLUSIVE_MINIMUM_KEY)
                )
            if minimum is None:
                raise DefinitionError(
                    "'{0}' must be present if '{1}' is defined".format(
                        MINIMUM_KEY, EXCLUSIVE_MINIMUM_KEY
                    )
                )

    @property
    def optional(self):
        return self._optional

    @property
    def definitions(self):<commit_msg>Validation functions are now module-level<commit_after>import json
import uuid


MAX_ITEMS_KEY = 'max_items'
MINIMUM_KEY = 'minimum'
EXCLUSIVE_MINIMUM_KEY = 'exclusive_minimum'


def validate_max_items(max_items):
    if max_items is None:
        return
    if not isinstance(max_items, int):
        raise DefinitionError("'{0}' must be an integer".format(MAX_ITEMS_KEY))
    if max_items < 0:
        raise DefinitionError("'{0}' must be gte zero".format(MAX_ITEMS_KEY))


def validate_minimum(minimum, exclusive_minimum):
    if minimum is not None:
        if not isinstance(minimum, (int, float)):
            raise DefinitionError("'{0}' must be a number".format(MINIMUM_KEY))
    if exclusive_minimum is not None:
        if not isinstance(exclusive_minimum, bool):
            raise DefinitionError(
                "'{0}' must be a boolean".format(EXCLUSIVE_MINIMUM_KEY)
            )
        if minimum is None:
            raise DefinitionError(
                "'{0}' must be present if '{1}' is defined".format(
                    MINIMUM_KEY, EXCLUSIVE_MINIMUM_KEY
                )
            )


class DefinitionError(Exception):
    pass


class JSchema(object):
    FIELD_NAMES = {
        # meta
        'type': 'type',
        'title': 'title',
        'description': 'description',
        'default': 'default',
        # array
        'additional_items': 'additionalItems',
        'items': 'items',
        'max_items': 'maxItems',
        'min_items': 'minItems',
        'unique_items': 'uniqueItems',
        # integer, number
        'multiple_of': 'multipleOf',
        'maximum': 'maximum',
        'exclusive_maximum': 'exclusiveMaximum',
        'minimum': 'minimum',
        'exclusive_minimum': 'exclusiveMinimum',
        # object
        'max_properties': 'maxProperties',
        'min_properties': 'minProperties',
        'required': 'required',
        'additional_properties': 'additionalProperties',
        'properties': 'properties',
        'pattern_properties': 'patternProperties',
        'dependencies': 'dependencies',
        # string
        'max_length': 'maxLength',
        'min_length': 'minLength',
        'pattern': 'pattern',
        # all
        'definitions': 'definitions',
        'all_of': 'allOf',
        'any_of': 'anyOf',
        'one_of': 'oneOf'
    }

    def __init__(self, **kwargs):
        max_items = kwargs.get(MAX_ITEMS_KEY, None)
        validate_max_items(max_items)

        minimum = kwargs.get(MINIMUM_KEY, None)
        exclusive_minimum = kwargs.get(EXCLUSIVE_MINIMUM_KEY, None)
        validate_minimum(minimum, exclusive_minimum)

        self._optional = kwargs.pop('optional', False)
        schema = {}
        for field, field_name in self.FIELD_NAMES.items():
            if field in kwargs:
                schema[field_name] = kwargs[field]
        if 'ref' in kwargs:
            self._dict = {
                'definitions': schema.pop('definitions', {}),
                '$ref': '#/definitions/{0}'.format(kwargs['ref'])
            }
            self._dict['definitions'][kwargs['ref']] = schema
        else:
            self._dict = schema

    @property
    def optional(self):
        return self._optional

    @property
    def definitions(self):
{'fraction_non_alphanumeric': 0.11847694174757281, 'fraction_numerical': 0.00212378640776699, 'mean_word_length': 2.191190706679574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '851512', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1847, 'n_words': 422}