Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67358

Re: Coding a simple state machine in python

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Coding a simple state machine in python
Date 2014-02-24 20:54 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-F51848.20540624022014@news.panix.com> (permalink)
References <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com>

Show all headers | View raw


In article <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com>,
 Ronaldo <abhishek1899@gmail.com> wrote:

> How do I write a state machine in python? I have identified the states and 
> the conditions. Is it possible to do simple a if-then-else sort of an 
> algorithm? Below is some pseudo code:
> 
> if state == "ABC":
>    do_something()
>    change state to DEF
> 
> if state == "DEF"
>    perform_the_next_function()

There's lots of ways to code state machines in Python.  The last time I 
did one, I made each state a function.  Each function returned a 
(next_state, output) tuple.  I don't know if that's the best way, but it 
worked and seemed logical to me.  Here's a trivial example with just two 
states:

class StateMachine:
    def __init__(self):
        self.state = self.start

    def process(self, input):
        for item in input:
            self.state, output = self.state(item)
            print output

    def start(self, item):
        if item > 5:
            return self.end, "done"
        else:
            return self.start, "still looking"

    def end(self, item):
        if item < 3:
            return self.start, "back to the beginning"
        else:
            return self.end, "still stuck at the end"

sm = StateMachine()
sm.process([1, 2, 6, 4, 2, 2])


When you run it, it should print:

still looking
still looking
done
still stuck at the end
back to the beginning
still looking

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Coding a simple state machine in python Ronaldo <abhishek1899@gmail.com> - 2014-02-24 17:30 -0800
  Re: Coding a simple state machine in python Tim Daneliuk <tundra@tundraware.com> - 2014-02-24 19:46 -0600
  Re: Coding a simple state machine in python William Ray Wing <wrw@mac.com> - 2014-02-24 21:55 -0500
    Re: Coding a simple state machine in python Tim Daneliuk <tundra@tundraware.com> - 2014-02-24 21:27 -0600
    Re: Coding a simple state machine in python Tim Daneliuk <tundra@tundraware.com> - 2014-02-24 21:27 -0600
      Re: Coding a simple state machine in python alex23 <wuwei23@gmail.com> - 2014-02-25 15:19 +1000
        Re: Coding a simple state machine in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-25 08:35 +0000
        Re: Coding a simple state machine in python Peter Otten <__peter__@web.de> - 2014-02-25 11:01 +0100
          Re: Coding a simple state machine in python alex23 <wuwei23@gmail.com> - 2014-03-03 14:49 +1000
  Re: Coding a simple state machine in python Peter Otten <__peter__@web.de> - 2014-02-25 11:04 +0100
    Re: Coding a simple state machine in python Marko Rauhamaa <marko@pacujo.net> - 2014-02-25 13:20 +0200
  Re: Coding a simple state machine in python Roy Smith <roy@panix.com> - 2014-02-24 20:54 -0500

csiph-web