Skip to main content

Easy to implement Finite State Machines

Project description

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.

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

easy_fsm-0.2.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

easy_fsm-0.2.0-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page