Lightweight dependency injection container for Python.
Project description
philiprehberger-di
Lightweight dependency injection container for Python.
Installation
pip install philiprehberger-di
Usage
from philiprehberger_di import Container
container = Container()
container.register(Logger)
logger = container.resolve(Logger)
Singletons
container.register(Database, singleton=True)
a = container.resolve(Database)
b = container.resolve(Database)
assert a is b
Custom Factories
container.register(Cache, factory=lambda: Cache(max_size=256))
cache = container.resolve(Cache)
Recursive Resolution
class Service:
def __init__(self, db: Database, logger: Logger) -> None:
self.db = db
self.logger = logger
container.register(Database)
container.register(Logger)
container.register(Service)
service = container.resolve(Service) # db and logger are injected automatically
Inject Decorator
from philiprehberger_di import Container, inject
container = Container()
container.register(Logger, singleton=True)
@inject(container)
def handle_request(logger: Logger) -> str:
logger.log("request handled")
return "ok"
handle_request() # logger is resolved and injected automatically
Lifecycle Hooks
container.register(
Database,
singleton=True,
on_create=lambda db: db.connect(),
on_destroy=lambda db: db.disconnect(),
)
db = container.resolve(Database) # on_create called after creation
container.reset() # on_destroy called before clearing singletons
API
| Function / Class | Description |
|---|---|
Container() |
Create a new dependency injection container |
container.register(cls, factory?, singleton?, on_create?, on_destroy?) |
Register a class with optional factory, singleton flag, and lifecycle hooks |
container.resolve(cls) |
Resolve an instance, recursively injecting dependencies |
container.reset() |
Call on_destroy for singletons with hooks, then clear the cache |
inject(container) |
Decorator that resolves type-hinted params from the container |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this package useful, consider starring the repository.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_di-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_di-0.2.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95864d04b8a2c670fee8fd5713e65b9838696e0481cbdd5bdbfcf47839f67874
|
|
| MD5 |
0b673cd0efa7fae3a8e151b6f00f1f6a
|
|
| BLAKE2b-256 |
c18a791c08c8e57cf7dc1d26aea0459115fa65d149baed873cca363d57e50237
|
File details
Details for the file philiprehberger_di-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_di-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a94ef1709defe001145ee37ecc93f6e58ae090b960094f6a38f5c9309cefebbe
|
|
| MD5 |
47b0c54fe800527f3ce11e944e50a54b
|
|
| BLAKE2b-256 |
778e0b5a55067cda97da3cc455402c5fe53e069871e072378278fd2040cc03f0
|