<commit_before>import re
import types
from copy import deepcopy, copy
from bson import ObjectId
from .document import Document
from .queries import ScopeBuilder
from .exceptions import NotFoundException
from .events import EventHandlerRegistrar
from .scopes import STANDARD_SCOPES


OBJECTIDEXPR = re.compile(r"^[a-fA-F0-9]{24}$")


class Model(Document):
    """
    Model base class on which all specific user model classes subclass.

    ** Do not attempt to subclass Model directly. **

    Instead use mongothon.create_model which will ensure the model
    subclass is correctly constructed with the appropriate collection
    and schema dependencies.
    """

    # Valid lifecycle states which a given Model instance may occupy.
    NEW = 1
    PERSISTED = 2
    DELETED = 3

    def __init__(self, inital_doc=None, initial_state=NEW):
        self._state = initial_state
        super(Model, self).__init__(inital_doc)
        self.emit('did_init')
        if initial_state == self.PERSISTED:
            self.emit('did_find')

    def _create_working(self):
        working = deepcopy(self)
        self.schema.apply_defaults(working)
        return working

    @classmethod
    def handler_registrar(cls):
        if not hasattr(cls, '_handler_registrar'):
            cls._handler_registrar = EventHandlerRegistrar()
        return cls._handler_registrar

    @classmethod
    def _ensure_object_id(cls, id):
        """Checks whether the given id is an ObjectId instance, and if not wraps it."""
        if isinstance(id, ObjectId):
            return id

        if isinstance(id, basestring) and OBJECTIDEXPR.match(id):
            return ObjectId(id)

        return id

    @classmethod
    def _id_spec(cls, id):
        return {'_id': cls._ensure_object_id(id)}

    def is_new(self):
        """Returns true if the current model instance is new and has yet to be
        persisted to the underlying Mongo collection."""
        return self._state == Model.NEW

    def is_persisted(self):
        """Returns true if the model instance exists in the database."""
        return self._state == Model.PERSISTED

    def is_deleted(self):
        """Returns true if the model instance was deleted from the database."""
        return self._state == Model.DELETED

    def validate(self):
        """Validates this model against the schema with which it was constructed.
        Throws a ValidationException if the document is found to be invalid."""
        self._do_validate(self._create_working())

    def _do_validate(self, document):
        self._emit('will_validate', document)
        self.schema.validate(document)
        self._emit('did_validate', document)

    def apply_defaults(self):
        """Apply schema defaults to this document."""
        self.emit('will_apply_defaults')
        self.schema.apply_defaults(self)
        self.emit('did_apply_defaults')

    def save(self, *args, **kwargs):
        # Create a working copy of ourselves and validate it
        working = self._create_working()
        self._do_validate(working)

        self._emit('will_save', working)

        # Attempt to save
        self.collection.save(working, *args, **kwargs)
        self._state = Model.PERSISTED

        self._emit('did_save', working)

        # On successful completion, update from the working copy
        self.populate(working)

    @classmethod
    def insert(cls, *args, **kwargs):
        cls.collection.insert(*args, **kwargs)

    def update_instance(self, *args, **kwargs):
        self.emit('will_update', *args, **kwargs)
        result = self.__class__.update({'_id': self['_id']}, *args, **kwargs)
        self.emit('did_update', *args, **kwargs)
        return result

    @classmethod
    def update(cls, *args, **kwargs):
        return cls.collection.update(*args, **kwargs)

    def __getattribute__(self, name):
        if name == 'update':
            return lambda *args, **kwargs: super(Model, self).update(*args, **kwargs)
        return super(Model, self).__getattribute__(name)

    def remove(self, *args, **kwargs):
        self.emit('will_remove', *args, **kwargs)
        self.collection.remove(self['_id'], *args, **kwargs)
        self.emit('did_remove', *args, **kwargs)
        self._state = Model.DELETED

    @classmethod
    def count(cls):
        return cls.collection.count()

    @classmethod
    def find_one(cls, *args, **kwargs):
        return cls(cls.collection.find_one(*args, **kwargs),
                   initial_state=Model.PERSISTED)

    @classmethod
    def find(cls, *args, **kwargs):
        return CursorWrapper(cls.collection.find(*args, **kwargs), cls)

    @classmethod
    def find_by_id(cls, id):
        """
        Finds a single document by its ID. Throws a
        NotFoundException if the document does not exist (the
        assumption being if you've got an id you should be
        pretty certain the thing exists)
        """
        obj = cls.find_one(cls._id_spec(id))
        if not obj:
            raise NotFoundException(cls.collection, id)
        return obj

    def reload(self):
        """Reloads the current model's data from the underlying
        database record, updating it in-place."""
        self.emit('will_reload')
        self.populate(self.collection.find_one(self.__class__._id_spec(self['_id'])))
        self.emit('did_reload')<commit_msg>use `type(Model())` to get the class of a Model() (as opposed to Model().__class__)<commit_after>import re
