<commit_before>from jsrn import exceptions
from jsrn.resources import create_resource_from_dict
from jsrn.fields import Field
from jsrn.validators import EMPTY_VALUES

__all__ = ('ObjectAs', 'ArrayOf',)


class ObjectAs(Field):
    default_error_messages = {
        'invalid': "Must be a object of type ``%r``.",
    }

    def __init__(self, of, **kwargs):
        try:
            of._meta
        except AttributeError:
            raise TypeError("``%r`` is not a valid type for a related field." % of)
        self.of = of

        super(ObjectAs, self).__init__(**kwargs)

    def to_python(self, value):
        if value is None:
            return None
        if isinstance(value, self.of):
            return value
        if isinstance(value, dict):
            return create_resource_from_dict(value, self.of._meta.resource_name)
        msg = self.error_messages['invalid'] % self.of<commit_msg>Set default item to be an instance of the "of"<commit_after>from jsrn import exceptions
from jsrn.resources import create_resource_from_dict
from jsrn.fields import Field
from jsrn.validators import EMPTY_VALUES

__all__ = ('ObjectAs', 'ArrayOf',)


class ObjectAs(Field):
    default_error_messages = {
        'invalid': "Must be a object of type ``%r``.",
    }

    def __init__(self, of, **kwargs):
        try:
            of._meta
        except AttributeError:
            raise TypeError("``%r`` is not a valid type for a related field." % of)
        self.of = of

        kwargs.setdefault('default', lambda:of())
        super(ObjectAs, self).__init__(**kwargs)

    def to_python(self, value):
        if value is None:
            return None
        if isinstance(value, self.of):
            return value
        if isinstance(value, dict):
            return create_resource_from_dict(value, self.of._meta.resource_name)
        msg = self.error_messages['invalid'] % self.of
{'fraction_non_alphanumeric': 0.10257759074171488, 'fraction_numerical': 0.0, 'mean_word_length': 2.6228571428571428, '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': '2107479', 'n_tokens_mistral': 559, 'n_tokens_neox': 550, 'n_words': 164}