Skip to main content

a strictly typed, flexible library for building and running state machines in Python

Project description

Statomata

Latest Version Python Supported versions MyPy Strict Test Coverage Downloads GitHub stars

Statomata is a strictly typed, flexible library for building and running finite state machines (FSMs) and automata in Python. It provides core automata implementations out of the box and lets you define custom states and state management logic.

Features

  • 🧠 State Interface & Context: Follow the state machine OOP pattern with clear, isolated state definitions.
  • ⏸️ Defer & Recall: States can defer incoming messages and recall them later, enabling complex multi-step workflows.
  • Built-in Automata: feel free to use one of the predefined automata classes from SDK (sync / async; unary / iterable)
  • 🏗 Custom Automata Support: Build your own automata with custom state management logic. You can choose declarative style or built custom automata using provided interfaces.
  • Strict Typing: Designed for type safety and clarity with full type hints.

Installation

pip install statomata

Quick Start

Use high-level declarative style:

from statomata.declarative import DeclarativeStateMachine, State


class OpenCloseExample(DeclarativeStateMachine):
    closed = State(initial=True)
    opened = State()

    @closed.to(opened)
    def open(self) -> str:
        return "Opened"

    @opened.to(closed)
    def close(self) -> str:
        return "Closed"

Run the state machine:

from contextlib import suppress
from statomata.exception import InvalidStateError

sm = OpenCloseExample()

print(sm.open())  # Output: Opened
print(sm.close())  # Output: Closed

with suppress(InvalidStateError):
    sm.close()

Or you can use low-level style:

from statomata.abc import State, Context
from statomata.exception import InvalidStateError


class OpenState(State[str, str]):
    def handle(self, income: str, context: Context[State[str, str]]) -> str:
        if income != "close":
            raise InvalidStateError(self, message="already opened")

        context.set_state(ClosedState())
        return "Closed"


class ClosedState(State[str, str]):
    def handle(self, income: str, context: Context[State[str, str]]) -> str:
        if income != "open":
            raise InvalidStateError(self, message="already closed")

        context.set_state(OpenState())
        return "Opened"

Run the state machine:

from contextlib import suppress

from statomata.exception import InvalidStateError
from statomata.sdk import create_unary_sm

sm = create_unary_sm(ClosedState())

print(sm.run("open"))  # Output: Opened
print(sm.run("close"))  # Output: Closed

with suppress(InvalidStateError):
    sm.run("close")

Examples

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

statomata-0.4.1.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

statomata-0.4.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file statomata-0.4.1.tar.gz.

File metadata

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

File hashes

Hashes for statomata-0.4.1.tar.gz
Algorithm Hash digest
SHA256 8df1e0747b5f66a14210c5971cbe459692ea466a8fa7a0f4a9b8915a105cc040
MD5 20f132c3a0b85d148e9e06c2d5830941
BLAKE2b-256 dd3103fc59f5bf710fbf66bb1cfa98ede1aea4da0046d126fd00852ab359a681

See more details on using hashes here.

Provenance

The following attestation bundles were made for statomata-0.4.1.tar.gz:

Publisher: publish.yaml on zerlok/statomata

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

File details

Details for the file statomata-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: statomata-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for statomata-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ccdb7755c52300a68b12fd80e6aee6b7ec5784e1c68cef412794eb6f71d5dac7
MD5 e97ccf8b72a08e16acb3fb75bc54b8d2
BLAKE2b-256 52acdced64bd8abff08a56be279e2947fa0894f9cee97d2e98ec41e2cd7294d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for statomata-0.4.1-py3-none-any.whl:

Publisher: publish.yaml on zerlok/statomata

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