Registry / communication / events

events

JSON →
library0.5pypypi✓ verified 29d ago

The 'events' library for Python, currently at version 0.5, aims to bring the elegance of C# EventHandler to Python. It provides a simple, lightweight mechanism for event subscription and firing, encapsulating the core functionality for attaching and invoking callback functions (event handlers) in sequence, thus facilitating event-driven programming patterns in a straightforward manner. The library is considered mature and stable, with its last release in July 2023, and sees infrequent updates.

pip install events
INSTALL
IMPORT
SIG · EVENTS
E
events
communicationpythonv0.5
Install
1.6s avg
Import
—
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Events
✓ from events import Events
The primary class for creating event publishers.

Demonstrates how to create an `Events` object, subscribe multiple callback functions using `+=`, fire events, and unsubscribe callbacks using `-=`.

from events import Events def my_event_handler(reason, data=None): print(f"Event fired: {reason}") if data: print(f" Data: {data}") # Create an Events instance app_events = Events() # Subscribe a handler to an event named 'on_change' app_events.on_change += my_event_handler # Fire the 'on_change' event print("Firing event: 'initial change'") app_events.on_change("initial change", data={"user": "admin"}) # Subscribe another handler to the same event def another_handler(reason): print(f"Another handler received: {reason}") app_events.on_change += another_handler # Fire the event again print("\nFiring event again: 'second change'") app_events.on_change("second change") # Unsubscribe a handler app_events.on_change -= my_event_handler # Fire the event one last time (only 'another_handler' will be invoked) print("\nFiring event after unsubscription: 'final change'") app_events.on_change("final change")
Debug
Known issues
gotchaThe `Events()` instance holds strong references to subscribed callback functions. If an event handler is a method of an object, and the `Events` instance outlives that object, it can prevent the object from being garbage collected, leading to memory leaks. Always explicitly unsubscribe handlers using `-=` when they are no longer needed, especially for object methods.
fix
Ensure `event_instance.event_name -= handler_function` is called when the handler is no longer required or the associated object is being disposed of. Consider weak references for advanced scenarios if manual unsubscription is problematic.
affects: 0.1 - 0.5
gotchaBy default, `Events` instances do not predefine event names. This means you can subscribe to and fire any arbitrary event name (e.g., `events.on_typo`). This flexibility can lead to subtle bugs due to typos in event names that might go unnoticed. For stricter validation, you can predefine events.
fix
To enforce predefined events, subclass `Events` and list event names in the class definition, or pass an iterator of event names to the `Events` constructor. Attempts to subscribe to or fire an undefined event will then raise an `EventsException`.
affects: 0.1 - 0.5
gotchaThe package name 'events' is highly generic. There are several other Python libraries and standard library modules (e.g., `eventsourcing`, `pyventus`, `python-dispatch`, `threading.Event`) that deal with event-related patterns. Ensure you are installing and using `pyeve/events` if this is your intended library.
fix
Always import explicitly (e.g., `from events import Events`) and check the `events.__file__` to confirm you're using the correct package. Refer to documentation specific to `github.com/pyeve/events`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'events'
The 'events' package has not been installed in your Python environment or there is a typo in the import statement.
fix
Install the library using pip: `pip install Events`. Ensure your import statement is `from events import Event`.
AttributeError: 'Event' object has no attribute '...' (e.g., 'name', 'type')
The base `events.Event` object does not automatically possess arbitrary attributes like 'name' or 'type'. Event data is typically accessed via `event.sender`, `event.args` (for positional arguments), or `event.kwargs` (for keyword arguments).
fix
Access event data using `event.sender`, `event.args`, or `event.kwargs`. If you intend to pass named data, use keyword arguments when firing the event and access them via `event.kwargs['your_key']`.
TypeError: handler_function() missing X required positional argument: 'Y'
An event handler function was subscribed with a signature (parameters) that does not match the arguments provided when the event is fired.
fix
Ensure that the event handler function's parameters (`sender`, `*args`, `**kwargs`) correctly correspond to the arguments passed to `event.fire(sender, *args, **kwargs)`.
Upgrade
Version history
0.5latest on PyPI · released Jul 31, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
26
OpenAI (training)
1
Resources
events — pip install events · libregistry