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

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.0.0.tar.gz (4.7 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.0.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyline_core-2.0.0.tar.gz
  • Upload date:
  • Size: 4.7 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.0.0.tar.gz
Algorithm Hash digest
SHA256 1da585129145a52b60697461f72e1f7f3eed7276938d32f1ee3242afbbf8c061
MD5 3f06bf83f4e8cd89bb01eadc8744e90b
BLAKE2b-256 6f63a8968a01a12dcfd21a0f69ecd3b3e47db92b5e6be5dccc198563ddeaf28b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyline_core-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f95a63400e4152e84cc9db072aef21711142a81c379870071931d77f5b96adf4
MD5 1cb70c39b038b426336bc1ed615adfd5
BLAKE2b-256 fabbff68c19468b61c68e51594647722c179f44056f2df254793ffb0ae8abe4c

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