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
  • 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):
    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):
    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

# Execute a command
command = CreateUserCommand(name="John Doe")
mediator.send(command)

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

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
create_user_pipe.run()

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-0.1.tar.gz (4.2 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-0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyline_core-0.1.tar.gz
Algorithm Hash digest
SHA256 15068c303f21f1e90461658b681e7db4922e4da59a9b69c9d308f2957cdaaa41
MD5 4da0ad997f1d2f637ddd492476ccd1f0
BLAKE2b-256 f68de44cc0b9bfc39bef3cf4b2656425a85167524fea61601854183efaf677b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyline_core-0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.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-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ed48952672a27a839c704c7a9716f58e2f7d76fc63588023cea0216331a861a
MD5 7b8b76bd317b51a564ae55e84666b423
BLAKE2b-256 d43f7140f489eff7116353acba778917e43a013ef3756a3b7c5bd6d85dde0f9e

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