Skip to main content

C#-style event handling mechanism for Python

Project description

C#-Style Event Handling Mechanism for Python

C# provides a very simple syntax using the observer pattern for its event handling system. The aim of this project is to implement the pattern in python as similarly as possible.

In C#, an "event" is a field or a property of the delegate type EventHandler. Since delegates in C# can be combined and removed with += and -= operators, event handlers can easily subscribe to or unsubscribe from the event using those operators.

Python does not support an addition of two Callable types. So the Event[P] class is provided to mimic delegates:

from cs_events import Event

on_change: Event[object, str] = Event()

Handlers can subscribe to and unsubscribe from the event with the same syntax:

def event_handler(o: object, s: str) -> None:
    ...

on_change += event_handler
on_change -= event_handler

An event can be raised by simply invoking it with the arguments:

on_change(self, "value")

Since Event acts just like a delegate from C#, it is not required to be bound to a class or an instance object. This is the major difference to other packages that try to implement the C#-style event system, which usually revolve around a container object for events.

An example class with event fields may look like this:

class EventExample:
    def __init__(self) -> None:
        self.on_update: Event[str] = Event()
        self.__value = ""
    
    def update(self, value: str) -> None:
        if self.__value != value:
            self.__value = value
            self.on_update(value)

obj = EventExample()
obj.on_update += lambda value: print(f"obj.{value=}")
obj.update("new value")

A class decorator @events is provided as a shortcut for event fields and properties:

from cs_events import Event, events

@events
class EventFieldsExample:
    item_added: Event[object]
    item_removed: Event[object]
    item_updated: Event[object, str]

@events(properties=True)
class EventPropertiesExample:
    loaded: Event[int]
    disposed: Event[[]]

    def __init__(self) -> None:
        self._events = {}

Check the documentation of @events for more detail on event properties.

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

cs-events-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

cs_events-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file cs-events-0.1.0.tar.gz.

File metadata

  • Download URL: cs-events-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for cs-events-0.1.0.tar.gz
Algorithm Hash digest
SHA256 90f1055fab65418ae70862ec02d7df3e1127305288294125ee23c8d7438dbbaf
MD5 67921c262ae678dd454cde3b6412ee00
BLAKE2b-256 b77525b090ec4400628761b802ba41c89a58045789f5ad956c4c759a76fa31fa

See more details on using hashes here.

File details

Details for the file cs_events-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cs_events-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for cs_events-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a88108091e0b72dacd1c777d4807e823344a885a47995169cb827adef33a9d2
MD5 789ef2f044068cf503679fea3309fb34
BLAKE2b-256 a530b2130947fde067e707acc9b6294e5652e32ad3dc5f99a0eb4dd706c19e4d

See more details on using hashes here.

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