A lightweight, flexible publish-subscribe event system for Python applications.
ALT-event-system provides a simple yet powerful event-driven architecture that enables loose coupling between components through an event bus pattern. Perfect for building reactive applications, plugin systems, or any scenario where you need decoupled communication.
- 🚀 Simple API - Easy to understand and use
- 🎯 Type-safe - Full type hints for better IDE support
- 🔌 Loose Coupling - Components communicate without direct dependencies
- 🌟 Wildcard Subscriptions - Subscribe to all events with
* - 📜 Event History - Track and query past events
- 🛡️ Error Isolation - Handler errors don't affect other handlers
- 🔍 Source Tracking - Optional source identification for events
- ⚡ Zero Dependencies - Uses only Python standard library
pip install ALT-event-systemfrom alt_event_system import EventSystem
# Create an event system
events = EventSystem()
# Define a handler
def on_user_login(event):
print(f"User {event.data['user_id']} logged in from {event.data['ip']}")
# Subscribe to events
events.subscribe("user.login", on_user_login)
# Emit events
events.emit("user.login", {"user_id": 123, "ip": "192.168.1.1"})Subscribe to all events:
def log_all_events(event):
print(f"[{event.timestamp}] {event.type}: {event.data}")
events.subscribe("*", log_all_events)Track and query past events:
# Get recent events
history = events.get_history(limit=10)
# Filter by event type
login_events = events.get_history(event_type="user.login")Handler errors are isolated:
def safe_handler(event):
print("I still work even if others fail!")
events.subscribe("test.event", safe_handler)EventSystem()- Create a new event systemsubscribe(event_type, handler)- Subscribe to eventsemit(event_type, data, source=None)- Emit an eventget_history(event_type=None, limit=100)- Get event historyclear_history()- Clear event historyget_subscriber_count(event_type)- Count subscribers
Contributions are welcome! Please feel free to submit a Pull Request.
Avi Layani - avilayani@gmail.com
This project is licensed under the MIT License - see the LICENSE file for details.