
Security News
NIST Under Federal Audit for NVD Processing Backlog and Delays
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
light-dependency-injection
Advanced tools
LightDI is a lightweight, framework-agnostic dependency injection module for Python.
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))
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")
FAQs
A lightweight dependency injection module for Python.
We found that light-dependency-injection demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.
Security News
TypeScript Native Previews offers a 10x faster Go-based compiler, now available on npm for public testing with early editor and language support.