Skip to main content

A lightweight, type-safe Python state machine implementation using modern Python 3.13+ features.

Project description

State Machine

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.transition decorator.
  • Auditing: Built-in support for recording transition history via AuditedStateMachine and AuditContext.
  • 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):
    ctx.total_coins += 1
    print("Coin inserted. Turnstile unlocked.")

@sm.transition(State.UNLOCKED, Event.PUSH, State.LOCKED)
def push_turnstile(ctx: TurnstileContext):
    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)  # [(State.LOCKED, Event.COIN)]

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semantic_state_machine-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

semantic_state_machine-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file semantic_state_machine-0.1.0.tar.gz.

File metadata

  • Download URL: semantic_state_machine-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for semantic_state_machine-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2081f613553ce64b0c4114a0ef46eff33b86deec8a3189f94f95e63baff50d49
MD5 d30c33b68da9019a68b68c6e414fb948
BLAKE2b-256 030ae94452cd6619b926614c6deb32cc59ef9daeb5f21dd6fe36435d69410f42

See more details on using hashes here.

Provenance

The following attestation bundles were made for semantic_state_machine-0.1.0.tar.gz:

Publisher: publish.yml on TheTrueSCU/semantic-state-machine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file semantic_state_machine-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for semantic_state_machine-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d39ba3b9cdd2fbbdfa07c21c7fe5a9e0e7ecd5c6e2d640c61e7f136b0817fd0b
MD5 0e74991abfde93228a8c6e3bf4da3b9a
BLAKE2b-256 a10eae96668cc7b17a1a619c3dc7a84fb9b782bd8a16a4610bf07c6de3101add

See more details on using hashes here.

Provenance

The following attestation bundles were made for semantic_state_machine-0.1.0-py3-none-any.whl:

Publisher: publish.yml on TheTrueSCU/semantic-state-machine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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