Exemple #1
0
 def encode_instance(self, o):
     if hasattr(o, "__slots__") and o.__slots__ != ():
         topic = get_topic(o.__class__)
         state = {k: self.encode_object(getattr(o, k)) for k in o.__slots__}
         return {"__class__": {"state": state, "topic": topic}}
     elif hasattr(o, "__dict__"):
         topic = get_topic(o.__class__)
         state = {k: self.encode_object(v) for k, v in o.__dict__.items()}
         return {"__class__": {"state": state, "topic": topic}}
     else:
         return o
Exemple #2
0
 def encode_deque(self, o):
     if type(o) == deque:
         return {"__deque__": self.encode_object(list(o))}
     else:
         return {
             "__deque__": {
                 "state": self.encode_object(list(o)),
                 "topic": get_topic(o.__class__)
             }
         }
Exemple #3
0
 def encode_frozenset(self, o):
     if type(o) == frozenset:
         return {"__frozenset__": self.encode_iterable(o)}
     else:
         return {
             "__frozenset__": {
                 "state": self.encode_iterable(o),
                 "topic": get_topic(o.__class__)
             }
         }
Exemple #4
0
 def encode_list(self, o):
     if type(o) == list:
         return [self.encode_object(i) for i in o]
     else:
         return {
             "__list__": {
                 "state": [self.encode_object(i) for i in o],
                 "topic": get_topic(o.__class__)
             }
         }
Exemple #5
0
 def encode_dict(self, o):
     if type(o) == dict:
         return self.encode_dict_state(o)
     else:
         return {
             "__dict__": {
                 "topic": get_topic(o.__class__),
                 "state": self.encode_dict_state(o)
             }
         }
Exemple #6
0
    async def __create__(
        cls: Type[TDomainEntity],
        originator_id: Optional[UUID] = None,
        event_class: Optional[
            Type["DomainEntity.Created[TDomainEntity]"]] = None,
        **kwargs: Any,
    ) -> TDomainEntity:
        """
        Creates a new domain entity.

        Constructs a "created" event, constructs the entity object
        from the event, publishes the "created" event, and returns
        the new domain entity object.

        :param cls DomainEntity: Class of domain event
        :param originator_id: ID of the new domain entity (defaults to ``uuid4()``).
        :param event_class: Domain event class to be used for the "created" event.
        :param kwargs: Other named attribute values of the "created" event.
        :return: New domain entity object.
        :rtype: DomainEntity
        """

        if originator_id is None:
            originator_id = uuid4()

        if event_class is None:
            created_event_class: Type[
                DomainEntity.Created[TDomainEntity]] = cls.Created
        else:
            created_event_class = event_class

        event = created_event_class(originator_id=originator_id,
                                    originator_topic=get_topic(cls),
                                    **kwargs)

        obj = event.__mutate__(None)

        if obj is None:
            raise ValueError("{0} returned None".format(
                type(event).__mutate__.__qualname__))

        await obj.__publish__(event)
        return obj
Exemple #7
0
    def __hash__(self) -> int:
        """
        Computes a Python integer hash for an event.

        Supports Python equality and inequality comparisons.

        :return: Python integer hash
        :rtype: int
        """
        attrs = self.__dict__.copy()

        # Involve the topic in the hash, so that different types
        # with same attribute values have different hash values.
        attrs["__event_topic__"] = get_topic(type(self))

        # Calculate the cryptographic hash of the event.
        sha256_hash = self.__hash_object__(attrs)

        # Return the Python hash of the cryptographic hash.
        return hash(sha256_hash)
Exemple #8
0
def encode_enum(obj):
    return {"__enum__": {"topic": get_topic(type(obj)), "name": obj.name}}
Exemple #9
0
def encode_type(o):
    return {"__type__": get_topic(o)}