import types
from copy import deepcopy, copy
from bson import ObjectId
from .document import Document
from .queries import ScopeBuilder
from .exceptions import NotFoundException
from .events import EventHandlerRegistrar
from .scopes import STANDARD_SCOPES


OBJECTIDEXPR = re.compile(r"^[a-fA-F0-9]{24}$")


class Model(Document):
    """
    Model base class on which all specific user model classes subclass.

    ** Do not attempt to subclass Model directly. **

    Instead use mongothon.create_model which will ensure the model
    subclass is correctly constructed with the appropriate collection
    and schema dependencies.
    """

    # Valid lifecycle states which a given Model instance may occupy.
    NEW = 1
    PERSISTED = 2
    DELETED = 3

    def __init__(self, inital_doc=None, initial_state=NEW):
        self._state = initial_state
        super(Model, self).__init__(inital_doc)
        self.emit('did_init')
        if initial_state == self.PERSISTED:
            self.emit('did_find')

    def _create_working(self):
        working = deepcopy(self)
        self.schema.apply_defaults(working)
        return working

    @classmethod
    def handler_registrar(cls):
        if not hasattr(cls, '_handler_registrar'):
            cls._handler_registrar = EventHandlerRegistrar()
        return cls._handler_registrar

    @classmethod
    def _ensure_object_id(cls, id):
        """Checks whether the given id is an ObjectId instance, and if not wraps it."""
        if isinstance(id, ObjectId):
            return id

        if isinstance(id, basestring) and OBJECTIDEXPR.match(id):
            return ObjectId(id)

        return id

    @classmethod
    def _id_spec(cls, id):
        return {'_id': cls._ensure_object_id(id)}

    def is_new(self):
        """Returns true if the current model instance is new and has yet to be
        persisted to the underlying Mongo collection."""
        return self._state == Model.NEW

    def is_persisted(self):
        """Returns true if the model instance exists in the database."""
        return self._state == Model.PERSISTED

    def is_deleted(self):
        """Returns true if the model instance was deleted from the database."""
        return self._state == Model.DELETED

    def validate(self):
        """Validates this model against the schema with which it was constructed.
        Throws a ValidationException if the document is found to be invalid."""
        self._do_validate(self._create_working())

    def _do_validate(self, document):
        self._emit('will_validate', document)
        self.schema.validate(document)
        self._emit('did_validate', document)

    def apply_defaults(self):
        """Apply schema defaults to this document."""
        self.emit('will_apply_defaults')
        self.schema.apply_defaults(self)
        self.emit('did_apply_defaults')

    def save(self, *args, **kwargs):
        # Create a working copy of ourselves and validate it
        working = self._create_working()
        self._do_validate(working)

        self._emit('will_save', working)

        # Attempt to save
        self.collection.save(working, *args, **kwargs)
        self._state = Model.PERSISTED

        self._emit('did_save', working)

        # On successful completion, update from the working copy
        self.populate(working)

    @classmethod
    def insert(cls, *args, **kwargs):
        cls.collection.insert(*args, **kwargs)

    def update_instance(self, *args, **kwargs):
        self.emit('will_update', *args, **kwargs)
        result = type(self).update({'_id': self['_id']}, *args, **kwargs)
        self.emit('did_update', *args, **kwargs)
        return result

    @classmethod
    def update(cls, *args, **kwargs):
        return cls.collection.update(*args, **kwargs)

    def __getattribute__(self, name):
        if name == 'update':
            return lambda *args, **kwargs: super(Model, self).update(*args, **kwargs)
        return super(Model, self).__getattribute__(name)

    def remove(self, *args, **kwargs):
        self.emit('will_remove', *args, **kwargs)
        self.collection.remove(self['_id'], *args, **kwargs)
        self.emit('did_remove', *args, **kwargs)
        self._state = Model.DELETED

    @classmethod
    def count(cls):
        return cls.collection.count()

    @classmethod
    def find_one(cls, *args, **kwargs):
        return cls(cls.collection.find_one(*args, **kwargs),
                   initial_state=Model.PERSISTED)

    @classmethod
    def find(cls, *args, **kwargs):
        return CursorWrapper(cls.collection.find(*args, **kwargs), cls)

    @classmethod
    def find_by_id(cls, id):
        """
        Finds a single document by its ID. Throws a
        NotFoundException if the document does not exist (the
        assumption being if you've got an id you should be
        pretty certain the thing exists)
        """
        obj = cls.find_one(cls._id_spec(id))
        if not obj:
            raise NotFoundException(cls.collection, id)
        return obj

    def reload(self):
        """Reloads the current model's data from the underlying
        database record, updating it in-place."""
        self.emit('will_reload')
        self.populate(self.collection.find_one(type(self)._id_spec(self['_id'])))
        self.emit('did_reload')
{'fraction_non_alphanumeric': 0.10470283280873913, 'fraction_numerical': 0.0012960562858729864, 'mean_word_length': 2.787868162692847, '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': '3555193', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2993, 'n_words': 905}