<commit_before>from collections import Iterable

from esengine.exceptions import RequiredField, InvalidMultiField
from esengine.exceptions import FieldTypeMismatch, ValidationError


class BaseField(object):
    _type = unicode
    _default_mapping = {'type': 'string'}

    def __init__(self, field_type=None, required=False, multi=False,
                 field_name=None, validators=None, mapping=None, **kwargs):
        self._validators = validators or []
        self._field_name = field_name
        self._mapping = mapping or {}
        if field_type is not None:
            self._type = field_type
        self._required = required or getattr(self, '_required', False)
        self._multi = multi or getattr(self, '_multi', False)
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

    def validate(self, value):
        if value is None:
            if self._required:
                raise RequiredField(self._field_name)
        else:
            if self._multi:
                if not isinstance(value, Iterable):
                    raise InvalidMultiField(self._field_name)
                for elem in value:
                    if not isinstance(elem, self._type):
                        raise FieldTypeMismatch(self._field_name, self._type,
                                                elem.__class__)
            else:
                if not isinstance(value, self._type):
                    raise FieldTypeMismatch(self._field_name, self._type,
                                            value.__class__)
        for validator in self._validators:
            """
            Functions in self._validators receives field_name, value
            should return None or<commit_msg>validate_field_type turned to method<commit_after>from collections import Iterable

from esengine.exceptions import RequiredField, InvalidMultiField
from esengine.exceptions import FieldTypeMismatch, ValidationError


class BaseField(object):
    _type = unicode
    _default_mapping = {'type': 'string'}

    def __init__(self, field_type=None, required=False, multi=False,
                 field_name=None, validators=None, mapping=None, **kwargs):
        self._validators = validators or []
        self._field_name = field_name
        self._mapping = mapping or {}
        if field_type is not None:
            self._type = field_type
        self._required = required or getattr(self, '_required', False)
        self._multi = multi or getattr(self, '_multi', False)
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

    def validate_field_type(self, value):
        if value is not None:
            if not isinstance(value, self._type):
                raise FieldTypeMismatch(self._field_name,
                                        self._type,
                                        value.__class__)

    def validate(self, value):
        if value is None:
            if self._required:
                raise RequiredField(self._field_name)
        else:
            if self._multi:
                if not isinstance(value, Iterable):
                    raise InvalidMultiField(self._field_name)
                for elem in value:
                    if not isinstance(elem, self._type):
                        raise FieldTypeMismatch(self._field_name, self._type,
                                                elem.__class__)
            else:
                if not isinstance(value, self._type):
                    raise FieldTypeMismatch(self._field_name, self._type,
                                            value.__class__)
        for validator in self._validators:
            """
            Functions in self._validators receives field_name, value
            should return None or
{'fraction_non_alphanumeric': 0.07208872458410351, 'fraction_numerical': 0.0, 'mean_word_length': 1.7873436350257543, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '735442', 'n_tokens_mistral': 914, 'n_tokens_neox': 885, 'n_words': 258}