Python Simple IoC - Dependency Injection Container
A lightweight, Pythonic dependency injection container inspired by Symfony's DependencyInjection component. This library embraces Python's philosophy of simplicity while providing powerful tools for organizing complex applications.
🎯 Why Use Dependency Injection?
Dependency injection isn't just enterprise complexity - it's a Pythonic pattern that promotes:
- Clear separation of concerns - Each class has a single responsibility
- Testability - Easy to mock dependencies for unit tests
- Flexibility - Change implementations without touching existing code
- Maintainability - Explicit dependencies make code easier to understand
This approach aligns perfectly with Python's zen: "Explicit is better than implicit" and "Readability counts".
🚀 Quick Start
Installation
pip install ioc
Basic Usage
- Define your services in a
services.ymlfile:
parameters:
database_url: "sqlite:///app.db"
debug_mode: true
services:
# Database connection
database:
class: myapp.database.Database
arguments: ["%database_url%"]
# User repository with injected database
user_repository:
class: myapp.repositories.UserRepository
arguments: ["@database"]
# User service with injected repository
user_service:
class: myapp.services.UserService
arguments: ["@user_repository"]
calls:
- [set_debug, ["%debug_mode%"]]
- Use the container in your application:
import ioc
# Build container from configuration
container = ioc.build(['services.yml'])
# Get your services - dependencies are automatically resolved!
user_service = container.get('user_service')
# Your service is ready to use with all dependencies injected
users = user_service.get_all_users()
🏗️ Perfect for Python Projects
This library follows Python best practices:
- Configuration over code - Define dependencies in YAML, not scattered across your codebase
- Explicit dependencies - See exactly what each service needs at a glance
- No magic - Simple, predictable behavior that follows Python conventions
- Framework agnostic - Works with Flask, Django, FastAPI, or pure Python
📚 Advanced Features
Service Definitions
services:
# Constructor injection
email_service:
class: myapp.EmailService
arguments: ["@mailer", "%sender_email%"]
# Method calls after construction
logger:
class: logging.Logger
arguments: ["myapp"]
calls:
- [setLevel, ["INFO"]]
- [addHandler, ["@file_handler"]]
# Weak references (lazy loading)
cache_service:
class: myapp.CacheService
arguments: ["#@redis_client"] # Only loaded when needed
Parameters and Environment
parameters:
# String interpolation
log_file: "/var/log/%app_name%.log"
# Environment variables
secret_key: "%env(SECRET_KEY)%"
# Default values
redis_url: "%env(REDIS_URL):redis://localhost:6379%"
🧪 Testing Made Easy
With dependency injection, testing becomes straightforward:
import unittest
from unittest.mock import Mock
class TestUserService(unittest.TestCase):
def test_create_user(self):
# Mock the repository
mock_repo = Mock()
mock_repo.save.return_value = True
# Inject the mock
user_service = UserService(mock_repo)
# Test with confidence
result = user_service.create_user("john@example.com")
self.assertTrue(result)
mock_repo.save.assert_called_once()
📖 Learn More
🤝 Contributing
Contributions are welcome! This project follows Python community standards:
- PEP 8 code style
- Type hints for better IDE support
- Comprehensive tests
- Clear documentation
📄 License
Licensed under the Apache License 2.0. See LICENSE for details.
"Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex." - The Zen of Python
This library embodies these principles while providing the power and flexibility needed for serious Python applications.
Metadata
Release files for ioc 0.1.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ioc-0.1.18.tar.gz | 24.9 kB | Without provenance | Details for ioc-0.1.18.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ioc-0.1.18-py3-none-any.whl | Python 3 | none | any | Details for ioc-0.1.18-py3-none-any.whl |
Total release size: 63.1 kB
Release files / ioc-0.1.18.tar.gz
| Download URL | ioc-0.1.18.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55f03e2d4a3c40159a09950d4aea289b7944d2cc0ac15053c8d41400376a1583
|
|
BLAKE2b-256 checksum How to use checksums |
29b5fc823d461cd2eba4e8b949dd5787b23666173fbf1ca26bcf71c15c7961df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / ioc-0.1.18-py3-none-any.whl
| Download URL | ioc-0.1.18-py3-none-any.whl |
|---|---|
| Size | 38.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eff109e7ee8836a7629d197194a884838b16bad237c4c6e3e5f7dcd0e4f22f69
|
|
BLAKE2b-256 checksum How to use checksums |
f371e343da3c3455b8fe160df903cb736f1af6eb1393f642a77e4097e8efa5f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Security for ioc 0.1.18
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.