Skip to main content

A safe, pure-Python finite state machine with colored terminal logging

Project description

StateLogic

StateLogic doesn’t just manage state. It protects it.

A pure, safe, and elegant finite state machine for Python — with colored terminal logging.

PyPI Tests License

StateLogic is a lightweight, dependency-free finite state machine library that enforces correctness by design.

It was born from frustration with existing FSM libraries that allow invalid states, silent failures, or require complex boilerplate. StateLogic fixes all of that — and adds beautiful colored logging as a bonus.

Design Philosophy — Why StateLogic Is Different (and Better)

"A state machine must never lie about its state."

StateLogic is built on four unbreakable principles:

1. The current state can only be changed via a valid, registered transition

Direct assignment like fsm.state("HACKED") is silently ignored if the state is not part of a defined transition.
This prevents bugs, race conditions, and security issues in critical systems.

2. You cannot set an initial state before defining transitions

fsm = StateLogic()
fsm.state("SOLID")        # → Does nothing (no transitions defined yet)
fsm.state()               # → None

Only after you define at least one transition are states considered "valid":

fsm.transition("melts", "SOLID", "LIQUID")
fsm.state("SOLID")        # → Now allowed

3. Invalid transitions are impossible — not just undetected

If you try to go from GASSOLID without defining that path, nothing happens.
No exception (unless you want one), no silent success — just correctness.

4. Hooks are first-class: before, on, and after

fsm.before("melts", lambda: input("Melt? ") == "Y")
fsm.on("melts", lambda: print("Melting started..."))
fsm.after("melts", lambda: print("Now it's water!"))

This isn't just convenience — it's enforced lifecycle safety.

These rules make StateLogic ideal for:

  • Embedded systems
  • Game logic
  • Workflow engines
  • Protocol implementations
  • Any domain where state corruption is unacceptable

Installation

pip install statelogic

Quick Example

from statelogic import StateLogic

s = StateLogic()
s.author("Wilgat").appName("WaterFSM").majorVersion(1)

# Define valid physics
s.transition("freeze",     "LIQUID", "SOLID")
s.transition("melts",      "SOLID",  "LIQUID")
s.transition("evaporate",  "LIQUID", "GAS")
s.transition("condense",   "GAS",    "LIQUID")

# Try to cheat physics
s.state("PLASMA")          # → Ignored. Still None.
print(s.state())           # → None

# Now play by the rules
s.transition("sublimate", "SOLID", "GAS")  # Define the edge case
s.state("SOLID")           # → Now allowed
s.sublimate()
print(s.state())           # → GAS

s.infoMsg("Sublimation complete!", "SCIENCE")

Output (with colors):

2025-12-02 10:30:45.123456 WaterFSM(v1.0.0)  [SCIENCE]: 
  Sublimation complete!

Features

  • Zero dependencies
  • Python 2.7 and 3.6+ compatible
  • Beautiful colored terminal logging (infoMsg, safeMsg, criticalMsg)
  • Full hook system: before, on, after
  • Dynamic attributes via Attr class
  • Signal handling (Ctrl+C → graceful exit)
  • Shell & environment utilities
  • 100% test coverage

Hook Examples

before — Guard transitions

def confirm():
    return input("Proceed? (y/n): ").lower() == "y"

s.before("launch", confirm)
s.launch()   # Only runs if user says yes

on — React immediately

s.on("error", lambda: s.criticalMsg("System failure!", "ALERT"))

after — Cleanup or notify

s.after("shutdown", lambda: s.safeMsg("System offline.", "BYE"))

Project Links

License

MIT © Wilgat


StateLogic doesn’t just manage state. It protects it.

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

statelogic-1.2.1.tar.gz (15.8 kB view details)

Uploaded Source

Built Distributions

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

statelogic-1.2.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

statelogic-1.2.1-py2-none-any.whl (13.9 kB view details)

Uploaded Python 2

File details

Details for the file statelogic-1.2.1.tar.gz.

File metadata

  • Download URL: statelogic-1.2.1.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for statelogic-1.2.1.tar.gz
Algorithm Hash digest
SHA256 199ad5864f003f919ee02ec0ddb3f8e249264481632ddfbe476988294976c040
MD5 ee4e32579ca0e52ef130c1b346c818c8
BLAKE2b-256 1443bbee7692dd9a64a2046c6673057ae0db7316abdb30515a0bd840d371d335

See more details on using hashes here.

File details

Details for the file statelogic-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: statelogic-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for statelogic-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9ba612d062cc6727697d95d854019e425399c9c80779ef912326e3d7d1d867d
MD5 185ac7e1d72a2cb89f69165d65c6742f
BLAKE2b-256 d2db82b925c1bce4863b840de14269c1586c6182817caa9810bd07b69ffaa72b

See more details on using hashes here.

File details

Details for the file statelogic-1.2.1-py2-none-any.whl.

File metadata

  • Download URL: statelogic-1.2.1-py2-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/41.2.0 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.18

File hashes

Hashes for statelogic-1.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 d1a200bdc6438b9820f88b098ecd6bd11ae8043834a491d889f994b1b9838176
MD5 b88a5d77b967ea6778d576482f73ca0a
BLAKE2b-256 4697287aa1600d8ab0a8ac328b074670b49c339cec5f0e9d9a3dfaf746fa44fe

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