or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

components.mdcore.mdextensions.mdindex.md
tile.json

tessl/pypi-transitions

A lightweight, object-oriented Python state machine implementation with many extensions

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/transitions@0.9.x

To install, run

npx @tessl/cli install tessl/pypi-transitions@0.9.0

index.mddocs/

Transitions

A 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.

Package Information

  • Package Name: transitions
  • Language: Python
  • Installation: pip install transitions

Core Imports

from transitions import Machine, State, Transition, Event, EventData, MachineError

For 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, Event

Basic Usage

from 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'

Architecture

Transitions follows a modular architecture with core components and extensions:

  • Core Components: Machine, State, Transition, Event, EventData provide the fundamental state machine functionality
  • Extensions: Modular enhancements for specialized needs (hierarchical states, threading, async operations, diagram generation)
  • Factory Pattern: MachineFactory provides convenient access to pre-configured machine combinations

The design enables incremental adoption - start with basic Machine functionality and add extensions as needed for complex requirements.

Capabilities

Core State Machine

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): ...

Core State Machine

Extensions and Specialized Machines

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): ...

Extensions

State and Transition Components

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): ...

Components