A lightweight, object-oriented Python state machine implementation with many extensions
npx @tessl/cli install tessl/pypi-transitions@0.9.0A lightweight, object-oriented Python state machine implementation with many extensions. Transitions provides a comprehensive and flexible API for defining states, transitions, and events with support for callbacks, conditional transitions, queued transitions, and automatic transitions. The library includes advanced features such as hierarchical state machines, diagram generation, async support, and integration with various Python frameworks.
pip install transitionsfrom transitions import Machine, State, Transition, Event, EventData, MachineErrorFor specific functionality:
# Core state machine
from transitions import Machine
# State machine with extensions
from transitions.extensions import GraphMachine, HierarchicalMachine, LockedMachine
from transitions.extensions import AsyncMachine, MachineFactory
# Individual components if needed
from transitions import State, Transition, Eventfrom transitions import Machine
# Define states and transitions
states = ['sleeping', 'awake', 'eating', 'playing']
transitions = [
{'trigger': 'wake_up', 'source': 'sleeping', 'dest': 'awake'},
{'trigger': 'eat', 'source': 'awake', 'dest': 'eating'},
{'trigger': 'play', 'source': 'eating', 'dest': 'playing'},
{'trigger': 'sleep', 'source': ['awake', 'eating', 'playing'], 'dest': 'sleeping'}
]
# Create a simple model
class Pet:
def __init__(self):
self.name = "Fluffy"
# Initialize the machine
pet = Pet()
machine = Machine(model=pet, states=states, transitions=transitions, initial='sleeping')
# Use the state machine
print(pet.state) # 'sleeping'
pet.wake_up()
print(pet.state) # 'awake'
pet.eat()
print(pet.state) # 'eating'Transitions follows a modular architecture with core components and extensions:
The design enables incremental adoption - start with basic Machine functionality and add extensions as needed for complex requirements.
Fundamental state machine functionality including state management, transitions, callbacks, and event handling. Provides the foundation for all state machine operations.
class Machine:
def __init__(self, model='self', states=None, initial='initial', transitions=None, **kwargs): ...
def add_state(self, states, **kwargs): ...
def add_transition(self, trigger, source, dest, **kwargs): ...
def get_state(self, state): ...
def is_state(self, state, model): ...Enhanced state machines with additional capabilities including hierarchical states, thread safety, asynchronous operations, and diagram generation.
class GraphMachine(Machine): ...
class HierarchicalMachine(Machine): ...
class LockedMachine(Machine): ...
class AsyncMachine(Machine): ...
class MachineFactory:
def get_predefined(self, graph=False, nested=False, locked=False, asyncio=False): ...Individual components for states, transitions, events, and conditions that can be used to build custom state machine configurations.
class State:
def __init__(self, name, on_enter=None, on_exit=None, **kwargs): ...
def enter(self, event_data): ...
def exit(self, event_data): ...
class Transition:
def __init__(self, source, dest, conditions=None, **kwargs): ...
def execute(self, event_data): ...