A simple and flexible Finite State Machine implementation in Python
Project description
Common-FSM
A simple and flexible Finite State Machine implementation in Python.
Features
- Simple and intuitive API
- Type-safe with generics support
- Optional state timeouts
- Enter/exit hooks for states
- Verbose mode for debugging
- Fully tested
Installation
pip install common-fsm
Quick Start
from enum import Enum
from common_fsm import FSM, State, Event, Transition
# Define your states and events
class States(Enum):
OFF = "off"
ON = "on"
class Events(Enum):
POWER_ON = "power_on"
POWER_OFF = "power_off"
# Create states with handlers
off_state = State()
off_state.add_handler(
Events.POWER_ON,
lambda event: Transition(States.ON)
)
on_state = State()
on_state.add_handler(
Events.POWER_OFF,
lambda event: Transition(States.OFF)
)
# Create FSM
fsm = FSM(
States.OFF, # Initial state
{
States.OFF: off_state,
States.ON: on_state
}
)
# Use the FSM
fsm.handle_event(Event(Events.POWER_ON)) # Transitions to ON
fsm.handle_event(Event(Events.POWER_OFF)) # Transitions to OFF
Advanced Features
State Timeouts
States can have automatic timeouts:
# State with 5 second timeout
on_state = State(timeout=5.0)
on_state.add_handler(
Events.TIMEOUT,
lambda event: Transition(States.OFF)
)
fsm = FSM(
States.OFF,
{
States.OFF: off_state,
States.ON: on_state
},
timeout_event=Events.TIMEOUT
)
State Hooks
Add hooks for state transitions:
on_state.add_enter_hook(lambda s: print("Entering ON state"))
on_state.add_exit_hook(lambda s: print("Exiting ON state"))
Examples
See the examples/ directory for more detailed examples, including:
- Basic state machine
- Speaker with volume control
- Traffic light controller
Development
Clone the repository:
git clone https://github.com/commonai/python-fsm.git
cd python-fsm
Install development dependencies:
pip install -e ".[dev]"
Run tests:
pytest tests/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file common_fsm-0.1.3.tar.gz.
File metadata
- Download URL: common_fsm-0.1.3.tar.gz
- Upload date:
- Size: 4.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
108628b6c3885e7538bfd12a6096b966b15ce6feedaccbeb1788f82f551c4711
|
|
| MD5 |
045393df39115092093579fd0a54c50d
|
|
| BLAKE2b-256 |
bbffd696f0aaf1eb5c9c031ded3af1a478f4a674f9b97db20b72a510fa617860
|
File details
Details for the file common_fsm-0.1.3-py3-none-any.whl.
File metadata
- Download URL: common_fsm-0.1.3-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edfd2772c12d651c034ebef79d8f327131325279e6b9a9ce9c3f8f7e957dc0ab
|
|
| MD5 |
d9ad65ea6f5deacd6f7373f1e94a3bf2
|
|
| BLAKE2b-256 |
a171700c3151c4672d3a617694174c8f0a0451296aeabeae6de304dd4388059c
|