tessl install tessl/pypi-rodi@2.0.0Implementation of dependency injection for Python 3
Agent Success
Agent success rate when using this tile
92%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.06x
Baseline
Agent success rate without this tile
87%
Build a service configuration system using a dependency injection container with factory functions that manage different service lifetimes.
Implement a Logger class with the following:
name parameter (string)log(message: str) method that prints: "[{name}] {message}"Implement a RequestContext class with the following:
request_id attribute (string)request_id parameterImplement a DatabaseService class with the following:
logger parameter (Logger instance)query(sql: str) method that logs the SQL query using the loggerCreate a function setup_container() that configures a dependency injection container with factory functions:
Logger instance with name "Application"RequestContext instances with request_id "scoped-request"DatabaseService instances, each with a new Logger named "DatabaseService"The function should return the configured container
# test_logger_system.test.py
def test_singleton_logger():
"""Test that singleton factory returns the same logger instance"""
container = setup_container()
provider = container.build_provider()
logger1 = provider.get(Logger)
logger2 = provider.get(Logger)
assert logger1 is logger2
assert logger1.name == "Application"# test_logger_system.test.py
def test_scoped_request_context():
"""Test that scoped factory returns same instance within scope"""
container = setup_container()
provider = container.build_provider()
with provider.create_scope() as scope1:
ctx1a = provider.get(RequestContext, scope1)
ctx1b = provider.get(RequestContext, scope1)
assert ctx1a is ctx1b
with provider.create_scope() as scope2:
ctx2 = provider.get(RequestContext, scope2)
assert ctx2 is not ctx1a# test_logger_system.test.py
def test_transient_database_service():
"""Test that transient factory creates new instances"""
container = setup_container()
provider = container.build_provider()
db1 = provider.get(DatabaseService)
db2 = provider.get(DatabaseService)
assert db1 is not db2
assert db1.logger.name == "DatabaseService"
assert db2.logger.name == "DatabaseService"Provides dependency injection container for service registration and resolution.