<commit_before>import trafaret as t


class TrafaretValidator(object):
    _validators = {}
    _errors = {}

    def __new__(cls, *args, **kwargs):
        instance = object.__new__(cls)
        validators = {}
        for attr_name in instance.__class__.__dict__:
            field = getattr(instance, attr_name)
            if isinstance(field, t.Trafaret):
                validators[attr_name] = field

        setattr(instance, '_validators', validators)
        return instance

    def __init__(self, **kwargs):
        self._params = self._prepare_params(kwargs)

    def __setattr__(self, name, value):
        if isinstance(value, t.Trafaret):
            self._validators[name] = value

        object.__setattr__(self, name, value)

    def _prepare_params(self, params):
        prepared_params = {}
        for attr_name in self._validators.keys():
            prepared_params[attr_name] = params.get(attr_name)
        return prepared_params

    @property
    def validators(self):
        return self._validators.copy()

    @property
    def trafaret(self):
        return t.Dict(**self._validators)

    @property
    def errors(self):
        return self._errors.copy()

    @property
    def params(self):
        return self._params.copy()

    def validate(self):
        try:
            self.trafaret.check(self.params)
            return True
        except t.DataError as error:
            self._errors = error.as_dict()
            return False<commit_msg>added trafaret subclass, type support<commit_after>import inspect

import trafaret as t


class TrafaretValidator:
    _validators = {}
    _errors = {}

    def __new__(cls, *args, **kwargs):
        instance = object.__new__(cls)
        validators = {}
        for attr_name in instance.__class__.__dict__:
            value = getattr(instance, attr_name)
            trafaret_instance = cls._prepare_trafaret_instance(value)
            if trafaret_instance:
                validators[attr_name] = trafaret_instance

        setattr(instance, '_validators', validators)
        return instance

    def __init__(self, **kwargs):
        self._params = self._prepare_params(kwargs)

    def __setattr__(self, name, value):
        trafaret_instance = TrafaretValidator._prepare_trafaret_instance(value)
        if trafaret_instance:
            self._validators[name] = trafaret_instance

        object.__setattr__(self, name, value)

    @staticmethod
    def _prepare_trafaret_instance(value):
        if isinstance(value, t.Trafaret) or inspect.isroutine(value):
            return value
        elif issubclass(value.__class__, t.Trafaret.__class__):
            return value()
        elif isinstance(value, type):
            return t.Type(value)
        return

    def _prepare_params(self, params):
        prepared_params = {}
        for attr_name in self._validators.keys():
            prepared_params[attr_name] = params.get(attr_name)
        return prepared_params

    @property
    def validators(self):
        return self._validators.copy()

    @property
    def trafaret(self):
        return t.Dict(**self._validators)

    @property
    def errors(self):
        return self._errors.copy()

    @property
    def params(self):
        return self._params.copy()

    def validate(self):
        try:
            self.trafaret.check(self.params)
            return True
        except t.DataError as error:
            self._errors = error.as_dict()
            return False
{'fraction_non_alphanumeric': 0.09145642201834862, 'fraction_numerical': 0.0, 'mean_word_length': 2.329198473282443, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1341438', 'n_tokens_mistral': 998, 'n_tokens_neox': 968, 'n_words': 218}