Skip to main content

A tiny and minimal finite state machine for Python.

Project description

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.

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

tinyfsm-0.0.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

tinyfsm-0.0.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file tinyfsm-0.0.2.tar.gz.

File metadata

  • Download URL: tinyfsm-0.0.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.17.0-1013-aws

File hashes

Hashes for tinyfsm-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d6687472e1c3a489d785e287d1edfc559a337d4b72651bca1bccb5a1d4d8191d
MD5 09336b56bc170d2e1576c2578978aa20
BLAKE2b-256 1ce65f396076617791890e8d37ea27e74b9e5d9c8f56cbaf89e3e53eb6792b30

See more details on using hashes here.

File details

Details for the file tinyfsm-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tinyfsm-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.17.0-1013-aws

File hashes

Hashes for tinyfsm-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8dbcb7695178d1f049a29441ea1cb1fbe271f1ac892e6c5057a735883b0594f5
MD5 96a2707f82f8b902f7625c7294078b6f
BLAKE2b-256 9e8083ca9f92c821e07939584f8e85280f17404c39dc510467a78ea30e70c85a

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