A simple observer pattern implementation
Project description
simple-observable
A simple observer pattern implementation.
Usage example
from simple_observable import Observable
observable = Observable()
def my_callback(event):
print(f"Called with {event}")
observable.add_listener("mytopic", my_callback)
observable.notify("mytopic", {"event_details": "hello"})
Topic lifecycle events
The special topic Observable.LIFECYCLE_EVENTS can be used to perform an actions when new topics are added and existing ones are removed.
from simple_observable import Observable
observable = Observable()
def on_lifecycle_event(event):
if event["event_type"] == observable.TOPIC_ADDED_EVENT:
topic = event["topic"]
print(f"topic lifecycle: added '{topic}'")
if event["event_type"] == observable.TOPIC_REMOVED_EVENT:
topic = event["topic"]
print(f"topic lifecycle: removed '{topic}'")
observable.add_listener(observable.LIFECYCLE_EVENTS, on_lifecycle_event)
def my_callback(event):
print(f"Called with {event}")
# the next line triggers a lifecycle event {"event_type": observable.TOPIC_ADDED_EVENT, "topic": "mytopic"}
listener_id = observable.add_listener("mytopic", my_callback)
observable.notify("mytopic", {"key": "value"})
# and the last line triggers a lifecycle event {"event_type": observable.TOPIC_REMOVED_EVENT, "topic": "mytopic"}
observable.remove_listener(listener_id)
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 simple-observable-0.1.0.tar.gz.
File metadata
- Download URL: simple-observable-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d29cd411ffc775452c7e9fa02a602c9ca5c768c49b312f69887090eb8c4014de
|
|
| MD5 |
eedae2aadbd70281c377608949016f32
|
|
| BLAKE2b-256 |
0bab9fbbfafa1e3a89004b5bf5458a655270516ea54dc8905e975bd05cec93f3
|
File details
Details for the file simple_observable-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simple_observable-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b7f9adcac95b73198f07dbef68918909c1c1f7613d90430546e097bd91e262a
|
|
| MD5 |
1eeda7fc87b71c40b17b0142dc392264
|
|
| BLAKE2b-256 |
d12fcb9c314e5a8abeb99adba1cc23c33b4478269477e132cea90582ee75470c
|