A lightweight, type-safe Python state machine implementation using modern Python 3.13+ features.
Project description
State Machine v0.2.0
A lightweight, type-safe Python state machine implementation using modern Python 3.13+ features like PEP 695 type parameters.
Features
- Type Safety: Leverage Python's type system to ensure consistent states, events, and contexts.
- Decorator Support: Easily define transitions using the
@sm.transitiondecorator. - Auditing: Built-in support for recording transition history via
AuditedStateMachineandAuditContext. - Flexible Context: Pass a custom context object to transition actions to maintain state outside the machine.
Installation
This project uses uv for dependency management. To get started, ensure you have uv installed and run:
uv sync
You can install the package via pip:
pip install semantic-state-machine
Usage
Basic State Machine
Define your states and events as Enums and create a context class.
from enum import Enum
from semantic_state_machine import StateMachine
class State(Enum):
LOCKED = 1
UNLOCKED = 2
class Event(Enum):
PUSH = 1
COIN = 2
class TurnstileContext:
def __init__(self):
self.total_coins = 0
sm = StateMachine[State, Event, TurnstileContext]()
@sm.transition(State.LOCKED, Event.COIN, State.UNLOCKED)
def insert_coin(ctx: TurnstileContext) -> None:
ctx.total_coins += 1
print("Coin inserted. Turnstile unlocked.")
@sm.transition(State.UNLOCKED, Event.PUSH, State.LOCKED)
def push_turnstile(ctx: TurnstileContext) -> None:
print("Turnstile pushed. Turnstile locked.")
# Usage
ctx = TurnstileContext()
current_state = State.LOCKED
current_state = sm.handle_transition(ctx, current_state, Event.COIN)
# current_state is now State.UNLOCKED
Audited State Machine
Use AuditedStateMachine and AuditContext to automatically track the history of transitions.
from semantic_state_machine import AuditedStateMachine, AuditContext
class MyContext(AuditContext[State, Event]):
pass
sm = AuditedStateMachine[State, Event, MyContext]()
# Transitions are defined the same way
@sm.transition(State.LOCKED, Event.COIN, State.UNLOCKED)
def insert_coin(ctx: MyContext):
pass
ctx = MyContext()
sm.handle_transition(ctx, State.LOCKED, Event.COIN)
# The transition is automatically recorded
print(ctx.audit_trail()) # [(State.LOCKED, Event.COIN)]
# Example of a custom audit trail formatter (e.g., counting transitions)
transition_count = ctx.audit_trail(lambda trail: len(trail))
print(f"Total transitions: {transition_count}") # Total transitions: 1
Testing
The project includes a comprehensive suite of unit and integration tests.
Run All Tests
uv run pytest tests/
Run with Coverage
uv run pytest --cov=src tests/
The current implementation maintains 100% code coverage across the core logic and integration workflows.
Project Structure
src/semantic_state_machine/: Core implementation.tests/unit/: Focused unit tests for individual components.tests/integration/: End-to-end workflow simulations.
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 semantic_state_machine-0.2.0.tar.gz.
File metadata
- Download URL: semantic_state_machine-0.2.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5a942287295bac3a507909b382fbb199c941c7147b4b76229c66708db0b1838
|
|
| MD5 |
711fdc7e6f198487e83c38f777b46671
|
|
| BLAKE2b-256 |
9ce3d144789d8addb722877b644fe9b1eba4ae9430e91f2db8f81e7b194bdc68
|
Provenance
The following attestation bundles were made for semantic_state_machine-0.2.0.tar.gz:
Publisher:
publish.yml on TheTrueSCU/semantic-state-machine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
semantic_state_machine-0.2.0.tar.gz -
Subject digest:
d5a942287295bac3a507909b382fbb199c941c7147b4b76229c66708db0b1838 - Sigstore transparency entry: 1282878556
- Sigstore integration time:
-
Permalink:
TheTrueSCU/semantic-state-machine@abcb63edd3f191b8fba202606c8f1d4a5a954626 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/TheTrueSCU
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@abcb63edd3f191b8fba202606c8f1d4a5a954626 -
Trigger Event:
release
-
Statement type:
File details
Details for the file semantic_state_machine-0.2.0-py3-none-any.whl.
File metadata
- Download URL: semantic_state_machine-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03258396b161438a9a86bae6a5c10c14dafffb562c8c068cf6fca4edabca7fa6
|
|
| MD5 |
5a2941f29438c2c0cddfc31c7a74db31
|
|
| BLAKE2b-256 |
244616d9ca3b06ab3bb9a7f98a055c3686ccd8f0100bd94e726fb7f088bada2e
|
Provenance
The following attestation bundles were made for semantic_state_machine-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on TheTrueSCU/semantic-state-machine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
semantic_state_machine-0.2.0-py3-none-any.whl -
Subject digest:
03258396b161438a9a86bae6a5c10c14dafffb562c8c068cf6fca4edabca7fa6 - Sigstore transparency entry: 1282878579
- Sigstore integration time:
-
Permalink:
TheTrueSCU/semantic-state-machine@abcb63edd3f191b8fba202606c8f1d4a5a954626 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/TheTrueSCU
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@abcb63edd3f191b8fba202606c8f1d4a5a954626 -
Trigger Event:
release
-
Statement type: