Skip to main content

easy_fsm

Easy to implement Finite State Machines.

License Development Status Latest release Supported Python versions Supported Python implementations Download format Build Status Downloads

Introduction

easy_fsm provides a very simple API to build finite state machines.

The state machine holds a context that is passed to the different states.

Each state returns either the next state to execute or None if the execution is done.

This allows you to implement business logic in small, well separated, chunks of code.

Example

from typing import Optional
from easy_fsm import StateMachine, State
from dataclasses import dataclass, field


@dataclass
class Stats:
    altitude: int = 0
    fly_time: int = 0
    suite: list[int] = field(default_factory=list)


class ComputeSyracuse(State[Stats]):
    def __init__(self, n: int):
        self.n = n

    def run(self, context: Stats) -> Optional[State[Stats]]:
        context.altitude = max(context.altitude, self.n)
        context.fly_time += 1
        context.suite.append(self.n)

        if self.n == 1:
            return None

        elif self.n % 2 == 0:
            return ComputeSyracuse(self.n // 2)

        else:
            return ComputeSyracuse(3 * self.n + 1)


class Syracuse(StateMachine[Stats]):
    def __init__(self):
        super().__init__(Stats())

    def compute(self, n: int) -> None:
        self.run_from(ComputeSyracuse(n))


def test_fsm():
    fsm = Syracuse()
    fsm.compute(5)

    assert fsm.context.altitude == 16
    assert fsm.context.fly_time == 6
    assert fsm.context.suite == [5, 16, 8, 4, 2, 1]

License

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

Metadata

Release files for easy-fsm 0.2.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 easy-fsm 0.2.0
File Size Uploaded
easy_fsm-0.2.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for easy-fsm 0.2.0
File Interpreter ABI Platform
easy_fsm-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / easy_fsm-0.2.0.tar.gz

Download URL easy_fsm-0.2.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a39dc0218baeb35bbc75f708e1e1f4fa4634abc981d5ac95db7d05a6289f99f0
BLAKE2b-256 checksum
How to use checksums
54e964230b36ed9c05fe98c8df7f538349895f43384c2d9d1b28a10144a37fda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.0 Windows/10

Release files / easy_fsm-0.2.0-py3-none-any.whl

Download URL easy_fsm-0.2.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0327d5c3b5bdb666658f9626f65504d7071c3554580cea7e60ecc388519361e
BLAKE2b-256 checksum
How to use checksums
317c76098a985c15c5323ea85b9bd865e1517c0006f3a203c8380289522cf065
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.0 Windows/10

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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