Skip to main content

python-mediator

CI codecov pypi downloads versions license

Elastic and extensible high-performance asyncio CQRS + ES python microframework. Compatible with recent python versions of CPython and pypy3.

Corresponds to clean architecture patterns, ideal for command/query segregation scenarios and event-driven design approaches.

Key features:

  • automatic handler inspection and action matching - like in modern frameworks (FastAPI, Typer, Click etc.) machinery is fully automatic and command, query or event object is matched with handler automatically
  • extra parameters injection with ease - extra context information like credentials can be passed safely and easily to handler with zero complexity
  • configurable middleware (modifier) stack - handler call flow can be extended easily with i.e. data mapping, special exception handling or extra logging by defining modifier stack that wraps handler execution
  • ultra-lightweight and performance optimized - has no external dependencies and all features are implemented in care of low runtime overhead

Help

Work in progress...

A command/query handling example

from dataclasses import dataclass

from mediator.request import LocalRequestBus

bus = LocalRequestBus()


@dataclass
class PrintMessageCommand:
    message: str


@bus.register
async def command_handler(event: PrintMessageCommand):
    print(f"print message: {event.message}")
    return event.message


@dataclass
class DataQuery:
    id: int


@bus.register
async def query_handler(query: DataQuery):
    print(f"data query: {query.id}")
    return {"id": query.id, "data": "test"}


async def main():
    printed_message = await bus.execute(PrintMessageCommand(message="test"))
    assert printed_message == "test"

    data = await bus.execute(DataQuery(id=1))
    assert data == {"id": 1, "data": "test"}

    # -- output --
    # print message: test
    # data query: 1

More advanced example available in example/test_request_advanced.py for reference.

An event handling example

from dataclasses import dataclass

from mediator.event import LocalEventBus

bus = LocalEventBus()


@dataclass
class MessageEvent:
    message: str


@bus.register
async def first_handler(event: MessageEvent):
    print(f"first handler: {event.message}")


@bus.register
async def second_handler(event: MessageEvent):
    print(f"second handler: {event.message}")


async def main():
    await bus.publish(MessageEvent(message="test"))
    # -- output --
    # first handler: test
    # second handler: test

More advanced example available in example/test_event_advanced.py for reference.

Metadata

Release files for python-mediator 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-mediator 0.7.0
File Size Uploaded
python-mediator-0.7.0.tar.gz 18.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-mediator 0.7.0
File Interpreter ABI Platform
python_mediator-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 46.7 kB

Release files / python-mediator-0.7.0.tar.gz

Download URL python-mediator-0.7.0.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
506edc18742cbd6c43eadc654c1f7563c87972b3123e18b2943e1677bb329654
BLAKE2b-256 checksum
How to use checksums
1264a51a2e55b7fe0189c8ed169ffff52a17a70cb26baf5b535478ffe912c7c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.8.12 Linux/5.8.0-1042-azure

Release files / python_mediator-0.7.0-py3-none-any.whl

Download URL python_mediator-0.7.0-py3-none-any.whl
Size 28.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1839eb264c724beef24f3d364f9ed8ba73eeddc0b69368df8e886dc09c56ff23
BLAKE2b-256 checksum
How to use checksums
ac5837406e45d0e21e4ef454cb19cdf1a7726890721eace96d40fe5643b3b8fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.8.12 Linux/5.8.0-1042-azure

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page