State Machines in Python

Roy Smith roy at panix.com
Sat Sep 4 15:45:17 EDT 2010


In article <mailman.448.1283625070.29448.python-list at python.org>,
 "D'Arcy J.M. Cain" <darcy at druid.net> wrote:

> On Sat, 04 Sep 2010 13:58:00 -0400
> Roy Smith <roy at panix.com> wrote:
> > > while True:
> > >     state = state(data)
> > 
> > This is the pattern I've always used.  Simple and effective for any 
> > state machine which is small enough to code by hand.  I generally have 
> > my state methods return (next_state, output) tuples, but that's a detail.
> 
> What is "output" for?  Is it a string or something else?

I've often used this pattern for parsing a text file and extracting 
interesting data.  At various points during the input stream, you will 
have read an entire piece of data, and return it as the output of the 
state machine.  Not every state will result in output being produced.

As a trivial example, let's say I had a file format which stored network 
addresses in a deliberately silly style:

--------------------
# This is a comment

host = 10.2.3.4
port = 999
proto = TCP

port = 1001
proto = TCP
host = 192.168.1.1

status = ignore
host = 1.2.3.4
port = 22
host = 127.0.0.1

proto = UDP
port = 1001
host = 192.168.1.1
--------------------

I want to parse out (host, port, proto) triples, i.e. the state machine 
should produce the following output:

(10.2.3.4, 9099, TCP)
(192.168.1.1, 1001, TCP)
(192.168.1.1, 1001, UDP)

As the end of each data block is recognized, a 3-tuple would be 
returned.  Other state transitions would return None as the output.  
Then, the main loop can be something like:

state = start
for line in input:
   next_state, output = state(line)
   if output:
      print output
   state = next_state

I'm not saying it has to be done that way, just that I've found it a 
handy pattern for the stuff I've done.



More information about the Python-list mailing list