PyBondi
Table of Contents
Introduction
PyBondi is a lightweight Python library that provides simple and intuitive tools for creating event driven systems with a FastAPI-like syntax thanks to the fast-depends dependency injection system. It provides base classes and abstractions for the following patterns:
- Event and Consumers: Create and dispatch events to several consumers.
- Commands and Handlers: Create and dispatch commands to their respective handlers.
- Messages and Publishers: Create and publish messages to several publishers.
- Services and Providers: Create and use services and inject dependencies.
- Sessions and the unit of work pattern: Create and manage the lifecycle of resources using the unit of work pattern.
Installation
Install with pip:
pip install pybondi
Usage
Import the library and start defining your commands, events, messages, etc.
from pybondi import Command
from pybondi import Event
from pybondi import Message
from dataclasses import dataclass
### Define your commands, events, messages, etc.
@dataclass
class CreateUser(Command):
id: str
name: str
@dataclass
class UpdateUser(Command):
id: str
name: str
@dataclass
class UserUpdated(Event):
id: str
name: str
@dataclass
class Notification(Message):
user_id: str
text: str
Create a service and start defining your handlers, subscribers, consumers, etc. if you don't want to resolve your dependencies yet, you can use
dependency injection with Depends like in FastAPI.
For messages, one can define several topics in which the message will be published.
For commands and events the type hints will pair the command or event with the handler or consumer that will process it. You can use unions to pair several commands or events with the same handler or consumer.
from pybondi import Service
from pybondi import Depends
service = Service(cast_dependency=False) # Disable automatic casting for this example
# this is needed because we are using dicts as dependencies
# and they get empty when casting
def database_dependency() -> dict:
raise NotImplementedError
def notifications_dependency() -> dict:
raise NotImplementedError
@service.handler
def handle_put_user(command: CreateUser | UpdateUser, database = Depends(database_dependency)):
database[command.id] = command.name
service.consume(UserUpdated(id=command.id, name=command.name))
@service.consumer
def consume_user_updated(event: UserUpdated, database = Depends(database_dependency)):
service.publish('topic-1', Notification(user_id=event.id, text=f'User {event.id} updated with name {event.name}'))
@service.subscriber('topic-1', 'topic-2')
def on_notifications(message: Notification, notifications = Depends(notifications_dependency)):
notifications[message.user_id] = message.text
Override the dependencies with implementations if you already didn't do it already.
nfs = {}
db = {}
def database_adapter():
return db
def notification_adapter():
return nfs
service.dependency_overrides[database_dependency] = database_adapter
service.dependency_overrides[notifications_dependency] = notification_adapter
Finally, execute your logic.
service.handle(CreateUser(id='1', name='John Doe'))
service.handle(UpdateUser(id='1', name='Jane Doe'))
print(db['1']) # Jane Doe
assert db['1'] == 'Jane Doe'
print(nfs['1']) # User 1 updated with name Jane Doe
assert nfs['1'] == 'User 1 updated with name Jane Doe'
You can see this example in the examples folder.
License
This project is licensed under the terms of the MIT license. Feel free to use it in your projects.
Release files for pybondi 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pybondi-2.0.1.tar.gz | 9.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pybondi-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.6 kB
Release files / pybondi-2.0.1.tar.gz
| Download URL | pybondi-2.0.1.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5c4570650b8b93459e543bec18c1c44680569656ce4cc87e56ca4d9acb68ce5f
|
|
BLAKE2b-256 checksum How to use checksums |
e06523a1882879ba05a16e985a7bc8fd86a30ae95aa4f048686cfe75d73110f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.12.1 Linux/6.5.0-1025-azure
|
Release files / pybondi-2.0.1-py3-none-any.whl
| Download URL | pybondi-2.0.1-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c20e14ec2ba20ea32d3dcfea720d844265ade076f4e7a50abaeaf8d254c33f2e
|
|
BLAKE2b-256 checksum How to use checksums |
5b0ef5c8d10396e6744bd8de92ac817f13a1e88787addd1a9f334903d60efb11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.12.1 Linux/6.5.0-1025-azure
|