or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/rodi@2.0.x
tile.json

tessl/pypi-rodi

tessl install tessl/pypi-rodi@2.0.0

Implementation 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%

task.mdevals/scenario-5/

Service Configuration with Factory Registration

Build a service configuration system using a dependency injection container with factory functions that manage different service lifetimes.

Requirements

  1. Implement a Logger class with the following:

    • Constructor that accepts a name parameter (string)
    • A log(message: str) method that prints: "[{name}] {message}"
  2. Implement a RequestContext class with the following:

    • A request_id attribute (string)
    • Constructor that accepts a request_id parameter
  3. Implement a DatabaseService class with the following:

    • Constructor that accepts a logger parameter (Logger instance)
    • A query(sql: str) method that logs the SQL query using the logger
  4. Create a function setup_container() that configures a dependency injection container with factory functions:

    • A singleton factory that creates a Logger instance with name "Application"
    • A scoped factory that creates RequestContext instances with request_id "scoped-request"
    • A transient factory that creates DatabaseService instances, each with a new Logger named "DatabaseService"
  5. The function should return the configured container

Test Cases { .test }

Test 1: Singleton Factory @test

# 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 2: Scoped Factory @test

# 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 3: Transient Factory @test

# 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"

Dependencies { .dependencies }

rodi { .dependency }

Provides dependency injection container for service registration and resolution.