Skip to content

Instantly share code, notes, and snippets.

@Unit03
Last active October 8, 2019 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Unit03/89e8ca5f5e47582139078b0bce10524d to your computer and use it in GitHub Desktop.
Save Unit03/89e8ca5f5e47582139078b0bce10524d to your computer and use it in GitHub Desktop.
Event Sourcing - event code sample
class MyEvent:
def __init__(self, foo: int):
"""For issuing new events in the application."""
self.foo = foo
@classmethod
def unserialize(cls, payload: dict) -> "MyEvent":
"""To use when fetching an existing event from the event log."""
return cls(foo=payload["foo"])
def serialize(self) -> dict:
"""To use when storing a new event in the event log."""
return {"foo": self.foo}
def test_my_event_serialize():
event = MyEvent(1)
assert event.serialize() == {"foo": 1}
def test_my_event_unserialize():
assert MyEvent.unserialize({"foo": 1}) == MyEvent(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment