Skip to content

factory

get_object_state_instance(state_class, obj, params=None)

Create an BaseObjectState child class instance for a given object & state.

The parameters passed in as a dictionary through params are passed as kwargs to the object state class constructor.

Parameters:

Name Type Description Default
state_class BaseObjectState

The state name from the state name dictionary.

required
obj StatefulObject

The object for which the state is being constructed.

required
params dict

Dictionary of {param: value} corresponding to the state's params.

None

Returns:

Name Type Description
BaseObjectState

The constructed state object

Source code in object_states/factory.py
def get_object_state_instance(state_class, obj, params=None):
    """
    Create an BaseObjectState child class instance for a given object & state.

    The parameters passed in as a dictionary through params are passed as
    kwargs to the object state class constructor.

    Args:
        state_class (BaseObjectState): The state name from the state name dictionary.
        obj (StatefulObject): The object for which the state is being constructed.
        params (dict): Dictionary of {param: value} corresponding to the state's params.

    Returns:
        BaseObjectState: The constructed state object
    """
    if not issubclass(state_class, BaseObjectState):
        assert False, "unknown state class: {}".format(state_class)

    if params is None:
        params = {}

    return state_class(obj, **params)

get_state_dependency_graph()

Returns:

Type Description

nx.DiGraph: State dependency graph of supported object states

Source code in object_states/factory.py
def get_state_dependency_graph():
    """
    Returns:
        nx.DiGraph: State dependency graph of supported object states
    """
    dependencies = {state: state.get_dependencies() + state.get_optional_dependencies() for state in get_all_states()}
    return nx.DiGraph(dependencies)

get_states_by_dependency_order()

Returns:

Name Type Description
list

all states in topological order of dependency

Source code in object_states/factory.py
def get_states_by_dependency_order():
    """
    Returns:
        list: all states in topological order of dependency
    """
    return list(reversed(list(nx.algorithms.topological_sort(get_state_dependency_graph()))))