Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'pgp': 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; 'subject:simple': 0.16; "tim's": 0.16; 'wing': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'examples': 0.20; 'feb': 0.22; 'machine': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'cc:2**0': 0.24; '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; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'point': 0.28; 'function': 0.29; 'points': 0.29; 'tim': 0.29; 'url:mailman': 0.30; '8:30': 0.31; 'ray': 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; 'possible': 0.36; 'url:org': 0.36; 'too': 0.37; 'thank': 0.38; 'conditions.': 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; 'easy': 0.60; 'new': 0.61; 'simple': 0.61; "you're": 0.61; 'here:': 0.62; 'making': 0.63; 'to:addr:gmail.com': 0.65; 'william': 0.81; '2014,': 0.84; 'thing,': 0.91; 'state.': 0.95 Date: Mon, 24 Feb 2014 21:27:30 -0600 From: Tim Daneliuk User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 Newsgroups: comp.lang.python To: William Ray Wing , Ronaldo Subject: Re: Coding a simple state machine in python References: <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.4.3 (ozzie.tundraware.com [75.145.138.73]); Mon, 24 Feb 2014 21:27:31 -0600 (CST) X-TundraWare-MailScanner-Information: Please contact the ISP for more information X-TundraWare-MailScanner-ID: s1P3RUb3029642 X-TundraWare-MailScanner: Found to be clean X-TundraWare-MailScanner-From: tundra@tundraware.com X-Spam-Status: No Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393299069 news.xs4all.nl 2835 [2001:888:2000:d::a6]:42848 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67018 On 02/24/2014 08:55 PM, William Ray Wing wrote: > > On Feb 24, 2014, at 8:30 PM, Ronaldo 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 > Now you're making it TOO easy Bill ;) -- ---------------------------------------------------------------------------- Tim Daneliuk tundra@tundraware.com PGP Key: http://www.tundraware.com/PGP/