Python implementation of the Event Pattern
Project description
delegate-events: Python implementation of the Event Pattern.
delegate-events provides a basic implementation of the well-known Event or Pub/Sub Pattern, and is built on top of the delegate-pattern package which handles the delegation part.
Example
from delegate.pattern import delegate
from delegate.events import Channel, Event
class OnSomethingEvent(Event):
def __init__(self, what: str, why: str):
self.what = what
self.why = why
def __str__(self):
return f"{self.what} happened because of {self.why}"
class Class1:
on_something = delegate(Channel[OnSomethingEvent])
inst = Class1()
msg_queue: list[Event] = []
def fn(publisher, event: OnSomethingEvent):
msg_queue.append(event)
inst.on_something.subscribe(fn)
inst.on_something.fire(OnSomethingEvent("Something", "some other thing"))
str(msg_queue[0]) # => "Something happened because of some other thing"
API
Event class
The Event class is the base class of all events. Events are the classes holding the event information, when an event is fired, thus, when subclassed, should contain properties/fields of the event information.
Example
from delegate.events import Event
class OnSomething(Event):
def __init__(self, what: str, why: str):
self.what = what
self.why = why
def __str__(self):
return f"{self.what} happened because of {self.why}"
Channel class
The Channel class is the event channel returned by the delegate, and its jobs are handling subscriptions and firing of events.
Channel.subscribe(handler: Callable[[object, T<Event>], None])
The subscribe function subscribes a handler function to the channel. The function takes 1 argument:
- handler:
Callable[[object, Event], None]
Example
from delegate.events import Channel
channel = Channel[OnSomethingEvent]()
def handler(sender: object, args: OnSomethingEvent) -> None:
pass
channel.subscribe(handler)
Channel.unsubscribe(handler: Callable[[object, T<Event>], None])
The unsubscribe function unsubscribes a handler function from the channel. The function takes 1 argument:
- handler:
Callable[[object, Event], None]
from delegate.events import Channel
channel = Channel[OnSomethingEvent]()
def handler(sender: object, args: OnSomethingEvent) -> None:
pass
channel.subscribe(handler)
channel.unsubscribe(handler)
Channel.fire(event: T<Event>, *, ttl: float | None = 0) -> bool function
The fire function fires the event, invoking all subscribed handlers. The function returns True if event was successfully sent and takes 1 argument:
- event:
Event
and 1 optional keyword argument:
- ttl:
float | Nonethe time-to-live (seconds) of the event.
from delegate.events import Channel
channel = Channel[OnSomethingEvent]()
msg_queue: list[Event] = []
def handler(sender: object, event: OnSomethingEvent) -> None:
msg_queue.append(event)
channel.fire(OnSomethingEvent("Something", "some other thing")) # => False
channel.fire(OnSomethingEvent("Something", "some other thing"), ttl=10) # => False - event stays in queue and will be sent if subcrribed to within 10 seconds though
channel.subscribe(handler)
channel.fire(OnSomethingEvent("Something", "some other thing")) # => True
len(msg_queue) # => 2
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 delegate_events-0.0.0.tar.gz.
File metadata
- Download URL: delegate_events-0.0.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17909142e937867efa58d1bd33e9e10bbd22bf39d71734a80e4ae5859ef1e6ea
|
|
| MD5 |
bd79729840165b66898db8def2dbb9cd
|
|
| BLAKE2b-256 |
06870996a70b5dfbd766540d3acfcbdbc426dfeb43fa570b8ff939a1ba3f3eab
|
Provenance
The following attestation bundles were made for delegate_events-0.0.0.tar.gz:
Publisher:
python-publish.yml on apmadsen/delegate-events
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
delegate_events-0.0.0.tar.gz -
Subject digest:
17909142e937867efa58d1bd33e9e10bbd22bf39d71734a80e4ae5859ef1e6ea - Sigstore transparency entry: 225824060
- Sigstore integration time:
-
Permalink:
apmadsen/delegate-events@edc3c662c5747a740d5cc5be64473c9307d80fc5 -
Branch / Tag:
refs/tags/v0.0.0 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@edc3c662c5747a740d5cc5be64473c9307d80fc5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file delegate_events-0.0.0-py3-none-any.whl.
File metadata
- Download URL: delegate_events-0.0.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48c96945e0224972d9f807508e1025a92cbaf018003657a20c6e17a3fac80814
|
|
| MD5 |
37bbe8cf12451bb219edd69929062367
|
|
| BLAKE2b-256 |
47bcef86261af24f5a395648b7c75a933e9cbe6eccbcd005e32db20357df8753
|
Provenance
The following attestation bundles were made for delegate_events-0.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on apmadsen/delegate-events
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
delegate_events-0.0.0-py3-none-any.whl -
Subject digest:
48c96945e0224972d9f807508e1025a92cbaf018003657a20c6e17a3fac80814 - Sigstore transparency entry: 225824061
- Sigstore integration time:
-
Permalink:
apmadsen/delegate-events@edc3c662c5747a740d5cc5be64473c9307d80fc5 -
Branch / Tag:
refs/tags/v0.0.0 - Owner: https://github.com/apmadsen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@edc3c662c5747a740d5cc5be64473c9307d80fc5 -
Trigger Event:
release
-
Statement type: