Dependency Injection Container inspired by Inversify for Python
Project description
Inversify for Python
A dependency injection container inspired by Inversify for Python.
This library supports hierarchical containers, various binding types (toSelf, to, constant, dynamic), and scopes (singleton and transient).
Examples
from inversify import Container, injectable, inject_params
class Token:
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return isinstance(other, Token) and self.name == other.name
# Tokens for bindings
LOGGER_TOKEN = Token("LOGGER")
USER_SERVICE_TOKEN = Token("USER_SERVICE")
@injectable
class Logger:
def __init__(self):
self.name = "Parent Logger"
def log(self, message: str):
print(f"[{self.name}]: {message}")
@injectable
class CustomLogger:
def __init__(self):
self.name = "Child Logger"
def log(self, message: str):
print(f"[{self.name}]: {message}")
@injectable
class UserService:
@inject_params({'logger': LOGGER_TOKEN})
def __init__(self, logger):
self.logger = logger
def process(self):
self.logger.log("Processing in UserService")
# Create parent container and bind tokens
parent_container = Container()
parent_container.bind(LOGGER_TOKEN).to(Logger).inSingletonScope()
parent_container.bind(USER_SERVICE_TOKEN).to(UserService).inTransientScope()
# Create child container and override the LOGGER_TOKEN binding
child_container = parent_container.create_child()
child_container.bind(LOGGER_TOKEN).to(CustomLogger).inSingletonScope()
# Get services from parent and child containers
parent_service = parent_container.get(USER_SERVICE_TOKEN)
child_service = child_container.get(USER_SERVICE_TOKEN)
parent_service.process() # Uses Parent Logger
child_service.process() # Uses Child Logger
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pynversify-1.0.2.tar.gz
(3.0 kB
view details)
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 pynversify-1.0.2.tar.gz.
File metadata
- Download URL: pynversify-1.0.2.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f22e7f56152237bcc7e27bc3c8974d54876602e91b7b17d5b32acae8fcc66f6f
|
|
| MD5 |
2a09edd115568a495c783bddd89622f7
|
|
| BLAKE2b-256 |
50898cf7eda5e93e52f2adf95108898a859db9897c9759b9bb9736b284f2479b
|
File details
Details for the file pynversify-1.0.2-py3-none-any.whl.
File metadata
- Download URL: pynversify-1.0.2-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12d295398e0086b1aec6f6050171e00ffceb931ae7619693aeb8efa9b90b9d75
|
|
| MD5 |
7546b9da39526693a217b271aa05bb3b
|
|
| BLAKE2b-256 |
641e67420a7e144c4a70820c84e922f601b64243ce453b5878603d03e01d22d2
|