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.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pymediator-0.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymediator-0.2.0.tar.gz
  • Upload date:
  • Size: 6.4 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.2.0.tar.gz
Algorithm Hash digest
SHA256 059e51cfec3d28cbbf7e90e8f5f215d258a28295c97a565ab6dfa2f0bd9ecc38
MD5 0cd414bb3425de24fd576bd5df632e7f
BLAKE2b-256 4fbc611adee9de671b4776345ab880092f426f029d0025e3c4288849e3830142

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymediator-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20dfedf23cd39060303858afe82bdd214c983f7fe5fb5b42f4e83e1ce5957a75
MD5 1dd3a2cdbd34d125c5e0d94fa541f8d0
BLAKE2b-256 8f0f26fb6302740a3ac37ac060d1ee25e068d345dfecef6a54e9f59425b53d29

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