A simple stupid finite state machine.
Project description
simplestate
Simplestate helps developers create and manage finite state machines in the simplest way possible—focusing solely on states and transitions, leaving out complex event systems.
Features
- Focus on states and transitions.
- Minimalistic and easy to use.
- Supports entering-state handlers for additional functionality.
Installation
To install simplestate, use poetry:
poetry add simplestate
Usage Example
Here's a basic example of using simplestate:
from simplestate import StateMachine
m = StateMachine("loading") # Define the machine with an initial state
m["loading"] + "error" >> "failed" # Transition from loading to failed on "error"
m["loading"] + "ok" >> "success" # Transition from loading to success on "ok"
m["?"] + "back" >> "loading" # Any state transitions to loading on "back"
m.add_callbacks({ # Add handlers for entering states
"loading": lambda previous: print(f"{previous} >> loading"),
"failed": lambda previous, error: print(f"{previous} >> failed: {error}"),
"success": lambda previous: print(f"{previous} >> success"),
})
m.start() # Start the machine, adjust inital state by this m.start(at_state="ok")
assert m.current == "loading"
m.handle("error", error="Something went wrong") # Trigger transitions
assert m.current == "failed"
m.handle("back")
assert m.current == "loading"
Why Simplestate?
The goal of simplestate is to keep finite state machines simple and focused. By eliminating complex event systems, you can easily manage states and transitions while keeping the code testable and maintainable.
Event Handling on State Entry
Simplestate allows event handling when entering a state using .add_callbacks()
. These callbacks take previous
and an optional **input_context
.
FAQ
Q: How can I handle state exit events?
A: You can use the entering handler for the next state to handle actions when leaving the previous state.
def on_state_x(previous, **input_context):
if previous == "a":
do_this()
elif previous == "b":
do_that()
Q: Can I represent the state machine as a class?
A: Yes! Think of simplestate as a state manager. You can compose it within your own class for handling side effects:
class TrafficLight:
def __init__(self):
brain = StateMachine("red")
brain["red"] + "next" >> "yellow_green"
brain["yellow_green"] + "next" >> "green"
brain["green"] + "next" >> "yellow_red"
brain["yellow_red"] + "next" >> "red"
brain.start("red")
self.machine = brain
def display(self) -> str:
return self.machine.current
def next(self) -> None:
self.machine.handle("next")
Tests
To run the tests:
poetry install --with test
poetry run pytest
Contribution
Contributions are welcome! Feel free to open an issue or submit a pull request.
- Install devcontainer
- Open this project in dev container
- Run
poetry shell
- Run
poetry install --with test
- Run
poetry run pytest
License
This project is licensed under a free, open-source license.
Project details
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
File details
Details for the file simplestate-0.1.1.tar.gz
.
File metadata
- Download URL: simplestate-0.1.1.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.6.26-linuxkit
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2a9187baa9dddf34603a11adeee7b1a25b2b4d415f795accf7092f0d43b8953 |
|
MD5 | 4d9270e136c083ae23d5d2855f079890 |
|
BLAKE2b-256 | 224515ff832ff1d43f2ebc0de037e0c10a738fe1c30ec83bad62dcf930b774fc |
File details
Details for the file simplestate-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: simplestate-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.6.26-linuxkit
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f79fda137111c3a04c923672aa5e9fbadc2dbd60c5b669b7fd2715d50540958 |
|
MD5 | fad743a0717c9fda9b44af30069b4131 |
|
BLAKE2b-256 | ae082852d48ed5a0375198684d201858fd9621ac678b73e9000765b98a94acef |