A lightweight, object-oriented finite state machine implementation in Python with many extensions.
Transitions is a Python library that implements finite state machines (FSMs) in an object-oriented and lightweight manner. It allows developers to model application states and transitions between them, handling complex workflows with callbacks, conditions, and custom logic. The library solves the problem of managing stateful behavior cleanly and maintainably in Python projects.
Python developers building applications with stateful logic, such as game engines, workflow systems, IoT device controllers, or any system requiring clear state transitions and lifecycle management.
Developers choose Transitions for its intuitive API, extensive feature set (including hierarchical states and visualization), and flexibility—it supports multiple initialization patterns and integrates easily into existing codebases without heavy dependencies.
A lightweight, object-oriented finite state machine implementation in Python with many extensions
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
States and transitions are first-class objects, making code self-documenting and maintainable, as demonstrated in the NarcolepticSuperhero example with clear state definitions and transitions.
Supports before, after, enter, and exit callbacks for fine-grained control over state changes, detailed in sections like 'Callbacks' and 'Transition callbacks' with examples like update_journal and change_into_super_secret_costume.
Enables nesting of states for complex workflows via the HSM extension, facilitating reuse and organization, as mentioned in the 'Hierarchical State Machine' extension section.
Can generate diagrams for state machines to aid debugging and documentation, shown in the 'Diagrams' extension with examples like the batman diagram in the Quickstart.
Offers multiple ways to bind machines to models, including inheritance or standalone instances, as detailed in 'Alternative initialization patterns' with examples like Machine(model=lump) or class Matter(Machine).
Advanced features like diagrams, async, or threading require separate extensions, adding setup steps and potential dependencies beyond the core library, as noted in the 'Extensions' section.
Default configuration raises exceptions on invalid transitions, necessitating explicit error handling or global settings like ignore_invalid_triggers, which can complicate code for robust applications.
With version 0.9.4, the library is still in development, meaning future updates might introduce breaking changes, as indicated by the version badge and lack of a 1.0 release.
While pickling is supported for serialization, there is no native integration with databases or external storage, requiring custom implementation for persistent state management.