<commit_before>import six
from python_jsonschema_objects import util


class ValidationError(Exception):
    pass

class ValidatorRegistry(object):

    def __init__(self):
       self.registry = {}

    def register(self, name=None):
       def f(functor):
          self.registry[name if name is not None else functor.__name__] = functor
          return functor
       return f

    def __call__(self, name):
       return self.registry.get(name)


registry = ValidatorRegistry()

@registry.register()
def multipleOf(param, value, _):
    quot, rem = divmod(value, param)
    if rem != 0:
        raise ValidationError(
            "{0} is not a multiple of {1}".format(value,
                                                   param))

@registry.register()
def enum(param, value, _):
    if value not in param:
        raise ValidationError(
            "{0} is not one of {1}".format(value, param))


@registry.register()
def minimum(param, value, type_data):
    exclusive = type_data.get('exclusiveMinimum')
    if exclusive:
        if value <= param:
            raise ValidationError(
                "{0} is less than or equal to {1}".format(value, param))
    elif value < param:
            raise ValidationError(
                "{0} is less than {1}".format(value, param))


@registry.register()
def maximum(param, value, type_data):
    exclusive = type_data.get('exclusiveMaximum')
    if exclusive:
        if value >= param:
            raise ValidationError(
                "{0} is greater than or equal to {1}".format(value, param))
    elif value > param:
            raise ValidationError(
                "{0} is greater than {1}".format(value, param))


@registry.register()
def maxLength(param, value, _):
    if len(value) > param:
        raise ValidationError(
            "{0} is longer than {1} characters".format(value, param))


@registry.register()
def minLength(param, value, _):
    if len(value) < param:
        raise ValidationError(
            "{0} is fewer than {1} characters".format(value, param))


@registry.register()
def pattern(param, value, _):
    import re
    match = re.search(param, value)
    if not match:
        raise ValidationError(
            "{0} does not match {1}".format(value, param)
        )


type_registry = ValidatorRegistry()

@type_registry.register(name='boolean')
def check_boolean_type(param, value, _):
    if not isinstance(value, bool):
        raise ValidationError(
            "{0} is not a boolean".format(value))
<commit_msg>implemented support for jsonschema 'format' field, based on the jsonschema python package. If the jsonschema package is installed on the machine, then we get format checking per the jsonschema implementation; else if the jsonschema package is NOT installed, then format validation does not occur (per the jsonschema Draft4 spec).<commit_after>import six
from python_jsonschema_objects import util


class ValidationError(Exception):
    pass

class ValidatorRegistry(object):

    def __init__(self):
       self.registry = {}

    def register(self, name=None):
       def f(functor):
          self.registry[name if name is not None else functor.__name__] = functor
          return functor
       return f

    def __call__(self, name):
       return self.registry.get(name)


registry = ValidatorRegistry()

@registry.register()
def multipleOf(param, value, _):
    quot, rem = divmod(value, param)
    if rem != 0:
        raise ValidationError(
            "{0} is not a multiple of {1}".format(value,
                                                   param))

@registry.register()
def enum(param, value, _):
    if value not in param:
        raise ValidationError(
            "{0} is not one of {1}".format(value, param))


@registry.register()
def minimum(param, value, type_data):
    exclusive = type_data.get('exclusiveMinimum')
    if exclusive:
        if value <= param:
            raise ValidationError(
                "{0} is less than or equal to {1}".format(value, param))
    elif value < param:
            raise ValidationError(
                "{0} is less than {1}".format(value, param))


@registry.register()
def maximum(param, value, type_data):
    exclusive = type_data.get('exclusiveMaximum')
    if exclusive:
        if value >= param:
            raise ValidationError(
                "{0} is greater than or equal to {1}".format(value, param))
    elif value > param:
            raise ValidationError(
                "{0} is greater than {1}".format(value, param))


@registry.register()
def maxLength(param, value, _):
    if len(value) > param:
        raise ValidationError(
            "{0} is longer than {1} characters".format(value, param))


@registry.register()
def minLength(param, value, _):
    if len(value) < param:
        raise ValidationError(
            "{0} is fewer than {1} characters".format(value, param))


@registry.register()
def pattern(param, value, _):
    import re
    match = re.search(param, value)
    if not match:
        raise ValidationError(
            "{0} does not match {1}".format(value, param)
        )


try:
    from jsonschema import FormatChecker
except ImportError:
    pass
else:
    @registry.register()
    def format(param, value, _):
        if not FormatChecker().conforms(value, param):
            raise ValidationError(
                "'{0}' is not formatted as a {1}".format(value, param)
            )


type_registry = ValidatorRegistry()

@type_registry.register(name='boolean')
def check_boolean_type(param, value, _):
    if not isinstance(value, bool):
        raise ValidationError(
            "{0} is not a boolean".format(value))

{'fraction_non_alphanumeric': 0.10873408769448373, 'fraction_numerical': 0.007602545968882602, 'mean_word_length': 2.5690851735015774, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2219930', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1578, 'n_words': 505}