event_statemachine package

Submodules

event_statemachine.context module

class event_statemachine.context.Context[source]

Bases: object

from_dict(data)[source]
to_dict()[source]

event_statemachine.handler module

class event_statemachine.handler.HandlerMeta(name, bases, dct)[source]

Bases: type

event_statemachine.sm module

Main module.

class event_statemachine.sm.StateMachine(initial_state: str | None = 'Initial')[source]

Bases: object

Base class for a state machine.

Args:

initial_state (str, optional): Initial state of the state machine. Defaults to “Initial”.

get_context() dict[source]

Obtain the context of the state machine.

Returns:

dict: context of the state machine

on_entries = {}
on_entry() None[source]

Hook to execute code when an event is received in the state machine, doesn’t matter if the event is handled or not.

on_exit() None[source]

Hook to execute code after an event is received in the state machine, doesn’t matter if the event was handled or not.

on_exits = {}
on_get_context() dict[source]

Hook to perform custom actions when the context is obtained.

Returns:

dict: new values for the context

on_return() Any[source]

Hook to return a value after executing the state machine.

Returns:

Any: custom value to return

on_set_context(context: dict) dict[source]

Hook to perform custom actions when the context is set.

Args:

context (dict): the received context

Returns:

dict: a modified context

run_state(event: Any | None = None) Any[source]

Method to run the state machine.

Args:

event (Optional[Any], optional): The data of the event. Defaults to None.

Returns:

Any: The value returned by the on_return hook.

set_context(context: dict) None[source]

Set the context of the state machine.

Args:

context (dict): context of the state machine obtained from get_context()

transitions = {}
event_statemachine.sm.event_condition(condition: Callable) Callable[source]

Decorator to define a condition for an event. The condition is a funtion that receives self from the state machine. It’s use to control the flow of the state machine, and validate the event that is received.

It is used in the following way:

@transition("StateFrom -> StateTo")
@event_condition(lambda self: self.evt.get("condition") == "expected_value")
def handler_function(self):
    pass
Args:

condition (Callable): a function that receives self from the state machine and returns a boolean.

event_statemachine.sm.on_state_entry(state: str) Callable[source]

Decorator to define a function that is executed when an event is received in a specific state.

It is used in the following way:

@on_state_entry("State")
def handler_function(self):
    # allways executed when an event is received in the state "State"
    pass
Args:

state (str): state name.

event_statemachine.sm.on_state_exit(state: str) Callable[source]

Decorator to define a function that is executed when an event is received after executing some state.

It is used in the following way:

@on_state_exit("State")
def handler_function(self):
    # allways executed after handling an event in the state "State"
    pass
Args:

state (str): state name.

event_statemachine.sm.transition(transition_name: str) Callable[source]

Decorator to define a transition.

It is used in the following way:

@transition("StateFrom -> StateTo")
def handler_function(self):
    pass

The states are defined as strings separated by a -> symbol.

Args:

transition_name (str): Transition from -> to.

Module contents

It is a simple state machine library, based on events. It is easy to use, extend and scale.