Last active
February 23, 2024 20:46
-
-
Save blinkinglight/951dbe3eda4f851e2aac38458939e3b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
events = [ | |
{ | |
"id" : 1, | |
"name" : "user.created", | |
"payload" : "name: labas, email:krabas", | |
"created" : "2024-01-01 12:13:14" | |
}, | |
{ | |
id: 2, | |
"name" : "user.updated", | |
"payload" : "name: labas, email:krabas@krabas", | |
"created" : "2024-01-02 12:13:14" | |
}, | |
{ | |
id: 3, | |
"name" : "user.deleted", | |
"payload" : "", | |
"created" : "2024-01-03 12:13:14" | |
} | |
] | |
class User: | |
def __init__(self): | |
self.active = False | |
self.name = "" | |
self.email = "" | |
self.createdAt = "" | |
self.updatedAt = "" | |
self.deletedAt = "" | |
def on(self, event, payload): | |
if event == "user.created": | |
self.on_user_created(payload) | |
if event == "user.updated": | |
self.on_user_updated(payload) | |
if event == "user.deleted": | |
self.on_user_deleted(payload) | |
def on_user_created(self, payload): | |
self.active = True | |
self.name = payload["name"] | |
self.email = payload["payload"] | |
self.createdAt = payload["created"] | |
def on_user_updated(self, payload): | |
self.name = payload["name"] | |
self.email = payload["payload"] | |
self.updatedAt = payload["created"] | |
def on_user_deleted(self, payload): | |
self.active = False | |
self.deletedAt = payload["created"] | |
user = User() | |
print("Initial state: {}\n".format(user.active)) | |
print("Name: \n {}, \nPayload:\n {}\n".format(user.name, user.email)) | |
print("Created at: {}\n".format(user.createdAt)) | |
print("Updated at: {}\n".format(user.updatedAt)) | |
print("Deleted at: {}\n".format(user.deletedAt)) | |
for event in events: | |
user.on(event["name"], event) | |
print("Last state: {}".format(user.active)) | |
print("Name: \n {}, \nPayload:\n {}\n".format(user.name, user.email)) | |
print("Created at: {}\n".format(user.createdAt)) | |
print("Updated at: {}\n".format(user.updatedAt)) | |
print("Deleted at: {}\n".format(user.deletedAt)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Initial state: False | |
Name: | |
, | |
Payload: | |
Created at: | |
Updated at: | |
Deleted at: | |
Last state: False | |
Name: | |
user.updated, | |
Payload: | |
name: labas, email:krabas@krabas | |
Created at: 2024-01-01 12:13:14 | |
Updated at: 2024-01-02 12:13:14 | |
Deleted at: 2024-01-03 12:13:14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment