Skip to main content

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

Metadata

Release files for rebalanced-fsm 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rebalanced-fsm 0.1.1
File Size Uploaded Provenance Actions
rebalanced_fsm-0.1.1.tar.gz 8.2 kB Without provenance Details for rebalanced_fsm-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rebalanced-fsm 0.1.1
File Interpreter ABI Platform Actions
rebalanced_fsm-0.1.1-py3-none-any.whl Python 3 none any Details for rebalanced_fsm-0.1.1-py3-none-any.whl

Total release size: 18.2 kB

Release files / rebalanced_fsm-0.1.1.tar.gz

Download URL rebalanced_fsm-0.1.1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a6c61033508dc2b4f0339942e5290ad52a3245e06fd9ab4f42d9f20c9447dab3
BLAKE2b-256 checksum
How to use checksums
bdbaff1448a200de18dbfa0561c2b4a8d2803fb10cc8b37b24f56d263ce846a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / rebalanced_fsm-0.1.1-py3-none-any.whl

Download URL rebalanced_fsm-0.1.1-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0213079bbbf7066851c030c4623219cb4f6a93529b583aaa971a6223344c69e
BLAKE2b-256 checksum
How to use checksums
5b9a52b755742f23c81af186878c47c9c976dcead083041b1a199b3d874b8a58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for rebalanced-fsm 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page