Skip to main content

Python implementation of the Event Pattern

Project description

Test Coverage Stable Version Pre-release Version PyPI - Python Version PyPI Downloads

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 | None the 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

delegate_events-0.0.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

delegate_events-0.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

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

Hashes for delegate_events-0.0.0.tar.gz
Algorithm Hash digest
SHA256 17909142e937867efa58d1bd33e9e10bbd22bf39d71734a80e4ae5859ef1e6ea
MD5 bd79729840165b66898db8def2dbb9cd
BLAKE2b-256 06870996a70b5dfbd766540d3acfcbdbc426dfeb43fa570b8ff939a1ba3f3eab

See more details on using hashes here.

Provenance

The following attestation bundles were made for delegate_events-0.0.0.tar.gz:

Publisher: python-publish.yml on apmadsen/delegate-events

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file delegate_events-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for delegate_events-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48c96945e0224972d9f807508e1025a92cbaf018003657a20c6e17a3fac80814
MD5 37bbe8cf12451bb219edd69929062367
BLAKE2b-256 47bcef86261af24f5a395648b7c75a933e9cbe6eccbcd005e32db20357df8753

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page