Over-simple Python finite automata (finite-state machine) implementation
Project description
FAVink
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
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 toon_...
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
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.
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
Built Distribution
File details
Details for the file favink-2.0.0.tar.gz
.
File metadata
- Download URL: favink-2.0.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.2 CPython/3.5.6 Linux/4.15.0-1028-gcp
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eade6fef04de11f4aaca79523272170a84423291cc51f78dfa357440b58835a |
|
MD5 | 86cf1e9f95bde2e6c4bc8e889bd431ae |
|
BLAKE2b-256 | c5e640e63e5dbea99ddf284ab9a498dcd24923b924216b634fbc9245ab97e2eb |
File details
Details for the file favink-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: favink-2.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.2 CPython/3.5.6 Linux/4.15.0-1028-gcp
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 130b357ca7fb19fe7d64a52d06396ecb65cf526aa2caef6e0bc879c5e72edfb4 |
|
MD5 | c5ca7b27cead40ee5e863692526364ac |
|
BLAKE2b-256 | f368213567b06efb40291a496907d4b9ce1f64e47ab89b3d83e0dfa3d7fe908e |