Skip to content

runnable_mixin

T = TypeVar('T', bound='RunnableMixin') module-attribute

RunnableMixin

Bases: ABC, Generic[T]

Mixin for classes that can be "run".

Implementing classes should pass themselves as the generic type to ensure that the correct type is used in the callbacks.

Attributes:

Name Type Description
on_before_run Optional[Callable[[T], None]]

Optional callback that is called at the very beginning of the run method.

on_after_run Optional[Callable[[T], None]]

Optional callback that is called at the very end of the run method.

Source code in griptape/mixins/runnable_mixin.py
@define()
class RunnableMixin(ABC, Generic[T]):
    """Mixin for classes that can be "run".

    Implementing classes should pass themselves as the generic type to ensure that the correct type is used in the callbacks.

    Attributes:
        on_before_run: Optional callback that is called at the very beginning of the `run` method.
        on_after_run: Optional callback that is called at the very end of the `run` method.
    """

    on_before_run: Optional[Callable[[T], None]] = field(kw_only=True, default=None)
    on_after_run: Optional[Callable[[T], None]] = field(kw_only=True, default=None)

    def before_run(self, *args, **kwargs) -> Any:
        if self.on_before_run is not None:
            self.on_before_run(cast(T, self))

    @abstractmethod
    def run(self, *args, **kwargs) -> Any: ...

    def after_run(self, *args, **kwargs) -> Any:
        if self.on_after_run is not None:
            self.on_after_run(cast(T, self))

on_after_run: Optional[Callable[[T], None]] = field(kw_only=True, default=None) class-attribute instance-attribute

on_before_run: Optional[Callable[[T], None]] = field(kw_only=True, default=None) class-attribute instance-attribute

after_run(*args, **kwargs)

Source code in griptape/mixins/runnable_mixin.py
def after_run(self, *args, **kwargs) -> Any:
    if self.on_after_run is not None:
        self.on_after_run(cast(T, self))

before_run(*args, **kwargs)

Source code in griptape/mixins/runnable_mixin.py
def before_run(self, *args, **kwargs) -> Any:
    if self.on_before_run is not None:
        self.on_before_run(cast(T, self))

run(*args, **kwargs) abstractmethod

Source code in griptape/mixins/runnable_mixin.py
@abstractmethod
def run(self, *args, **kwargs) -> Any: ...