Skip to main content

FAVink

Maintainability CodeFactor Build Status

Over-simple python finite automata (finite-state machine) implementation

Getting Started

To install favink use the package from the PyPI repository:

pip install favink

To add finite automata feature to the class in your code you have to inherit FiniteAutomata class and define the following members:

  • transitions table transitions
  • initial state init_state
  • event handlers methods.

For every transition the constructor creates a dynamic method with argument mask (self, *args, **kwargs). Each method is named after the transition.

To make the transition you should call the transition method. After the method has been called it changes the instance state and invoke related event handlers. If the called transition isn't allowed for the current state InvalidTransition extension will be raised.

Transition Table and Initial State

The transitions is a dictionary where keys are transition names, values define the allowed and target states:

transitions = {
    "transition_1":
    [
        "allowed_state_1",
        "target_state_1"
    ],
    "transition_2":
    [
        [
            "allowed_state_2",
            "allowed_state_3"
        ],
        "target_state_2"
    ]
}

Initial state is defined by init_state member.

Events

Transition Life Cycle

Making of transition triggers the following events and invokes the related handlers (if they have been implemented in the class):

  • after
  • before
  • on

Event Handler Definitions

For every state (for example state_name) the following methods can be defined:

def before_state_name(self, name, *args, **kwargs):
    ...


def on_state_name(self, name, origin, *args, **kwargs):
    ...


def after_state_name(self, name, *args, **kwargs):
    ...

The following arguments are passed to the handlers:

  • name is a invoked transition name,
  • origin (passed only to on_... handler) is a previous state name,
  • *args, **kwargs are positional and keyword arguments passed to the transition method.

If after_... or before... handlers raise the exception transition is aborted.

Car Example

Transition Life Cycle

class Car(FiniteAutomata):
    init_state = "stopped"
    transitions = {
        "start_engine": ["stopped", "idle"],
        "stop_engine": ["idle", "stopped"],
        "forward": ["idle", "moving_forward"],
        "backward": ["idle", "moving_backward"],
        "stop": [["moving_forward", "moving_backward"], "idle"],
    }

    def on_stopped(self, transition, origin, *args, **kwargs):
        print("Engine has been stopped")

    def on_idle(self, transition, origin, *args, **kwargs):
        print("I'm not moving, but engine is on")

    def on_moving_forward(self, transition, origin, *args, **kwargs):
        print("Let's go!")

    def on_moving_backward(self, transition, origin, *args, **kwargs):
        print("Why are we retreating?")

car = Car()

car.start_engine()
car.forward()
car.stop()
car.backward()
car.stop()
car.stop_engine()

Output:

I'm not moving, but engine is on
Let's go!
I'm not moving, but engine is on
Why are we retreating?
I'm not moving, but engine is on
Engine has been stopped

API Reference

Predefined FiniteAutomata Methods

FiniteAutomata.get_state(self)

Returns the current instance state name as a string.

FiniteAutomata.get_allowed_transitions(self)

Return the list contains all transactions which are allowed for the current instance state.

FiniteAutomata.is_allowed(self, transition)

Transition Methods

Dynamically defined methods for every transition (key) in the transitions dictionary.

Metadata

Release files for favink 2.0.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 favink 2.0.0
File Size Uploaded
favink-2.0.0.tar.gz 4.8 kB Details

Built distribution (wheel)

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

Total release size: 9.4 kB

Release files / favink-2.0.0.tar.gz

Download URL favink-2.0.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6eade6fef04de11f4aaca79523272170a84423291cc51f78dfa357440b58835a
BLAKE2b-256 checksum
How to use checksums
c5e640e63e5dbea99ddf284ab9a498dcd24923b924216b634fbc9245ab97e2eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.2 CPython/3.5.6 Linux/4.15.0-1028-gcp

Release files / favink-2.0.0-py3-none-any.whl

Download URL favink-2.0.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
130b357ca7fb19fe7d64a52d06396ecb65cf526aa2caef6e0bc879c5e72edfb4
BLAKE2b-256 checksum
How to use checksums
f368213567b06efb40291a496907d4b9ce1f64e47ab89b3d83e0dfa3d7fe908e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.2 CPython/3.5.6 Linux/4.15.0-1028-gcp

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.2

2 release files

0.1.1

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