Python Advanced - Finite State Machine in Python
Python Advanced - Finite State Machine in Python
Topics
Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact
To implement the previous example, we program first a general Finite State Machine in Python. We save
this class as statemachine.py: This topic in German
Help Needed / Deutsche
class StateMachine: Übersetzung:
This website is free def __init__(self): Endliche Automaten
of annoying ads. We self.handlers = {} in Python
want to keep it like self.startState = None
this. You can help self.endStates = []
Python Training
with your donation: Courses
def add_state(self, name, handler, end_state=0):
name = name.upper()
self.handlers[name] = handler If you want to learn
The need for if end_state: Python fast and
donations self.endStates.append(name) efficiently, the right
step will be a
def set_start(self, name): Python Training
self.startState = name.upper() course at Bodenseo.
Wisdom There are also
def run(self, cargo): special seminars for
try:
"The infinite is in the advanced students
handler = self.handlers[self.startState]
finite of every like the Python &
except:
instant" (Zen XML Training Course.
raise InitializationError("must call .set_start() before .run()")
proverb) if not self.endStates: If you want to
raise InitializationError("at least one state must be an end_state") acquire special
*** knowledge in Text
while True: Processing and Text
"No finite point has (newState, cargo) = handler(cargo) Classification, then
meaning without an if newState.upper() in self.endStates: "Python Text
infinite reference print("reached ", newState) Processing Course"
point" (Jean-Paul break will be the right one
Sartre)
else: for you.
handler = self.handlers[newState.upper()] All the Python
*** seminars are
This general FSM is used in the next program: available in German
"Ask what Time is, it as well: Python-
from statemachine import StateMachine Kurse"
is nothing else but
something of eternal positive_adjectives = ["great","super", "fun", "entertaining", "easy"]
duration become negative_adjectives = ["boring", "difficult", "ugly", "bad"]
finite, measurable
and transitory." def start_transitions(txt):
(William Law) splitted_txt = txt.split(None,1)
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"")
if word == "Python":
newState = "Python_state"
"The machine does else: Python Courses at
not isolate us from newState = "error_state" Bodenseo.
the great problems return (newState, txt)
of nature but You can book Bernd
def python_state_transitions(txt): Klein for on-site
plunges us more
splitted_txt = txt.split(None,1) Python courses as
deeply into them"
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") well.
(Antoine de Saint- if word == "is":
Exupery) newState = "is_state"
else:
*** newState = "error_state" Text
return (newState, txt) Classification
"A tool is but the
extension of a man's def is_state_transitions(txt): Though the
hand, and a machine splitted_txt = txt.split(None,1) automated
is but a complex word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") classification
tool. And he that if word == "not": (categorization) of
invents a machine newState = "not_state"
texts has been
augments the power elif word in positive_adjectives:
flourishing in the last
of a man and the newState = "pos_state"
elif word in negative_adjectives: decade, it has a
well-being of history, which dates
newState = "neg_state"
mankind." (Henry back to about 1960.
else:
Ward Beecher) The incredible
newState = "error_state"
return (newState, txt) increase in online
documents, which
This website is def not_state_transitions(txt): has been mostly due
supported by: splitted_txt = txt.split(None,1) to the expanding
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") internet, has
Python Training if word in positive_adjectives: renewed the interst
Courses newState = "neg_state" in automated
elif word in negative_adjectives: document
newState = "pos_state" classification and
else: data mining. While
newState = "error_state"
text classification in
return (newState, txt)
the beginning was
def neg_state(txt): based mainly on
print("Hallo") heuristic methods,
return ("neg_state", "") i.e. applying a set of
rules based on
if __name__== "__main__": expert knowledge,
m = StateMachine() nowadays the focus
m.add_state("Start", start_transitions) has turned to fully
m.add_state("Python_state", python_state_transitions) automatic learning
m.add_state("is_state", is_state_transitions) and even clustering
m.add_state("not_state", not_state_transitions) methods.
m.add_state("neg_state", None, end_state=1)
m.add_state("pos_state", None, end_state=1)
m.add_state("error_state", None, end_state=1)
m.set_start("Start")
m.run("Python is great")
Help Needed
m.run("Python is difficult")
m.run("Perl is ugly")
This website is free
If we save the application of our general Finite State Machine in statemachine_test.py and call it with of annoying ads. We
want to keep it like
python statemachine_test.py this. You can help
with your donation:
we get the following results:
$ python statemachine_test.py
The need for
reached pos_state which is an end state
reached neg_state which is an end state donations
reached error_state which is an end state
The code of the finite state machine is compatible with Python3 as well! Data Protection
Previous Chapter: Currying in Python Declaration
Next Chapter: Turing Machine in Python
Data Protection
Declaration
© 2011 - 2020, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein