Skip to main content

Lightweight and extensible implementation of the Mediator pattern in Python

Project description

Pymediator

downloads versions license

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

Uploaded Source

Built Distribution

pymediator-0.3.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymediator-0.3.0.tar.gz
  • Upload date:
  • Size: 6.6 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.3.0.tar.gz
Algorithm Hash digest
SHA256 e6f223bd7e54057127c3dcfe8f13fe7dc52d1f7b48f46639b1f0087639b32eb7
MD5 9ba7b8f738e74e32645f26ea049774f9
BLAKE2b-256 f93d9ec7a167aef3f1a74592a1e284ab08014580cd396fffe86160ab5049a263

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymediator-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f51be647ca9a5d3b5cba759738ce7899734c373f75cc1dcadd7f10d3f723e0e
MD5 efc13d6168a062389e346342ded58e0d
BLAKE2b-256 7a64034391599147df80c496e57619066f63e11b52e89ec03b392e3ad307aef3

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