<commit_before>from abc import ABCMeta, abstractproperty
from functools import reduce
from six import with_metaclass
from eventsourcing.domain.model.entity import EventSourcedEntity
from eventsourcing.infrastructure.event_store import EventStore
from eventsourcing.infrastructure.stored_events.base import recreate_domain_event


class EventPlayer(object):

    def __init__(self, event_store, domain_class):
        assert isinstance(event_store, EventStore), event_store
        assert issubclass(domain_class, EventSourcedEntity), domain_class
        self.event_store = event_store
        self.domain_class = domain_class

    def __getitem__(self, entity_id):
        # Get the stored events from the event store.
        stored_entity_id = self.domain_class.prefix_id(entity_id)
        stored_events = self.event_store.stored_event_repo.get_entity_events(stored_entity_id=stored_entity_id)

        # Recreate the domain events from the stored events.
        domain_events = map(recreate_domain_event, stored_events)

        # Successively apply the domain events to the entity state.
        entity = reduce(self.domain_class.mutator, domain_events, None)
        if entity is self.domain_class or entity is None:
            # Either entity was not created, or it was already discarded.
            raise KeyError(entity_id)
        else:
            return entity


class EventSourcedRepository(with_metaclass(ABCMeta)):

    def __init__(self, event_store):
        self.event_player = self.construct_event_player(event_store)

    def construct_event_player(self, event_store):
        return EventPlayer(event_store=event_store, domain_class=self.domain_class)

    @abstractproperty
    def domain_class(self):
        """Defines the type of entity available in this repo.
        """

    def __getitem__(self, entity_id):
        return self.event_player[entity_id]

    def __contains__(self, entity_id):
        try:
            self[entity_id]
        except KeyError:
            return False<commit_msg>Changed mutator() to be a class method, which simplifies and clarifies thing a little bit.<commit_after>from abc import ABCMeta, abstractproperty
from functools import reduce
from six import with_metaclass
from eventsourcing.domain.model.entity import EventSourcedEntity
from eventsourcing.infrastructure.event_store import EventStore
from eventsourcing.infrastructure.stored_events.base import recreate_domain_event


class EventPlayer(object):

    def __init__(self, event_store, domain_class):
        assert isinstance(event_store, EventStore), event_store
        assert issubclass(domain_class, EventSourcedEntity), domain_class
        self.event_store = event_store
        self.domain_class = domain_class

    def __getitem__(self, entity_id):
        # Get the stored events from the event store.
        stored_entity_id = self.domain_class.prefix_id(entity_id)
        stored_events = self.event_store.stored_event_repo.get_entity_events(stored_entity_id=stored_entity_id)

        # Recreate the domain events from the stored events.
        domain_events = map(recreate_domain_event, stored_events)

        # Successively apply the domain events to the entity state.
        entity = reduce(self.domain_class.mutator, domain_events, None)
        if entity is None:
            # Either entity was not created, or it was already discarded.
            raise KeyError(entity_id)
        else:
            return entity


class EventSourcedRepository(with_metaclass(ABCMeta)):

    def __init__(self, event_store):
        self.event_player = self.construct_event_player(event_store)

    def construct_event_player(self, event_store):
        return EventPlayer(event_store=event_store, domain_class=self.domain_class)

    @abstractproperty
    def domain_class(self):
        """Defines the type of entity available in this repo.
        """

    def __getitem__(self, entity_id):
        return self.event_player[entity_id]

    def __contains__(self, entity_id):
        try:
            self[entity_id]
        except KeyError:
            return False
{'fraction_non_alphanumeric': 0.06073170731707317, 'fraction_numerical': 0.0, 'mean_word_length': 3.5314917127071825, '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': '3220296', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1101, 'n_words': 297}