Package: python-automaton
Pythonic finite-state machines.Automaton is an easy to use,pythonicfinite-state machinemodule for Python 3.4 or greater.
The goal here is to have something to define finite-state machines in a minimal, straightforward
and elegant way that enforcesclarityandcorrectness.InstallationAutomaton is available onpypi,
so to install it just use:$ pip3 install python-automatonDependenciesPython >= 3.4networkxtabulateGetting startedIn order to define an automaton, just subclass a provided base:fromautomatonimport*classTrafficLight(Automaton):go=Event("red","green")slowdown=Event("green","yellow")stop=Event("yellow","red")You’re done: you now have a newautomatondefinition that can be instantiated to aninitial state:crossroads=TrafficLight(initial_state="red")assertcrossroads.state=="red"Eachevent(also known astransition) is a class attribute defined by itssource stateanddestination state. When anevent fires, the automaton changes its state from thesourceto thedestination:
you canfirean event by calling it:crossroads.go()assertcrossroads.state=="green"crossroads.slowdown()assertcrossroads.state=="yellow"An alternative way, more convenient if triggering events progammatically, is to call theevent()method:crossroads.event("stop")assertcrossroads.state=="red"CorrectnessAutomaton enforces correctness in two ways:checking that the requested event isvalid, that is a transition from the current state to the destination
state exists in the state machine definition;checking whether thestate graphrepresenting the automaton isconnectedor not (that is it must have onlyoneconnected component).So, if you try to trigger an invalid event…crossroads=TrafficLight(initial_state="red")crossroads.stop()…you will end up with an error:automaton.InvalidTransitionError: The specified event 'Event(source_states=('yellow',), dest_state='red')'
                                  is invalid in current state 'red'.Again, trying to define a class like this…classBrokenTrafficLight(Automaton):go=Event("red","green")slowdown=Event("green","yellow")# broken!stop=Event("black","purple")…will trigger an error:automaton.DefinitionError: The state graph contains 2 connected components:
                           ['green', 'yellow', 'red'], ['purple', 'black']How to visualize an automaton?When things are getting complex and it seems that our automata are becoming autonomous life forms grasping to escape
our control, it could be useful to have ahuman friendlyrepresentation of their behaviour.You can ask for thetransition table…transitiontable(TrafficLight,fmt='rst')…and you will be presented with a nicereStructuredTextsnippet:========  ======  ========
Source    Dest    Event
========  ======  ========
green     yellow  slowdown
yellow    red     stop
red       green   go
========  ======  ========You can ask for thestate graphas well…stategraph(TrafficLight,fmt='plantuml')…and you’ll end up with a properPlantUMLrepresentation…@startuml
    [*] --> red
    green --> yellow : slowdown
    red --> green : go
    yellow --> red : stop
@enduml…that can of course be rendered throughplantuml:Keep your docstrings tidy!Sinceautomata are classeshere, it would be great to have a textual representation of the automaton’s behaviour
in our docstrings. What about having one that updates itself in order to stay up-to-date with the
actual code?Here you have it:classTrafficLight(Automaton):""" This is a pretty standard traffic light.

    This automaton follows the behaviour defined by
    the following transition table:

    {automaton:rst}

    """go=Event("red","green")slowdown=Event("green","yellow")stop=Event("yellow","red")Using a standard format specifier with theautomatonkeyword and the proper output format (e.g.:rst),the
automaton representation will be inserted in the docstring during the class definition,just where it should be:>>>print(TrafficLight.__doc__)""" This is a pretty standard traffic light.

This automaton follows the behaviour defined by
the following transition table:

========  ======  ========
Source    Dest    Event
========  ======  ========
green     yellow  slowdown
yellow    red     stop
red       green   go
========  ======  ========

"""Easy!DocumentationYou can find the full documentation athttp://automaton.readthedocs.org.Changes1.3.0(2017-02-05)AddedEnabled access to all event’s attributes from automaton instances.New constructor parameter to initialize an automaton given an initialstartupevent.ChangedMisc bugs fixed.Tests cleanup.Improved reference and documentation.1.2.1(2017-01-30)FixedSevere distribution issue: package was missing some files.Tox testing:py.testwas running againstsource files,not against the package installed in ``tox`` virtualenv.1.2.0(2017-01-29)AddedCustom format specifiers forAutomatondefinitions (classes and instances).Auto-docstring completion: if requested, the automaton textual representation
is automatically added to the__doc__class attribute.ChangedRefactored formatting functions to more streamlined and coherent interfaces.Removed package, now the whole library lives in one module file.1.1.0(2017-01-28)AddedAutomaton representation as transition table or state-transition graph.1.0.0(2017-01-25)AddedFunctions to retrieve incoming and outgoing events from a state or a set of states.