Skip to main content

TinyFSM

A tiny and minimal finite state machine for Python.

About

TinyFSM is a fast and minimal finite state machine engine for Python. It runs the finite state defined as list of traversals, where each traversal is given by current and next state name and a traversal function that is evaluated on currently processed event. Once traversal function returns true, the current state is changed to the next state according to matched traversal.

This library can be used as a backbone for creating larger scale FSM-based tokenizers and parsers.

Installation

$ pip install tinyfsm

Quickstart

Here's a simple text tokenizer written using TinyFSM library that splits text composed of alphanumeric characters into groups of words and numbers and returns list of (token_name, token_data) tuples:

from tinyfsm.api import Traversal, StateMachineRunner, InputRejectedError

definition = [
    Traversal[str]("initial", "word", str.isalpha),
    Traversal[str]("initial", "number", str.isdigit),
    Traversal[str]("initial", "final", lambda input: input == ""),
    Traversal[str]("word", "word", str.isalpha),
    Traversal[str]("word", "number", str.isdigit),
    Traversal[str]("word", "final", lambda input: input == ""),
    Traversal[str]("number", "number", str.isdigit),
    Traversal[str]("number", "word", str.isalpha),
    Traversal[str]("number", "final", lambda input: input == ""),
]

class Listener:
    def __init__(self, output: list[tuple[str, str]]):
        self._output = output
        self._buffer = ""

    def on_state_change(self, input: str, prev_state: str, current_state: str):
        if prev_state != current_state:
            if prev_state == "word":
                self._output.append(("WORD", self._buffer))
            if prev_state == "number":
                self._output.append(("NUMBER", self._buffer))
            self._buffer = ""

    def on_dispatch_done(self, input: str, current_state: str):
        self._buffer += input

def tokenize(text: str) -> list[tuple[str, str]]:
    out = []
    listener = Listener(out)
    runner = StateMachineRunner(definition, listener)
    with runner:
        for char in text:
            runner.dispatch(char)
        runner.dispatch("")
    return out

And the tokenizer from above will split any text composed of just words and numbers into something like this:

out = tokenize("foo123bar456")
print(out)  # Would print: [("WORD", "foo"), ("NUMBER", "123"), ("WORD", "bar"), ("NUMBER", "456")]

License

This project is released under the terms of the MIT license.

Metadata

Release files for tinyfsm 0.1.0

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

Source distribution (sdist)

Source distribution for tinyfsm 0.1.0
File Size Uploaded Provenance Actions
tinyfsm-0.1.0.tar.gz 5.5 kB Without provenance Details for tinyfsm-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tinyfsm 0.1.0
File Interpreter ABI Platform Actions
tinyfsm-0.1.0-py3-none-any.whl Python 3 none any Details for tinyfsm-0.1.0-py3-none-any.whl

Total release size: 12.9 kB

Release files / tinyfsm-0.1.0.tar.gz

Download URL tinyfsm-0.1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9b92822d868ad92e93d4caf8e0c9787fc52da79e20a6a12b941feabacfc42727
BLAKE2b-256 checksum
How to use checksums
48f970e6ae683a271dc58d15b14a8b1b8be0f17cfe4257ba99de1adbc2145e83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.13.15 Linux/7.0.0-1009-aws

Release files / tinyfsm-0.1.0-py3-none-any.whl

Download URL tinyfsm-0.1.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de7fd8765215d7fc3a3ca364afbd980b6da25fe55eedcf5cad0014a1e7181334
BLAKE2b-256 checksum
How to use checksums
b1fb520e4cb311d88d2ce889d504b0cecbf7bb9876410a50b09b0490f2dfefb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.13.15 Linux/7.0.0-1009-aws

Security for tinyfsm 0.1.0

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.

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