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


Groups > comp.lang.python > #67016

Re: Coding a simple state machine in python

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <wrw@mac.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'table.': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'compact': 0.09; 'variables.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'gui': 0.12; '"def"': 0.16; '24,': 0.16; 'direction?': 0.16; 'received:mac.com': 0.16; 'subject:simple': 0.16; "tim's": 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'received:10.0.1': 0.19; 'examples': 0.20; 'feb': 0.22; 'machine': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'this?': 0.23; 'cc:no real name:2**0': 0.24; 'sort': 0.25; 'pass': 0.26; 'code:': 0.26; 'certain': 0.27; 'values': 0.27; 'gets': 0.27; 'point': 0.28; 'function': 0.29; 'points': 0.29; 'url:mailman': 0.30; '8:30': 0.31; 'class': 0.32; 'extend': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'really': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'next': 0.36; 'charset :us-ascii': 0.36; 'possible': 0.36; 'received:10.0': 0.36; 'url:org': 0.36; 'received:10': 0.37; 'thank': 0.38; 'conditions.': 0.38; 'received:17': 0.38; 'pm,': 0.38; 'does': 0.39; 'bill': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'url:mail': 0.40; 'how': 0.40; 'new': 0.61; 'simple': 0.61; 'here:': 0.62; 'to:addr:gmail.com': 0.65; '2014,': 0.84; 'header:In-reply-to:1': 0.84; 'thing,': 0.91; 'state.': 0.95
X-Proofpoint-Virus-Version vendor=fsecure engine=2.50.10432:5.11.87,1.0.14,0.0.0000 definitions=2014-02-24_03:2014-02-21,2014-02-24,1970-01-01 signatures=0
X-Proofpoint-Spam-Details rule=notspam policy=default score=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1401130000 definitions=main-1402240179
Content-type text/plain; charset=us-ascii
MIME-version 1.0 (Mac OS X Mail 6.6 \(1510\))
Subject Re: Coding a simple state machine in python
From William Ray Wing <wrw@mac.com>
In-reply-to <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com>
Date Mon, 24 Feb 2014 21:55:31 -0500
Content-transfer-encoding quoted-printable
References <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com>
To Ronaldo <abhishek1899@gmail.com>
X-Mailer Apple Mail (2.1510)
Cc python-list@python.org, William Ray Wing <wrw@mac.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7338.1393296954.18130.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393296954 news.xs4all.nl 2829 [2001:888:2000:d::a6]:39419
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67016

Show key headers only | View raw


On Feb 24, 2014, at 8:30 PM, 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()
> ...
> 
> I have a class to which certain values are passed from a GUI and the functions above have to make use of those variables. How do I go about doing this? I have the following algorithm:
> 
> class TestClass():
>    def __init__(self, var1, var2): #var1 and var2 are received from a GUI
>       self.var1 = var1
> ...
>    if state == "ABC"
>       doSomething(var1, var2)
> ..
> 
> Could someone point me in the right direction? Thank you!
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

And, to extend Tim's suggestion of a dictionary just a bit, note that since Python functions are happy to pass function names as arguments, you can use a dictionary to make a really nice compact dispatch table.  That is, function A does its thing, gets to a new state, and returns as one of its return arguments the key into the dictionary that points to the next function_name to be called based on that new state.

Stackoverflow has a couple of compact examples here:  

http://stackoverflow.com/questions/715457/how-do-you-implement-a-dispatch-table-in-your-language-of-choice

Bill

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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