Skip to main content

pyline core

Project description

PyLine Core

A lightweight Python framework for implementing the Command Query Responsibility Segregation (CQRS) pattern with pipeline orchestration capabilities.

Overview

PyLine Core provides a clean architecture for building applications using the CQRS pattern, where commands and queries are separated, and handlers are managed through a mediator pattern. The framework also includes a pipeline system for orchestrating complex workflows.

Features

  • Command Pattern: Separate command objects from their handlers
  • Query Pattern: Dedicated query objects with result types
  • Mediator Pattern: Centralized handler registration and execution
  • Pipeline Orchestration: Chain commands and queries in sequential workflows
  • Async Support: Native asyncio support for high-performance I/O operations
  • Type Safety: Built with Python type hints for better IDE support
  • Minimal Dependencies: Lightweight with no external dependencies

Installation

pip install pyline-core

Quick Start

1. Define Commands and Handlers

from pyline import Command, CommandHandler
from dataclasses import dataclass

@dataclass
class CreateUserCommand(Command):
    name: str

class CreateUserCommandHandler(CommandHandler):
    async def handle(self, command: CreateUserCommand):
        print(f"Creating user: {command.name}")
        # Your business logic here

2. Define Queries and Handlers

from pyline import Query, QueryResult, QueryHandler
from dataclasses import dataclass

@dataclass
class GetUserByNameQuery(Query):
    name: str

@dataclass
class GetUserByNameQueryResult(QueryResult):
    user: dict
    email: str

class GetUserByNameQueryHandler(QueryHandler):
    async def handle(self, query: GetUserByNameQuery):
        # Your data access logic here
        return GetUserByNameQueryResult(
            user={"id": 1, "name": query.name, "email": "user@example.com"},
            email="user@example.com"
        )

3. Register Handlers

from pyline import mediator

mediator.register_handler(CreateUserCommand, CreateUserCommandHandler())
mediator.register_handler(GetUserByNameQuery, GetUserByNameQueryHandler())

4. Execute Commands and Queries

import asyncio

async def main():
    # Execute a command
    command = CreateUserCommand(name="John Doe")
    await mediator.send(command)

    # Execute a query
    query = GetUserByNameQuery(name="John Doe")
    result = await mediator.send(query)
    print(f"User email: {result.email}")

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

Pipeline Orchestration

PyLine Core includes a powerful pipeline system for orchestrating complex workflows:

from pyline.pipe import Pipe

# Define a pipeline
create_user_pipe = Pipe(
    name="Create User Pipeline",
    context={
        "name": "John Doe",
    },
    steps=[
        CreateUserCommand,
        GetUserByNameQuery,
        # Add more commands/queries as needed
    ],
)

# Execute the pipeline
async def main():
    await create_user_pipe.run()

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

Pipeline Features

  • Context Sharing: Data flows between pipeline steps through a shared context
  • Automatic Parameter Mapping: Pipeline automatically maps context data to command/query parameters
  • Result Propagation: Query results are automatically added to the context for subsequent steps
  • Step Tracking: Built-in logging shows progress through pipeline execution

Architecture

Core Components

  1. Command: Abstract base class for commands
  2. CommandHandler: Abstract base class for command handlers
  3. Query: Abstract base class for queries
  4. QueryResult: Abstract base class for query results
  5. QueryHandler: Abstract base class for query handlers
  6. HandlerMediator: Central registry and dispatcher for handlers
  7. Pipe: Pipeline orchestration system

Design Patterns

  • Command Pattern: Encapsulates requests as objects
  • Query Pattern: Separates read operations from write operations
  • Mediator Pattern: Centralizes communication between components
  • Pipeline Pattern: Orchestrates sequential execution of operations

Architecture Flow

graph TD
    Client([Client / API]) -->|Starts Workflow| Context[{Shared Context}]
    Client -->|Triggers| P[Pipe Orchestrator]
    
    P --> |Step 1: Extracts Params| C[Command / Query]
    P --> |Step 2: Sends to Mediator| M{Handler Mediator}
    
    M --> |Resolves Handler| H[CommandHandler / QueryHandler]
    H --> |Executes Logic| DB[(Data Layer)]
    
    H -.-> |Returns Result| P
    P -.-> |Updates Context| Context
    
    P --> |Next Step| C2[Next Command / Query]

Requirements

  • Python 3.10+

License

This project is licensed under the terms specified in the LICENSE file.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Alp Sakaci
Email: alp@alpsakaci.com

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

pyline_core-2.2.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyline_core-2.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pyline_core-2.2.0.tar.gz.

File metadata

  • Download URL: pyline_core-2.2.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for pyline_core-2.2.0.tar.gz
Algorithm Hash digest
SHA256 8f50507ed59562cfd53546819881835c5a6db7c4a81af9758bce621479467a24
MD5 aa02b15928c55df4b2e44932d029a76b
BLAKE2b-256 cbdd2d2f14460faa8f62d58e6ddfe6fb492533a1aad104d1ba43a791606edb2a

See more details on using hashes here.

File details

Details for the file pyline_core-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyline_core-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for pyline_core-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac74ddaa9185ddcdc347295e660f83168f64674c64d73fde194d4b03eb7dc1ea
MD5 f4c4f189362cc62c8ae9c58e791eed0b
BLAKE2b-256 bb50f880f79649486f1bd0534b24b390b6599e68885f10ee412d017fdd7a99f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page