Skip to main content

Python Command Query Segregation (CQS) utilities

Project description

pycqs

Command Query Segregation (CQS) utilities for Python.

Provides the CommandBus and QueryBus classes.

Requirements

Python 3.10 or above.

Installation

You can use pip to install pycqs with e.g.:

pip install pycqs

Usage

  • Using the CommandBus.
import asyncio
from dataclasses import dataclass

from pycqs import (
    Command,
    CommandBus,
    CommandHandler,
)


# Create the command
@dataclass
class HelloWorldCommand(Command):
    name: str


# Create the command handler
class HelloWorldCommandHandler(
    CommandHandler[HelloWorldCommand],
):
    async def execute(
        self,
        command: HelloWorldCommand,
    ) -> None:
        print(f'Hello World, {command.name}')


async def main() -> None:
    # Initialize the command bus
    command_bus = CommandBus()
    command_bus.register_handler(
        HelloWorldCommand,
        HelloWorldCommandHandler(),
    )

    # Execute a command
    command = HelloWorldCommand('cqs')
    await command_bus.execute(command)


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

More usage examples on tests.

  • Using the QueryBus.
import asyncio
from dataclasses import dataclass

from pycqs import (
    Query,
    QueryBus,
    QueryHandler,
    QueryResult,
)


# Create the query
@dataclass
class HelloWorldQuery(Query):
    name: str


# Create the query result
@dataclass
class HelloWorldQueryResult(QueryResult):
    message: str


# Create the query handler
class HelloWorldQueryHandler(
    QueryHandler[
        HelloWorldQuery,
        HelloWorldQueryResult,
    ],
):
    async def execute(
        self,
        query: HelloWorldQuery,
    ) -> HelloWorldQueryResult:
        message = f'Hello World, {query.name}'
        return HelloWorldQueryResult(message)


async def main() -> None:
    # Initialize the query bus
    query_bus = QueryBus()
    query_bus.register_handler(
        HelloWorldQuery,
        HelloWorldQueryHandler(),
    )

    # Execute a query
    query = HelloWorldQuery('cqs')
    query_result: HelloWorldQueryResult = await query_bus.execute(query)
    print(query_result.message)


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

More usage examples on tests.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycqs-1.0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

pycqs-1.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pycqs-1.0.1.tar.gz.

File metadata

  • Download URL: pycqs-1.0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pycqs-1.0.1.tar.gz
Algorithm Hash digest
SHA256 486f9cd9cd1a129038dfa3e905d104ae5917236aa97ed3d37f8a2d7b259200e3
MD5 2014f9aaeb6b0351d17bc008d31dc558
BLAKE2b-256 e759dc637560e53606a28bc7b21246e20bec2ed2a4bb4f9607dbb12975b43ea7

See more details on using hashes here.

File details

Details for the file pycqs-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pycqs-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pycqs-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4677a9c8bce34f77f06e96341d4df099290d34558cc0e8f872d229a673b49af1
MD5 3daa2212a54bab4df1f24b62846f0ab8
BLAKE2b-256 c1420074ffa3857607a29ba4a9c2b6982ef3b1d26d923f59eb592d5cc5a159dd

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