Author: Trix Cyrus(Vicky)
Every Python application needs the same foundation: configuration management, logging, dependency injection, and lifecycle management. Yet developers often spend hours setting up these basic infrastructure components before writing a single line of business logic.
Enter pystack-core - a unified runtime layer that provides essential application infrastructure through a single, coherent API.
The Problem
If you've built production Python applications, you know the drill:
# Setting up configuration
from dotenv import load_dotenv
import os
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
# Setting up logging
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Setting up dependency injection
# (You probably end up writing your own or using a complex framework)
# Setting up middleware
# (More custom code or another library)
Multiply this by 20+ different concerns (HTTP, caching, databases, AI, scheduling, metrics, etc.) and you've spent more time on infrastructure than your actual application.
The Solution
pystack-core consolidates all these concerns into a single, production-ready package:
from py_core import App, AppConfig
app = App(AppConfig(
name="my-app",
environment="production",
log_level="INFO"
))
await app.start()
app.logger.info("Application started")
await app.stop()
Everything is configured and ready to use. No boilerplate, no configuration hell.
v0.1.0 Features
Core Runtime
The v0.1.0 release includes production-ready implementations of:
- Application Lifecycle Management - Async-aware startup/shutdown with hooks
- Dependency Injection - Singleton/transient resolution with decorators
- Middleware Pipeline - Cross-cutting concerns (request ID, timing, etc.)
- Context Management - Application state management
from py_core import App, AppConfig
def on_startup(app):
app.logger.info("Initializing resources...")
def on_shutdown(app):
app.logger.info("Cleaning up...")
app = App(AppConfig(name="my-app"))
app.add_startup_hook(on_startup)
app.add_shutdown_hook(on_shutdown)
await app.start()
# Your application logic
await app.stop()
Configuration System
Multi-source configuration loading with automatic type conversion and Pydantic validation:
from py_core import Config, AppConfig
from pydantic import BaseModel, Field
class DatabaseConfig(BaseModel):
url: str = Field(..., min_length=1)
pool_size: int = Field(default=10, ge=1, le=100)
config = Config(AppConfig(config_path="config.yaml"))
await config.load()
# Automatic type conversion
db_url = config.get("database.url") # from YAML/env
debug = config.get("debug") # string "true" -> bool True
# Pydantic validation
db_config = config.validate_section("database", DatabaseConfig)
Configuration sources:
- Environment variables (with automatic type conversion)
- YAML files
- JSON files
- TOML files
- Smart merging from multiple sources
Logging System
Structured logging with automatic context injection and async support:
from py_core import Logger, LogLevel
logger = Logger(name="my-app", level=LogLevel.INFO)
# Global context
logger.add_global_context(app_version="1.0.0")
# Request-specific context
request_logger = logger.with_context(
request_id="req-12345",
user_id="user-67890"
)
request_logger.info("Processing request")
# Async logging for high throughput
logger.enable_async_logging()
await logger.start_async()
await logger.ainfo("Async log message")
Features:
- Multiple formatters (console with colors, JSON, text)
- Async logging with queue-based processing
- File handlers with rotation support
- Request tracking with async-safe contextvars
- Cloud logging adapters (CloudWatch, Loggly)
Performance
pystack-core is designed for production environments:
- Logging: 10,000+ logs/sec throughput
- Configuration: 1,000+ loads/sec
- App startup: <1 second
- App shutdown: <1 second
- Dependency resolution: 10,000+ resolutions/sec
Testing & Quality
- 79 comprehensive tests (all passing)
- Integration tests for all modules
- Performance benchmarks meeting production targets
- Memory efficiency tests
pytest
# 79 passed in 2.51s
Get Started
pip install pystack-core
import asyncio
from py_core import App, AppConfig
async def main():
app = App(AppConfig(
name="my-app",
environment="production",
log_level="INFO"
))
await app.start()
app.logger.info("Application started")
await app.stop()
asyncio.run(main())
Documentation
- Usage Guide - Comprehensive examples and patterns
- API Documentation - Complete API reference
- Changelog - Version history
- Examples - Working code examples
Roadmap
v0.1.0 focuses on the foundation: Core Runtime, Configuration, and Logging. Future releases will include:
- HTTP Client with automatic retry, timeout, metrics, and tracing
- Multi-backend caching (Memory, Redis, Disk)
- Unified database interface (PostgreSQL, MySQL, SQLite, MongoDB)
- Provider-agnostic AI interface (OpenAI, Anthropic, Gemini)
- Background tasks with multiple backends
- Scheduler with cron and natural language support
- Unified secrets management
- Automatic metrics collection and export
- Event bus for cross-module communication
Conclusion
pystack-core aims to become the standard runtime layer that Python developers begin their applications with. Instead of spending time on infrastructure setup, focus on what matters: your business logic.
Check it out:
- 📦 GitHub: https://github.com/TrixSec/pystack-core
- 📖 Documentation: https://github.com/TrixSec/pystack-core/blob/main/docs/API.md
- 💡 Examples: https://github.com/TrixSec/pystack-core/tree/main/examples
Built with ❤️ for Python developers.
Top comments (0)