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-4/

Event Handler System

Build a simple event handling system that uses dependency injection to execute event handlers with their required dependencies automatically injected.

Requirements

Create a system that:

  1. Registers event handler functions that require various dependencies (e.g., logger, database connection, configuration)
  2. Executes these handlers when events are triggered, automatically injecting the required dependencies
  3. Supports both synchronous and asynchronous event handlers
  4. Returns the result from the executed handler

Your implementation should consist of:

  • An EventManager class that manages and dispatches events to registered handlers
  • Event handler functions that have dependencies specified via type hints in their parameters
  • A method to register handlers for specific event types
  • A method to trigger events and execute the appropriate handler with injected dependencies

Test Cases

  • Executing a synchronous handler function with injected logger returns the correct result @test
  • Executing an async handler function with injected database connection returns the correct result @test
  • Handler receives multiple injected dependencies (logger and config) correctly @test

Implementation

@generates

API

from typing import Callable, Any, TypeVar

T = TypeVar('T')

class EventManager:
    """Manages event handlers with dependency injection."""

    def __init__(self, provider):
        """
        Initialize the event manager with a service provider.

        Args:
            provider: A rodi Services instance that provides dependency resolution
        """
        pass

    def register_handler(self, event_type: str, handler: Callable[..., T]) -> None:
        """
        Register a handler function for a specific event type.

        Args:
            event_type: The name/type of the event
            handler: A function that will be called when the event is triggered.
                    Dependencies will be automatically injected based on type hints.
        """
        pass

    def trigger_event(self, event_type: str) -> Any:
        """
        Trigger an event and execute its registered handler with dependency injection.

        Args:
            event_type: The name/type of the event to trigger

        Returns:
            The result returned by the handler function

        Raises:
            ValueError: If no handler is registered for the event type
        """
        pass

Dependencies { .dependencies }

rodi { .dependency }

Provides dependency injection functionality for executing functions with auto-injected parameters.

@satisfied-by