Implementation of dependency injection for Python 3
Overall
score
92%
Build a plugin management system that supports multiple dependency injection container implementations through a common interface.
You need to create a plugin system where plugins can be loaded dynamically and their services registered with any DI container implementation that follows a common protocol. The system should support switching between different container implementations without changing plugin code.
Create a ContainerWrapper class that:
register, resolve, and __contains__ methods)Each plugin should:
register_services method that accepts a container instanceregister(), resolve(), and __contains__Create a PluginManager class that:
load_plugin method that calls the plugin's register_services methodget_service method that resolves services from the underlying container@generates
from typing import Protocol, Any, Type
class ContainerProtocol(Protocol):
"""Protocol defining the minimal interface for a DI container."""
def register(self, base_type: Type, concrete_type: Type = None) -> None:
"""Register a service type with the container."""
...
def resolve(self, service_type: Type) -> Any:
"""Resolve and return an instance of the service type."""
...
def __contains__(self, service_type: Type) -> bool:
"""Check if a service type is registered."""
...
class ContainerWrapper:
"""Wraps any container that follows ContainerProtocol."""
def __init__(self, container: ContainerProtocol) -> None:
"""Initialize with a container that implements the protocol."""
...
def register(self, base_type: Type, concrete_type: Type = None) -> None:
"""Register a service type."""
...
def resolve(self, service_type: Type) -> Any:
"""Resolve a service type."""
...
def has_service(self, service_type: Type) -> bool:
"""Check if service is registered."""
...
class Plugin(Protocol):
"""Protocol for plugins that can register services."""
def register_services(self, container: ContainerProtocol) -> None:
"""Register plugin services with the container."""
...
class PluginManager:
"""Manages plugins and their service registration."""
def __init__(self, container_wrapper: ContainerWrapper) -> None:
"""Initialize with a container wrapper."""
...
def load_plugin(self, plugin: Plugin) -> None:
"""Load a plugin by calling its register_services method."""
...
def get_service(self, service_type: Type) -> Any:
"""Get a service from the underlying container."""
...Python dependency injection library for container implementation.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/pypi-rodidocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10