Skip to content

Event Handlers

ropt.plugins.event_handler.base.EventHandlerPlugin

Bases: Plugin

Abstract base class for event handler plugins.

This class defines the interface for plugins responsible for creating EventHandler instances within an optimization workflow. Event handlers respond to events after being connected to a compute step by the add_event_handler method. For each event emitted by the compute step, the handle_event method of connected handlers is called with the event.

create abstractmethod classmethod

create(name: str, **kwargs: Any) -> EventHandler

Create a EventHandler instance.

This abstract class method serves as a factory for creating concrete EventHandler objects. Plugin implementations must override this method to return an instance of their specific EventHandler subclass.

The name argument specifies the requested event handler, potentially in the format "plugin-name/method-name" or just "method-name". Implementations can use this name to vary the created event handler if the plugin supports multiple event handler types.

Parameters:

Name Type Description Default
name str

The requested event handler name (potentially plugin-specific).

required
kwargs Any

Additional arguments for custom configuration.

{}

Returns:

Type Description
EventHandler

An initialized instance of a EventHandler subclass.

ropt.plugins.event_handler.base.EventHandler

Bases: ABC

Abstract base class for event handlers.

This class defines the fundamental interface for all event handlers within an optimization workflow. Concrete handler implementations, (e.g., tracking results, storing data, logging), must inherit from this base class.

Handlers may store state using dictionary-like access ([]), allowing them to accumulate information or make data available to other components in an optimization workflow.

Subclasses must implement the abstract handle_event method to define their specific event processing logic.

EventHandler instances are typically created using the create_event_handler function.

Event handlers are attached to a ComputeStep using its add_event_handler method. When the compute step emits an event, the handle_event method of each attached handler is invoked, allowing it to process the event.

event_types abstractmethod property

event_types: set[EventType]

Return the event types that are handled.

Returns:

Type Description
set[EventType]

A set of event types that are handled.

__init__

__init__() -> None

Initialize the EventHandler.

handle_event abstractmethod

handle_event(event: Event) -> None

Process an event.

This abstract method must be implemented by concrete EventHandler subclasses. It defines the event handler's core logic for reacting to Event objects emitted in the optimization workflow.

Implementations should inspect the event object (its event_type and data) and perform computations accordingly, such as storing results, logging information, or updating internal state.

Parameters:

Name Type Description Default
event Event

The event object.

required

__getitem__

__getitem__(key: str) -> Any

Retrieve a value from the event handler's internal state.

This method enables dictionary-like access (handler[key]) to the values stored within the event handler's internal state dictionary. This allows handlers to store and retrieve data accumulated during workflow execution.

Parameters:

Name Type Description Default
key str

The string key identifying the value to retrieve.

required

Returns:

Type Description
Any

The value associated with the specified key.

Raises:

Type Description
AttributeError

If the provided key does not exist in the event handler's stored values.

__setitem__

__setitem__(key: str, value: Any) -> None

Store or update a value in the event handler's internal state.

This method enables dictionary-like assignment (handler[key] = value) to store arbitrary data within the event handler's internal state dictionary. This allows event handlers to accumulate information or make data available to other components of the workflow.

The key must be a valid Python identifier.

Parameters:

Name Type Description Default
key str

The string key identifying the value to store (must be an identifier).

required
value Any

The value to associate with the key.

required

Raises:

Type Description
AttributeError

If the provided key is not a valid identifier.