Skip to main content

Diator Logo

Test Downloads Package version Supported Python versions

Diator - CQRS Library for Python

Docs | PyPI

Diator is a Python library for implementing CQRS pattern in your Python applications. It provides a set of abstractions and utilities to help you separate your read and write concerns, allowing for better scalability, performance, and maintainability of your application.

Features :bulb:

  • Implements the CQRS pattern.
  • Simple, yet flexible API.
  • Supports multiple message brokers, such as Redis Pub/Sub and Azure Service Bus.
  • Supports various di-frameworks, such as di and rodi.
  • Easy to integrate with existing codebases.

Installation :triangular_ruler:

Install the Diator library with pip

pip install diator

There are also several installation options:

  • To use Redis as Message Broker

    pip install diator[redis]
    
  • Or Azure Service Bus

    pip install diator[azure]
    

Simple Example :hammer_and_wrench:

Minimal example of diator usage:

import asyncio
from dataclasses import dataclass, field
from di import Container, bind_by_type
from di.dependent import Dependent
from diator.events import EventMap, Event, EventEmitter
from diator.container.di import DIContainer
from diator.mediator import Mediator
from diator.requests import Request, RequestHandler, RequestMap


@dataclass(frozen=True, kw_only=True)
class JoinMeetingCommand(Request):
    meeting_id: int
    user_id: int
    is_late: bool = field(default=False)


class JoinMeetingCommandHandler(RequestHandler[JoinMeetingCommand, None]):
    def __init__(self, meeting_api) -> None:
        self._meeting_api = meeting_api
        self._events: list[Event] = []

    @property
    def events(self) -> list[Event]:
        return self._events

    async def handle(self, request: JoinMeetingCommand) -> None:
        self._meeting_api.join(request.meeting_id, request.user_id)
        if request.is_late:
            self._meeting_api.warn(request.user_id)


def setup_di() -> DIContainer:
    external_container = Container()

    external_container.bind(
        bind_by_type(
            Dependent(JoinMeetingCommandHandler, scope="request"),
            JoinMeetingCommandHandler,
        )
    )

    container = DIContainer()
    container.attach_external_container(external_container)

    return container


async def main() -> None:
    container = setup_di()

    request_map = RequestMap()
    request_map.bind(JoinMeetingCommand, JoinMeetingCommandHandler)

    event_emitter = EventEmitter(
        event_map=EventMap(), container=container, message_broker=None
    )

    mediator = Mediator(
        request_map=request_map,
        event_emitter=event_emitter,
        container=container,
    )

    await mediator.send(JoinMeetingCommand(user_id=1, meeting_id=1, is_late=True))


if __name__ == "__main__":
    asyncio.run(main())

Further reading :scroll:

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for diator 0.1.2

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

Source distribution (sdist)

Source distribution for diator 0.1.2
File Size Uploaded
diator-0.1.2.tar.gz 18.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for diator 0.1.2
File Interpreter ABI Platform
diator-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 34.7 kB

Release files / diator-0.1.2.tar.gz

Download URL diator-0.1.2.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
da44cfec719fd7dc0b33eda7295ae31ad0afb379489778addb5c85eea0755d8f
BLAKE2b-256 checksum
How to use checksums
7f43b908d8bc852fa275e80e54d9883b35f4f6326d36b815e006f9edce394388
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / diator-0.1.2-py3-none-any.whl

Download URL diator-0.1.2-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a252c4c2cb9963b9fb24dbc4e8c7b63f3bf880501505b07ef7bffb356ac3c09
BLAKE2b-256 checksum
How to use checksums
0723906766e984b53d3abe63c9ae2a2c6990269dcaa0a7be106c39226657b321
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.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