A lightweight dependency injection module for Python.
Project description
LIGHT DI
Dependency Injection Module for Python
LightDI is a lightweight, framework-agnostic dependency injection module for Python.
Features
- Automatic dependency resolution
- Support for qualifiers
- Abstract class inference
Usage
Example 1: Basic Usage
from abc import ABC, abstractmethod
from lightdi import implementation, inject
class PaymentProcessor(ABC):
@abstractmethod
def process_payment(self, amount: int) -> str:
pass
@implementation(qualifier="credit_card")
class CreditCardProcessor(PaymentProcessor):
def process_payment(self, amount: int) -> str:
return f"Processed {amount} via Credit Card."
@implementation(qualifier="paypal", primary=True)
class PayPalProcessor(PaymentProcessor):
def process_payment(self, amount: int) -> str:
return f"Processed {amount} via PayPal."
class LogTransactionRepository(ABC):
@abstractmethod
def save(self, amount: int) -> None:
pass
@implementation(qualifier="in_memory")
class InMemoryLogTransactionRepository(LogTransactionRepository):
def save(self, amount: int):
self._write_to_file(f"Transaction saved: {amount}")
def _write_to_file(self, message) -> None:
with open("transactions.txt", "a") as file:
file.write(message + "\n")
@inject
class CheckoutService:
def __init__(self, payment_processor: PaymentProcessor):
self.payment_processor = payment_processor
def checkout(self, amount) -> str:
return self.payment_processor.process_payment(amount)
@inject
class PaymentController:
def __init__(self, checkout_service: CheckoutService, log_transaction_repository: LogTransactionRepository):
self.checkout_service = checkout_service
self.log_transaction_repository = log_transaction_repository
def process_checkout(self, amount) -> str:
self.log_transaction_repository.save(amount)
return self.checkout_service.checkout(amount)
if __name__ == "__main__":
controller = PaymentController()
print(controller.process_checkout(100))
Example 2: Wire repositories to variables
from abc import ABC, abstractmethod
from lightdi import implementation, wire
class LogRepository(ABC):
@abstractmethod
def log(self, message):
pass
@implementation(qualifier="console")
class ConsoleLogRepository(LogRepository):
def log(self, message):
print(message)
@implementation(qualifier="timestamp", primary=True)
class TimeStampLogRepository(LogRepository):
def log(self, message):
print(f"{__import__('datetime').datetime.now()}: {message}")
if __name__ == "__main__":
logger = wire(LogRepository)
logger.log("message to be logged")
console_logger = wire(LogRepository, qualifier="console")
console_logger.log("message to be logged")
Examples
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
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 light-dependency-injection-0.1.2.tar.gz.
File metadata
- Download URL: light-dependency-injection-0.1.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8926bcac09cc81f03f4bb4747928feb5073a98c75b424d0e3b14e741bcbfc8e8
|
|
| MD5 |
ade87d84263d64bc06f02a46ddcd34a3
|
|
| BLAKE2b-256 |
03b6ce8814bec1fa967b20356aa0b282fe8d24dfa49249816b4439300fe93882
|
Provenance
The following attestation bundles were made for light-dependency-injection-0.1.2.tar.gz:
Publisher:
publish.yml on PabloSanchi/light-di
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
light_dependency_injection-0.1.2.tar.gz -
Subject digest:
8926bcac09cc81f03f4bb4747928feb5073a98c75b424d0e3b14e741bcbfc8e8 - Sigstore transparency entry: 161817739
- Sigstore integration time:
-
Permalink:
PabloSanchi/light-di@46e84d6b957e99e16aff504f0f40e5fd7f5e4b73 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/PabloSanchi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@46e84d6b957e99e16aff504f0f40e5fd7f5e4b73 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file light_dependency_injection-0.1.2-py3-none-any.whl.
File metadata
- Download URL: light_dependency_injection-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff4f5559c0f4d964af88c48824a895a581e136c171c4c2db88fb8948bffa8410
|
|
| MD5 |
9182bfe1c6ce620a95ebf4b3c1c42cbe
|
|
| BLAKE2b-256 |
55452d4c664274a34edf43c2fa38fcba01d5b24513c5bb193e10c29f54ea52e2
|
Provenance
The following attestation bundles were made for light_dependency_injection-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on PabloSanchi/light-di
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
light_dependency_injection-0.1.2-py3-none-any.whl -
Subject digest:
ff4f5559c0f4d964af88c48824a895a581e136c171c4c2db88fb8948bffa8410 - Sigstore transparency entry: 161817740
- Sigstore integration time:
-
Permalink:
PabloSanchi/light-di@46e84d6b957e99e16aff504f0f40e5fd7f5e4b73 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/PabloSanchi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@46e84d6b957e99e16aff504f0f40e5fd7f5e4b73 -
Trigger Event:
workflow_dispatch
-
Statement type: