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 implementationflask_example.py- Flask implementationtornado_example.py- Tornado implementationrepositories_example.py- Multi-database repository examplescache_example.py- Comprehensive caching examplesevents_example.py- Multi-backend event messaging examples
License
MIT License
Release files for py-hexagonal-arch 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_hexagonal_arch-1.0.3.tar.gz | 42.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_hexagonal_arch-1.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 83.9 kB
Release files / py_hexagonal_arch-1.0.3.tar.gz
| Download URL | py_hexagonal_arch-1.0.3.tar.gz |
|---|---|
| Size | 42.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ffa2d0ee7b2ded3a4b9bf21eede4c40f62a0f3888bda283ba748595936ecdaf3
|
|
BLAKE2b-256 checksum How to use checksums |
1c566b78bcbc83e2b1b1ed4b277f32df676ee7f88ed3013f55043d0b5d453fe5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|
Release files / py_hexagonal_arch-1.0.3-py3-none-any.whl
| Download URL | py_hexagonal_arch-1.0.3-py3-none-any.whl |
|---|---|
| Size | 41.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8b856b2f472656dd66e14876ed6d6832957bf1623b55132fc73ec9e3b01cfc5f
|
|
BLAKE2b-256 checksum How to use checksums |
08be51e5b9d0281d82ee44664c108cfca6015debbdd0d0f3f382ea0cdce9fa31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|