Python implementation of C# style events
Project description
pynetevents
A lightweight, flexible C#-like event system in Python supporting strong/weak references, async listeners, and exception propagation.
Features
- Event slots — attach and fire multiple listeners
- Descriptor-based events
- Supports sync and async listeners
- Weak reference support to avoid memory leaks
- Duplicate listener protection
- Exception propagation
- Clean syntax:
+=/-=
Table of Contents
Check the API Reference for full details.
Quick Start
import asyncio
from pynetevents import Event
class ChatServer:
on_message = Event()
def send_message(self, msg):
self.on_message(msg)
def log(msg):
print(f"[LOG] {msg}")
async def save(msg):
await asyncio.sleep(0.1)
print(f"[ASYNC] Saved {msg}")
server = ChatServer()
server.on_message += log
server.on_message += save
server.send_message("Hello World")
EventSlots
EventSlot and EventSlotWeakRef are containers for listeners.
Use += / -= to subscribe/unsubscribe.
EventSlot
Strong references, suitable for long-lived listeners:
from pynetevents import EventSlot
slot = EventSlot("on_data")
slot += lambda x: print(f"Received {x}")
slot("Test")
EventSlotWeakRef
Weak references, automatically cleared when listener is deleted:
from pynetevents import EventSlotWeakRef
class Listener:
def on_event(self, msg):
print(msg)
slot = EventSlotWeakRef("on_message")
listener = Listener()
slot += listener.on_event
slot("Hello")
del listener
slot("World") # No output
Common Usage
subscribe,subscribe_weak,unsubscribe,unsubscribe_weakexist, but+=/-=is simpler.invoke()calls sync listeners and schedules async (fire-and-forget).invoke_async()awaits async listeners.
import asyncio
from pynetevents import EventSlot
def sync(msg): print(msg)
async def async_listener(msg): print(msg)
slot = EventSlot("event")
slot += sync
slot += async_listener
slot("Fire-and-forget")
asyncio.run(slot.invoke_async("Awaited"))
- Slots can be configured in the constructor:
EventSlot("name", propagate_exceptions=True, allow_duplicate_listeners=True)
Event Descriptor
Event is a descriptor to declare class-level events:
from pynetevents import Event
class MyApp:
on_update = Event(propagate_exceptions=True)
on_weak = Event(use_weakref_slot=True, allow_duplicate_listeners=True)
app = MyApp()
app.on_update += lambda msg: print(msg)
app.on_update("Update fired")
- Automatically creates per-instance slot.
+=/-=preferred; direct assignment is restricted.- Configuration must match if manually assigning a slot in
__init__as well.
Exceptions
EventExecutionError— Raised when a listener fails.DuplicateEventListenerError— Raised when adding a duplicate listener when not allowed.
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 pynetevents-1.0.0.tar.gz.
File metadata
- Download URL: pynetevents-1.0.0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dc9ba77f94181878d242ff1129d51e42a5b8956b48ab3622a01a7b0cd8efc9e
|
|
| MD5 |
3a0e6c835edb4672cba96da5553179e1
|
|
| BLAKE2b-256 |
9a5abc4ff49d8f9decf639672f441683f91ab12f8b18abbdc8a21a87516f62a5
|
File details
Details for the file pynetevents-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pynetevents-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8100010a7da956e38f5310627f072506a3a3fb50c022607b21f933ab2fc184a
|
|
| MD5 |
bba79236cde145cef42bfa5d04126610
|
|
| BLAKE2b-256 |
2c66868ef7507a2e66398b291211d0323dd53636e84bdb14e7921ef30cd96786
|