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
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
rebalanced_fsm-0.1.1.tar.gz
(8.2 kB
view details)
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 rebalanced_fsm-0.1.1.tar.gz.
File metadata
- Download URL: rebalanced_fsm-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6c61033508dc2b4f0339942e5290ad52a3245e06fd9ab4f42d9f20c9447dab3
|
|
| MD5 |
968fbc82cd806a0cb758215bd3d459db
|
|
| BLAKE2b-256 |
bdbaff1448a200de18dbfa0561c2b4a8d2803fb10cc8b37b24f56d263ce846a3
|
File details
Details for the file rebalanced_fsm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: rebalanced_fsm-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0213079bbbf7066851c030c4623219cb4f6a93529b583aaa971a6223344c69e
|
|
| MD5 |
bb4817337ba666cfdd4bc663a763975b
|
|
| BLAKE2b-256 |
5b9a52b755742f23c81af186878c47c9c976dcead083041b1a199b3d874b8a58
|