pyline core
Project description
PyLine Core
A lightweight Python framework for implementing the Command Query Responsibility Segregation (CQRS) pattern with pipeline orchestration capabilities.
🚀 Key Features
- CQRS Implementation: Strong separation between Commands (write) and Queries (read).
- Mediator Pattern: Decouple components with a centralized handler registration.
- Pipeline Orchestration: Execute sequences of steps with a shared context and automatic parameter mapping.
- Event-Driven Architecture: Powerful background
EventBuswith Publish/Subscribe support and graceful shutdown. - Type-Safe: Modern Python type hints with
@overloadsupport for superior developer experience. - Micro-Framework: Ultra-lightweight with zero external dependencies in core.
📦 Installation
pip install pyline-core
🚥 Quick Start
1. Define Components
from pyline import Command, Query, CommandHandler, QueryHandler
from dataclasses import dataclass
@dataclass
class CreateUserCommand(Command):
name: str
class CreateUserCommandHandler(CommandHandler):
async def handle(self, command: CreateUserCommand) -> None:
print(f"Creating user: {command.name}")
@dataclass
class GetUserQuery(Query):
name: str
class GetUserQueryHandler(QueryHandler):
async def handle(self, query: GetUserQuery):
return {"id": 1, "name": query.name}
2. Register and Execute
from pyline import mediator
# Registration
mediator.register_handler(CreateUserCommand, CreateUserCommandHandler())
mediator.register_handler(GetUserQuery, GetUserQueryHandler())
# Execution
async def main():
await mediator.send(CreateUserCommand(name="Alp"))
user = await mediator.send(GetUserQuery(name="Alp"))
print(user)
🛠 Advanced: Pipeline Orchestration
Chain multiple commands and queries into a single workflow with shared context:
from pyline.pipe import Pipe
pipe = Pipe(
name="Registration Flow",
context={"name": "John Doe"},
steps=[CreateUserCommand, GetUserQuery]
)
await pipe.run()
📻 Event-Driven Architecture (Event Bus)
PyLine Core includes a lightweight EventBus to decouple components and handle side-effects asynchronously without blocking the main execution:
from pyline import BaseEvent, EventHandler, EventBus
from dataclasses import dataclass
import asyncio
@dataclass(frozen=True, kw_only=True)
class UserCreatedEvent(BaseEvent):
user_id: int
name: str
class EmailNotificationHandler(EventHandler[UserCreatedEvent]):
async def handle(self, event: UserCreatedEvent) -> None:
print(f"Sending welcome email to User {event.user_id} ({event.name})")
async def main():
bus = EventBus()
bus.subscribe(UserCreatedEvent, EmailNotificationHandler())
# Publish event (runs in the background)
bus.publish(UserCreatedEvent(user_id=1, name="Alp"))
# Gracefully shut down and wait for all background tasks
await bus.shutdown()
if __name__ == "__main__":
asyncio.run(main())
📖 Documentation
For detailed guides and full API reference, visit our documentation site (coming soon).
🤝 Contributing
Contributions are welcome! Please see CONTRIBUTING.md for local development setup and standards.
📄 License
MIT. See LICENSE for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyline_core-3.0.0.tar.gz.
File metadata
- Download URL: pyline_core-3.0.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8bdf3751d19d93acc33397aeaf2b916523cc6356d48ebbe067ee4a3bd6cf1d92
|
|
| MD5 |
a88b5e1d6abf987f5825bdfa81e878f6
|
|
| BLAKE2b-256 |
f7eef48ba8aad4c3b7267a0cddc9cd03b8cba2f7dc9a508b839c1ee8331eafb4
|
File details
Details for the file pyline_core-3.0.0-py3-none-any.whl.
File metadata
- Download URL: pyline_core-3.0.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a9d6674ee35d9b44d4c3bcfe087432043443c2137dedb0bf5f06b77da75f7ee
|
|
| MD5 |
31c88da42aaf17c6d70ec5317ab8de0d
|
|
| BLAKE2b-256 |
d0dc9ea4fd8e3b12bdbd469fd8f415bc3642233547c3839b91f42f6ffdc83006
|