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
Project details
Release history Release notifications | RSS feed
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.0.tar.gz
(5.2 kB
view details)
Built Distribution
pycqs-1.0.0-py3-none-any.whl
(4.6 kB
view details)
File details
Details for the file pycqs-1.0.0.tar.gz
.
File metadata
- Download URL: pycqs-1.0.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df836b24a7590f6f8e9149ad61c1e960cb203d972f728630c741bc4b60550d1f |
|
MD5 | 35f11aa178fb04209b6743bf696c21df |
|
BLAKE2b-256 | 525a999492cef6c5b4f4dce3ba676cf0ce0a033808c874a67be0d90f832e0372 |
File details
Details for the file pycqs-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pycqs-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e4a178939821f63367a2b87ec17238bb8ce15072eea66ecda3c08d7eb028896 |
|
MD5 | 38e8777ff6fe02dc3162bebd48b82c93 |
|
BLAKE2b-256 | 8e8cbbc8d8f5b3643a59c374230d1109b38602fd6a67644f3115e7ef89c8d9bd |