Skip to main content

A lightweight library for creating message driven systems.

Project description

PyBondi

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. License

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.

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

pybondi-2.0.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

pybondi-2.0.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file pybondi-2.0.0.tar.gz.

File metadata

  • Download URL: pybondi-2.0.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.1 Linux/6.5.0-1025-azure

File hashes

Hashes for pybondi-2.0.0.tar.gz
Algorithm Hash digest
SHA256 88d297e39a253c589cbfb2e615fab0ed1ff3400aff73391a63ed1e09999d0908
MD5 9754087add74a1ca183ca8edb2bbc3bf
BLAKE2b-256 2d5bb38e0088ddac56de7c2c99d80904f0b90f17d1178ff4d77bd5b5fb994098

See more details on using hashes here.

File details

Details for the file pybondi-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pybondi-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.1 Linux/6.5.0-1025-azure

File hashes

Hashes for pybondi-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ecd31c40f966a69bc9a3ff74fc5835df9cd2ee5efd6b960fda1414df39a76b2
MD5 6dd200692774395af3fd990e4b3eff09
BLAKE2b-256 5d49e76d70970677ae5edf33325f897714c13e2968cf821779e39660c0e93e53

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