State of Things¶
state_of_things.state_of_things¶
Base classes used to implement complex state machines. Each Thing
starts in an initial State, and then updates to the thing’s state
execute logic and determine whether or not to change to another state.
See examples directory for ideas on usage.
Author(s): Aaron Silinskas
- class state_of_things.state_of_things.State¶
Represents a state that a
Thingcan enter and exit. The state can transition aThinginto another state when it is updated.- enter(thing: Thing)¶
Called when a
Thingenters this state. Typically used for one-time setup where state-specific context is added to the Thing.
- exit(thing: Thing)¶
Called when a
Thingexists this state. Typically used to clean up resources initialized when the state is entered.
- property name¶
The state’s name, defaulting to the class name.
- update(thing: Thing) State¶
Called periodically while a
Thingis in this state. This function determines whether theThingshould remain in this state or change to another state.Often
Thing.time_elapsedandThing.time_activeare referenced when aThingshould transition to another state after a given amount of time.
- class state_of_things.state_of_things.Thing(initial_state: State, name: str = None)¶
Represents an object that can only be in one
Stateat a time. It holds all global and state-specific context needed byStateimplementations to update and transition between states.Constructor that stores the initial
Statebut does not change to it untilupdateis called.- Parameters:
- property name: str¶
Name of this thing, defaulting to class name but can be manually set via the constructor.
- Returns:
the name of this thing.
- Return type:
- property observers: Observers¶
The observers that will be notified by this
Thing. For instance, all observers that implementThingObserverwill be notified when thisThingchangesState.Things can have custom observers that it’s
Stateimplementations can notify as needed. For instance, a buttonThingmay have an observer that is notified when the button is pressed or released.
- property time_active: float¶
The total amount of time that this thing has been in the current
State.
- property time_elapsed: float¶
The amount of time that has elapsed since this thing’s last update call.
- Returns:
the amount of elapsed time, in seconds.
- Return type:
- update()¶
Updates
time_elapsedandtime_activeof this thing, and then updates theStateby callingState.updateofcurrent_state. IfState.updatereturns a differentStatethan the current one, then this thing will transition to the returnedState.
- class state_of_things.state_of_things.ThingObserver¶
Implement the
state_changedfunction of this class to receive notifications when aThingchanges state. AllThingobservers should inherited from this class.Logging state changes to stdout is common for debugging purposes.