<commit_before>import fields


class DuplicateFieldError(Exception):
    '''Error thrown when there are two different fields with the same name
    '''
    pass

class TransactionFailedError(Exception):
    '''Error thrown when datastore transaction failed
    '''
    pass

class NotSavedError(Exception):
    '''Thrown when trying to get key for object was not stored in database
    '''
    pass


class ModelBase(type):
    """Metaclass used to ORM class generation from definitions
    """

    def __new__(cls, name, bases, attrs):
        """
        """
        super_new = super(ModelBase, cls).__new__
        parents = [b for b in bases if isinstance(b, ModelBase)]
        if not parents:
            # If this isn't a subclass of Model, don't do anything special.
            return super_new(cls, name, bases, attrs)
        # 
        new_attrs = {}
        new_attrs['_fields'] = {}
        field_source = {}

        def get_attr_source(name, cls):
            for src_cls in cls.mro():
                if name in src_cls.__dict__:
                    return src_cls

        defined = set()
        for parent in parents:
            if hasattr(parent, '_fields'):
                field_keys = set(parent._fields.keys())
                duplicate_field_keys = defined & field_keys
                for dupe_field_name in duplicate_field_keys:
                    field_source[dupe_field_name] = get_attr_source(
                                dupe_field_name, field_source[dupe_field_name])
                    old_source = field_source[dupe_field_name]
                    new_source = get_attr_source(dupe_field_name, parent)
                    if old_source != new_source:
                        raise DuplicateFieldError('Duplicate field, %s, is '
                                'inherited from both %s and %s.'
                                % (dupe_field_name, old_source.__name__, 
                                new_source.__name__))
                field_keys -= duplicate_field_keys
                if field_keys:
                    defined |= field_keys
                    field_source.update(dict.fromkeys(field_keys, parent))
                    new_attrs['_fields'].update(parent._fields)

        new_attrs['_key_name'] = None

        for attr_name in attrs.keys():
            attr = attrs[attr_name]
            if isinstance(attr, fields.Field):
                #check_reserved_word(attr_name)
                if attr_name in defined:
                    raise DuplicateFieldError('Duplicate field: %s' % attr_name)
                defined.add(attr_name)
                new_attrs['_fields'][attr_name] = attr
                attr.__config__(cls, attr_name)
                new_attrs[attr_name] = None
            else:
                new_attrs[attr_name] = attr

        # Initialize properties
        new_attrs['_unindexed_properties'] = frozenset(
            new_attrs['_fields'][field].name
            for field in new_attrs['_fields']
            if not new_attrs['_fields'][field].primary_key and
               not new_attrs['_fields'][field].db_index)

        # Create class instance
        return super_new(cls, name, bases, new_attrs)


class Model(object):
    """Model is the superclass of all object entities in the datastore.
<commit_msg>Rewrite model superclass to keep class fields on their correct place<commit_after>import fields


class DuplicateFieldError(Exception):
    '''Error thrown when there are two different fields with the same name
    '''
    pass

class TransactionFailedError(Exception):
    '''Error thrown when datastore transaction failed
    '''
    pass

class NotSavedError(Exception):
    '''Thrown when trying to get key for object was not stored in database
    '''
    pass


class ModelBase(type):
    """Metaclass used to ORM class generation from definitions
    """

    def __new__(cls, name, bases, attrs):
        """
        """
        super_new = super(ModelBase, cls).__new__
        parents = [b for b in bases if isinstance(b, ModelBase)]
        if not parents:
            # If this isn't a subclass of Model, don't do anything special.
            return super_new(cls, name, bases, attrs)
        # 
        new_attrs = {}
        new_attrs['_fields'] = set()
        field_source = {}

        def get_attr_source(name, cls):
            for src_cls in cls.mro():
                if name in src_cls.__dict__:
                    return src_cls

        defined = set()
        for parent in parents:
            if hasattr(parent, '_fields'):
                duplicate_field_keys = defined & parent._fields
                for dupe_field_name in duplicate_field_keys:
                    field_source[dupe_field_name] = get_attr_source(
                                dupe_field_name, field_source[dupe_field_name])
                    old_source = field_source[dupe_field_name]
                    new_source = get_attr_source(dupe_field_name, parent)
                    if old_source != new_source:
                        raise DuplicateFieldError('Duplicate field, %s, is '
                                'inherited from both %s and %s.'
                                % (dupe_field_name, old_source.__name__, 
                                new_source.__name__))
                defined |= parent._fields
                field_source.update(dict.fromkeys(parent._fields, parent))
                new_attrs.update(parent._fields)

        new_attrs['_key_name'] = None

        for attr_name in attrs.keys():
            attr = attrs[attr_name]
            if isinstance(attr, fields.Field):
                if attr_name in defined:
                    raise DuplicateFieldError('Duplicate field: %s. It was ' 
                                'defined in %s already' % (attr_name, 
                                get_attr_source(attr_name, 
                                                field_source[attr_name])))
                defined.add(attr_name)
                attr.__config__(cls, attr_name)
            new_attrs[attr_name] = attr

        new_attrs['_fields'] = defined

        # Initialize properties
        new_attrs['_unindexed_properties'] = frozenset(
            new_attrs[field].name for field in new_attrs['_fields']
            if not (new_attrs[field].primary_key or new_attrs[field].db_index))

        # Create class instance
        return super_new(cls, name, bases, new_attrs)


class Model(object):
    """Model is the superclass of all object entities in the datastore.

{'fraction_non_alphanumeric': 0.07970570202329859, 'fraction_numerical': 0.0, 'mean_word_length': 1.810077519379845, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5275544', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1678, 'n_words': 456}