Skip to main content

Lightweight and extensible implementation of the Mediator pattern in Python

Project description

Pymediator

Provides a lightweight and extensible implementation of the Mediator pattern in Python, specifically designed for systems that follow the CQRS (Command Query Responsibility Segregation) pattern. Its architecture allows for a clear separation of command and query logic, making it ideal for clean architectures and event-driven systems.

Install

pip install pymediator

Usage

from dataclases import dataclass
from pymediator import Mediator as _Mediator, SingletonRegistry

registry: SingletonRegistry = SingletonRegistry()

@dataclass
class SumRequest:
    x: int | float
    y: int | float

class SumHandler:
    def handle(self, request: SumRequest) -> int | float:
        return request.x + request.y

registry.register(SumRequest, SumHandler)

class Mediator(_Mediator):
    def __init__(self) -> None:
        super().__init__(registry=SingletonRegistry())

mediator: Mediator = Mediator()

mediator.send(SumRequest(x=3, y=8)) # Output: 11

Recommended use

# my_app/application/queries/sum.py
from dataclases import dataclass

@dataclass
class SumRequest:
    x: int | float
    y: int | float

class SumHandler:
    def handle(self, request: SumRequest) -> int | float:
        return request.x + request.y
# my_app/infrastructure/mediator.py
from pymediator import SingletonRegistry
from my_app.application.queries.sum import SumRequest, SumHandler

registry: SingletonRegistry = SingletonRegistry()

registry.register(SumRequest, SumHandler)
# my_app/infrastructure/console.py
import sys
from my_app.application.queries.sum import SumRequest

@inject
def main(x: int, y: int, mediator: Mediator = Provide[AppContainer.mediator]) -> None:
    print(mediator.send(SumRequest(x=x, y=y)))

if __name__ == "__main__":
    num1: int = int(sys.argv[1])
    num2: int = int(sys.argv[2])
    main(num1, num2)

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

pymediator-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

pymediator-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pymediator-0.1.0.tar.gz.

File metadata

  • Download URL: pymediator-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure

File hashes

Hashes for pymediator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4798f62c26a0f696c0366ba71c46b319720c9f9f092a302b6ba84eda13bfb98b
MD5 92f6ce251351185845b5a2e4ad9abbd6
BLAKE2b-256 3f4a9484e75256058d889cbf8c27898cb6f7e1b28e1afdd7e136d3986b6679ca

See more details on using hashes here.

File details

Details for the file pymediator-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pymediator-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.15 Linux/6.5.0-1025-azure

File hashes

Hashes for pymediator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 236214e8bcd238943e0dd695a9e3d2f673ee2e387bb541954da06c260563e64f
MD5 63477bae118b1e17d94f28884cb64588
BLAKE2b-256 a9982ffe62de03ea2263330b2645ac5942d0861d836b5fc706cb44100cebcb40

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page