Skip to main content

A comprehensive Python framework implementing Hexagonal Architecture with multi-framework, multi-database, and multi-messaging support

Project description

Python Hexagonal Architecture Package

A Python package implementing hexagonal architecture pattern with multi-framework web support.

Features

  • Hexagonal Architecture: Clean separation of concerns with ports and adapters
  • Multi-Framework Support: FastAPI, Flask, and Tornado support out of the box
  • Base Controllers: Generic CRUD operations with filtering
  • Multi-Database Support: PostgreSQL, MariaDB, SQL Server, Oracle
  • Multi-Messaging Support: Kafka, RabbitMQ, AWS Kinesis, GCP Pub/Sub
  • Multi-Cache Support: Redis, MemCache, and In-Memory caching
  • Type Safety: Full type hints support

Structure

src/
├── adapters/           # External adapters (web, db, cache, etc.)   ├── routers/       # Web framework routers   ├── repositories/  # Data access implementations   ├── caches/        # Cache implementations   └── events/        # Event handlers
├── controllers/        # Application controllers
├── models/            # Domain models
├── ports/             # Application ports (interfaces)
├── schemas/           # Data schemas
└── config/            # Configuration

Installation

Quick Install

# Basic installation (includes PostgreSQL and FastAPI)
pip install py-hexagonal-arch

# Install with specific extras
pip install py-hexagonal-arch[redis,kafka,flask]

# Install everything (all adapters)
pip install py-hexagonal-arch[all]

Available Extras

# Web frameworks
pip install py-hexagonal-arch[flask]      # Flask support
pip install py-hexagonal-arch[tornado]    # Tornado support

# Databases  
pip install py-hexagonal-arch[mysql]      # MariaDB/MySQL support
pip install py-hexagonal-arch[sqlserver]  # SQL Server support
pip install py-hexagonal-arch[oracle]     # Oracle support

# Cache systems
pip install py-hexagonal-arch[redis]      # Redis support
pip install py-hexagonal-arch[memcache]   # MemCache support

# Event messaging
pip install py-hexagonal-arch[kafka]      # Kafka support
pip install py-hexagonal-arch[rabbitmq]   # RabbitMQ support  
pip install py-hexagonal-arch[kinesis]    # AWS Kinesis support
pip install py-hexagonal-arch[pubsub]     # GCP Pub/Sub support

# Development tools
pip install py-hexagonal-arch[dev]        # Development dependencies
pip install py-hexagonal-arch[docs]       # Documentation tools

Quick Start

1. Create a Model

from py_hexagonal_arch import CustomModel
from typing import Optional

class User(CustomModel):
    id: Optional[str] = None
    name: str
    email: str
    age: int

2. Create a Controller

from py_hexagonal_arch import BaseController

class UserController(BaseController[User]):
    # Implement your business logic
    pass

3. Set Up Repository

from py_hexagonal_arch import BaseRepository

class UserRepository(BaseRepository[User]):
    def __init__(self):
        super().__init__(model=User, schema=UserSchema)

# PostgreSQL (default)
user_repo = UserRepository()

# MariaDB/MySQL
user_repo = UserRepository(db_type="mariadb")

# SQL Server
user_repo = UserRepository(db_type="sqlserver")

# Oracle
user_repo = UserRepository(db_type="oracle")

# Basic operations
user = User(name="John", email="john@example.com")
created_user = await user_repo.create(user)
users = await user_repo.list()

📖 For detailed repository documentation, configuration, and advanced usage, see: src/adapters/repositories/README.md

4. Create a Router

from py_hexagonal_arch import BaseRouter

# FastAPI (default)
user_router = BaseRouter(
    model=User,
    controller=UserController,
    prefix="/users",
    tags=["users"]
)

# Flask
user_router = BaseRouter(
    model=User,
    controller=UserController,
    prefix="/users",
    tags=["users"],
    framework="flask"
)

# Tornado
user_router = BaseRouter(
    model=User,
    controller=UserController,
    prefix="/users",
    tags=["users"],
    framework="tornado"
)

📖 For detailed router documentation, patterns, and advanced usage, see: src/adapters/routers/README.md

5. Set Up Caching

from py_hexagonal_arch import BaseCache

class UserCache(BaseCache[User]):
    def __init__(self):
        super().__init__(model=User)

# Redis (default)
user_cache = UserCache()

# MemCache
user_cache = UserCache(
    cache_type="memcache",
    servers=["localhost:11211"]
)

# In-Memory (for testing)
user_cache = UserCache(cache_type="memory")

# Usage
user = User(id="1", name="John", email="john@example.com", age=30)
await user_cache.set("user:1", user)
cached_user = await user_cache.get("user:1")

📖 For detailed cache documentation, patterns, and advanced usage, see: src/adapters/caches/README.md

6. Set Up Events

from py_hexagonal_arch import BaseEvent

class UserEvent(BaseEvent[User]):
    def __init__(self):
        super().__init__(model=User)

# Kafka (default)
user_events = UserEvent()

# RabbitMQ
user_events = UserEvent(event_type="rabbitmq")

# AWS Kinesis
user_events = UserEvent(event_type="kinesis")

# Basic operations
await user_events.push("created", user, key=user.id)
async for user_data in user_events.pull("created"):
    print(f"User event: {user_data.name}")

📖 For detailed event documentation, patterns, and advanced usage, see: src/adapters/events/README.md

Examples

See the examples/ directory for complete working examples with each framework and caching system.

  • fastapi_example.py - FastAPI implementation
  • flask_example.py - Flask implementation
  • tornado_example.py - Tornado implementation
  • repositories_example.py - Multi-database repository examples
  • cache_example.py - Comprehensive caching examples
  • events_example.py - Multi-backend event messaging examples

License

MIT License

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

py_hexagonal_arch-1.0.3.tar.gz (42.5 kB view details)

Uploaded Source

Built Distribution

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

py_hexagonal_arch-1.0.3-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

Details for the file py_hexagonal_arch-1.0.3.tar.gz.

File metadata

  • Download URL: py_hexagonal_arch-1.0.3.tar.gz
  • Upload date:
  • Size: 42.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for py_hexagonal_arch-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ffa2d0ee7b2ded3a4b9bf21eede4c40f62a0f3888bda283ba748595936ecdaf3
MD5 49557b75b003c44ec51ae3f0d9e63d07
BLAKE2b-256 1c566b78bcbc83e2b1b1ed4b277f32df676ee7f88ed3013f55043d0b5d453fe5

See more details on using hashes here.

File details

Details for the file py_hexagonal_arch-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for py_hexagonal_arch-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8b856b2f472656dd66e14876ed6d6832957bf1623b55132fc73ec9e3b01cfc5f
MD5 5524221ec10eebf3adac9361e4b33bc8
BLAKE2b-256 08be51e5b9d0281d82ee44664c108cfca6015debbdd0d0f3f382ea0cdce9fa31

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