<commit_before>"""Parameter Parsing and Validation for Chassis Applications."""

import re
import six


class ValidationError(Exception):
    """Raised when validation fails."""


class BaseValidator(object):
    """Base class for validators."""

    def __init__(self):
        self.documentation = "Validated Parameter."
        self.message = "Validation failed."

    def fail(self):
        """Raise a validation error with the messsage."""
        raise ValidationError(self.message)

    def validate(self, unused_value, unused_handler):
        """Override this to implement validation logic.

        Return the validated value or call self.fail() to raise a
        ValidationError.
        """
        raise NotImplementedError


class Boolean(BaseValidator):
    """Validates Boolean inputs for truthiness."""

    truthy = ['true', '1', 'yes', 't', 'y', True, 1]
    falsy = ['false', '0', 'no', 'f', 'n', False, 0]

    def __init__(self):
        super(Boolean, self).__init__()
        self.documentation = "Truthy value: Prefered `true` or `false`"
        self.message = "Valid boolean required"

    def validate(self, value, unused_handler):
        if isinstance(value, six.string_types):
            value = value.lower()

        if value in self.truthy:
            return True

        if value in self.falsy:
            return False

        self.fail()


class String(BaseValidator):
    """Validates strings with optional length requirements."""


    def __init__(self, min_length=None, max_length=None):
        self.min_length = min_length
        self.max_length = max_length

        super(String, self).__init__()

        # TODO: Overwrite self.message based on parameters.
        self.documentation = "String."
        self.message = "Valid string required."

    def validate(self, value, unused_handler):
        if not isinstance(value, six.string_types):
            self.fail()

        length = len(value)

        if self.min_length is not None:
            if length < self.min_length:
                self.fail()

        if self.max_length is not None:
            if length > self.max_length:
                self.fail()

        return value


class Regex(BaseValidator):
    """Validates a string agains a regular expression."""


    def __init__(self, regex):
        self.pattern = re.compile(regex)

        super(Regex, self).__init__()

        self.documentation = "Regex."
        self.message = "String matching regular expression required."

    def validate(self, value, unused_handler):
        match = self.pattern.match(value)

        if match and value == match.group():
            return value

        self.fail()


class Number(BaseValidator):
    """Validates floating point numbers with optional length requirements."""

    def __init__(self, minimum=None, maximum=None):

        self.minimum = minimum
        self.maximum = maximum

        super(Number, self).__init__()

        self.documentation = "Number."
        self.message = "Valid number required."


    def validate(self, value, unused_handler):
        try:
            value = float(value)<commit_msg>Move super() calls to the tops of methods<commit_after>"""Parameter Parsing and Validation for Chassis Applications."""

import re
import six


class ValidationError(Exception):
    """Raised when validation fails."""


class BaseValidator(object):
    """Base class for validators."""

    def __init__(self):
        self.documentation = "Validated Parameter."
        self.message = "Validation failed."

    def fail(self):
        """Raise a validation error with the messsage."""
        raise ValidationError(self.message)

    def validate(self, unused_value, unused_handler):
        """Override this to implement validation logic.

        Return the validated value or call self.fail() to raise a
        ValidationError.
        """
        raise NotImplementedError


class Boolean(BaseValidator):
    """Validates Boolean inputs for truthiness."""

    truthy = ['true', '1', 'yes', 't', 'y', True, 1]
    falsy = ['false', '0', 'no', 'f', 'n', False, 0]

    def __init__(self):
        super(Boolean, self).__init__()
        self.documentation = "Truthy value: Prefered `true` or `false`"
        self.message = "Valid boolean required"

    def validate(self, value, unused_handler):
        if isinstance(value, six.string_types):
            value = value.lower()

        if value in self.truthy:
            return True

        if value in self.falsy:
            return False

        self.fail()


class String(BaseValidator):
    """Validates strings with optional length requirements."""


    def __init__(self, min_length=None, max_length=None):
        super(String, self).__init__()

        # TODO: Overwrite self.message based on parameters.
        self.documentation = "String."
        self.message = "Valid string required."
        self.min_length = min_length
        self.max_length = max_length

    def validate(self, value, unused_handler):
        if not isinstance(value, six.string_types):
            self.fail()

        length = len(value)

        if self.min_length is not None:
            if length < self.min_length:
                self.fail()

        if self.max_length is not None:
            if length > self.max_length:
                self.fail()

        return value


class Regex(BaseValidator):
    """Validates a string agains a regular expression."""


    def __init__(self, regex):
        super(Regex, self).__init__()
        self.documentation = "Regex."
        self.message = "String matching regular expression required."
        self.pattern = re.compile(regex)

    def validate(self, value, unused_handler):
        match = self.pattern.match(value)

        if match and value == match.group():
            return value

        self.fail()


class Number(BaseValidator):
    """Validates floating point numbers with optional length requirements."""

    def __init__(self, minimum=None, maximum=None):
        super(Number, self).__init__()
        self.documentation = "Number."
        self.message = "Valid number required."
        self.minimum = minimum
        self.maximum = maximum


    def validate(self, value, unused_handler):
        try:
            value = float(value)
{'fraction_non_alphanumeric': 0.10684844641724794, 'fraction_numerical': 0.0012682308180088776, 'mean_word_length': 2.4626783754116355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2069641', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1625, 'n_words': 520}