Infrastructure for attaching events to objects
Project description
Python SimpUtils Events
An example
from datetime import datetime, timezone
from enum import Enum
from simputils.events.abstract.Eventful import Eventful
from simputils.events.components.BasicEventCall import BasicEventCall
from simputils.events.components.BasicEventResult import BasicEventResult
from simputils.events.exceptions.InterruptEventSequence import InterruptEventSequence
# Creating event names as str-backed enum
class MyEventEnum(str, Enum):
BEFORE = "evt-before"
AFTER = "evt-after"
# Creating target class to which we will be attaching events
class MyObj(Eventful):
@classmethod
def _display_summary(cls, sub_results: list):
for item in sub_results:
for desc in item:
print(">>> ", desc)
def prepare_data(self, name: str, surname: str, age: int):
sub_res = self.event_run(MyEventEnum.BEFORE, name, surname, age)
if sub_res:
self._display_summary(sub_res.results)
else:
print("No pre-processed description prepared")
sub_res_2 = self.event_run(MyEventEnum.AFTER, datetime.now(timezone.utc))
return self._preprocess_results(sub_res) + self._preprocess_results(sub_res_2)
@classmethod
def _preprocess_results(cls, sub_res: BasicEventResult) -> list:
res = []
call: BasicEventCall
for call, call_res in sub_res:
if call_res is not None:
for item in call_res:
res.append(item)
if call.interrupted:
res.append(f"{call.callback.__name__}() INTERRUPTED")
return res
# Callback for "on_event" of "BEFORE"
def on_before(call: BasicEventCall, name: str, surname: str, age: int) -> list[str]:
res = [
f"[[event \"{call.event}\" adjusted through `{call.callback.__name__}()` callback]]",
f"Name: {name} {surname}",
f"Age: {age}"
]
return res
# Callback for "on_event" of "AFTER"
def on_after(call: BasicEventCall, ts: datetime) -> list[str]:
res = [
f"[[event \"{call.event}\" adjusted through `{call.callback.__name__}()` callback]]",
f"Finished at: {ts}"
]
# raise InterruptEventSequence(res)
return res
def main():
obj = MyObj()
obj.on_event(MyEventEnum.BEFORE, on_before)
obj.on_event(MyEventEnum.AFTER, on_after)
obj.on_event(MyEventEnum.AFTER, on_after)
descriptions = obj.prepare_data("Ivan", "Ponomarev", 35)
print(f"Resulting descriptions:")
for desc in descriptions:
print("##\t", desc)
if __name__ == "__main__":
main()
Output:
>>> [[event "<BasicEvent evt-before>" adjusted through `on_before()` callback]]
>>> Name: Ivan Ponomarev
>>> Age: 35
Resulting descriptions:
## [[event "<BasicEvent evt-before>" adjusted through `on_before()` callback]]
## Name: Ivan Ponomarev
## Age: 35
## [[event "<BasicEvent evt-after>" adjusted through `on_after()` callback]]
## Finished at: 2026-01-07 10:07:04.820497+00:00
## [[event "<BasicEvent evt-after>" adjusted through `on_after()` callback]]
## Finished at: 2026-01-07 10:07:04.820497+00:00
In case if # raise InterruptEventSequence(res) is uncommented, the output would be:
>>> [[event "<BasicEvent evt-before>" adjusted through `on_before()` callback]]
>>> Name: Ivan Ponomarev
>>> Age: 35
Resulting descriptions:
## [[event "<BasicEvent evt-before>" adjusted through `on_before()` callback]]
## Name: Ivan Ponomarev
## Age: 35
## [[event "<BasicEvent evt-after>" adjusted through `on_after()` callback]]
## Finished at: 2026-01-07 10:08:08.767001+00:00
## on_after() INTERRUPTED
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simputils_events-0.1.0.tar.gz.
File metadata
- Download URL: simputils_events-0.1.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.2","id":"zara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7d443965870a13f94c78a8771e01ef14efc0680e775a39a0f08f5abbd57c467
|
|
| MD5 |
92a963857e02c7dbcd2b4c118345bb65
|
|
| BLAKE2b-256 |
bbf3c88df3666da25c59fcd11e0a1702b7c7cd0e993e44a9639a608a70394588
|
File details
Details for the file simputils_events-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simputils_events-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.2","id":"zara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa1011e7fdd8e3d68832969a080d5c0355b5e4d75db59b4ec314f539a1f002fe
|
|
| MD5 |
b81f3469e99728f697f74bcbf599e80b
|
|
| BLAKE2b-256 |
8d9fbc83df88a3abd38dd309f39a0906ce0823c98e0a7a12bdce54c08b50f505
|