Skip to main content

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:

m[state] + action >> next_state

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"

# Start the machine
m.start()  # adjust inital state by this m.start(at_state="ok")
assert m.current == "loading"

# Trigger transitions
m.handle("ok")
assert m.current == "success"

m.handle("back")
assert m.current == "loading"

m.handle("back")
assert m.current == "loading"

# Trigger transitions with context
m.handle("error", error="Something went wrong")
assert m.current == "failed"

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.

# Add handlers for entering states
m.add_callbacks({
    "loading": lambda previous: print(f"{previous} >> loading"),
    "failed": lambda previous, error: print(f"{previous} >> failed: {error}"),
    "success": lambda previous: print(f"{previous} >> success"),
})

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()

        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.

  1. Install devcontainer
  2. Open this project in dev container
  3. Run poetry shell
  4. Run poetry install --with test
  5. 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

simplestate-0.1.4.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

simplestate-0.1.4-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file simplestate-0.1.4.tar.gz.

File metadata

  • Download URL: simplestate-0.1.4.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for simplestate-0.1.4.tar.gz
Algorithm Hash digest
SHA256 825f052f5c7cc698e39a1bca2c5da1ed620166000a7512312146f0ca33b130d0
MD5 7575c721d4d0d6fe0e7cce5769c57273
BLAKE2b-256 0fde82acda2eaa772814db079e4044310511782be4cb0f1d8d5b003a2dc9a7c1

See more details on using hashes here.

File details

Details for the file simplestate-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: simplestate-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for simplestate-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dd31c495970136751530ccffdf6684b9855d273d7f405b504560834537964971
MD5 3388a080f8cac9536b4c42c8569104f3
BLAKE2b-256 18317ef759b8136ba14cccfa0e50f96b1d96c0796ca3d231d81e0f8ce6e82c06

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page