Skip to main content

A data-first, type-safe finite state machine library for Python 3.14+

Project description

rebalanced-fsm

A small, data-first finite state machine for Python 3.14+. Typed, immutable, and fluent.

Install

pip install rebalanced-fsm

Basics

from dataclasses import dataclass
from rebalanced_fsm import Machine, State, Transition, Event

@dataclass
class Account:
    balance: float = 0

m = Machine[Account](
    initial="empty",
    context=Account(),
    states={
        "empty": State(on={"deposit": Transition("funded").then(
            lambda ctx, ev: Account(ctx.balance + ev.payload["amount"])
        )}),
        "funded": State(on={"withdraw": Transition("empty").when(
            lambda ctx, ev: ctx.balance >= ev.payload["amount"]
        ).then(
            lambda ctx, ev: Account(ctx.balance - ev.payload["amount"])
        )}, final=True),
    },
)

m.send(Event("deposit", {"amount": 100}))
print(m.state, m.context.balance)  # funded 100

Concepts

Type-safe context

Machine[Portfolio](...)  # mypy/pyright knows context is Portfolio

Guards

from rebalanced_fsm import where

has_cash = where(lambda ctx, ev: ctx.cash >= ev.payload["cost"])
many_shares = where(lambda ctx, ev: ctx.shares > 100)

# Compose with &, |, ~
Transition("buy").when(has_cash & ~many_shares)

Fluent transitions

Transition("armed") \
    .when(rsi_low) \
    .then(deploy_cash)

# Internal transitions skip enter/leave hooks
Transition("self").internal_only()

Final states

"done": State(final=True)  # accepts no events

Error handling

from rebalanced_fsm import UnhandledPolicy

m = Machine(..., on_unhandled=UnhandledPolicy.RAISE)
# m.send(Event("unknown"))  # UnhandledEventError

License

Apache 2.0 - see root 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

rebalanced_fsm-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

rebalanced_fsm-0.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rebalanced_fsm-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for rebalanced_fsm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8e04f013173bba38c3c4fd8f55dc3d054f25bb59ec2aaebfc14ea5923832d8e9
MD5 523acd405050bec034bd2a36ba2f2ed6
BLAKE2b-256 e6bbb47e4b4a335793c67588a2502df6ce5f8e89e5674c6635f604b9281d794d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rebalanced_fsm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for rebalanced_fsm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 439d3a2569729be940e80e65217571c3f525a990a7cb038d29119b606d7993cd
MD5 edab01757a775217f60a812f447918bb
BLAKE2b-256 bcdc5a9f937a23b51b16bf4a8a6051fa4a19b726c5d8f86a73f161d063e08b89

See more details on using hashes here.

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