Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67017
| Path | csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Daneliuk <tundra@tundraware.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Coding a simple state machine in python |
| Date | Mon, 24 Feb 2014 21:27:30 -0600 |
| Organization | A noiseless patient Spider |
| Lines | 46 |
| Message-ID | <530C0DA2.7020202@tundraware.com> (permalink) |
| References | <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com> <mailman.7338.1393296954.18130.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Info | mx05.eternal-september.org; posting-host="d887b3e675279b69468e57e74ea1dcb8"; logging-data="10418"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+D1GpRQerP+DauiDfZrHBkHPEPXjGbRHI=" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 |
| In-Reply-To | <mailman.7338.1393296954.18130.python-list@python.org> |
| Cancel-Lock | sha1:t2cf+UCMTVu4AYdTXX4oOYrnx98= |
| Xref | csiph.com comp.lang.python:67017 |
Show key headers only | View raw
On 02/24/2014 08:55 PM, William Ray Wing wrote: > > 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 > Now you're making it TOO easy Bill ;) -- ---------------------------------------------------------------------------- Tim Daneliuk tundra@tundraware.com PGP Key: http://www.tundraware.com/PGP/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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