Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67038
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Coding a simple state machine in python |
| Date | 2014-02-25 13:20 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <87lhwz1ls8.fsf@elektro.pacujo.net> (permalink) |
| References | <65ac9612-fd48-472a-b077-c802be96ece3@googlegroups.com> <41DAA444-F51A-458D-8F79-8CD3117A4879@mac.com> <mailman.7351.1393322706.18130.python-list@python.org> |
Peter Otten <__peter__@web.de>:
> Why have the function return a name? Why not just another function?
As people have said, there are many ways to skin the cat.
A function can represent a state if it is the only type of event the
state machine must process. A regular expression parser would be an
example.
In the general case, a state machine is a matrix of M states by N types
of event. Then, one natural manner of representing a state is a nested
class:
class Lackey:
def __init__(self):
lackey = self
class Idle:
def handle_ding(self):
lackey.start_timer(10)
lackey.set_state(Dinged)
class Dinged:
def handle_dong(self):
lackey.cancel_timer()
lackey.open_door()
lackey.ask_for_name()
lackey.start_timer(20)
lackey.set_state(AwaitingName)
def handle_timeout(self):
lackey.open_door()
lackey.shoo_visitor_away()
lackey.set_state(Annoyed)
# other state classes here...
self.set_state(Idle)
def set_state(self, state):
log("Lackey(): New state: {}".format(
id(self), state.__class__.__name__))
self.state = state()
def handle_ding(self):
self.state.handle_ding()
def handle_dong(self):
self.state.handle_dong()
def handle_timeout(self):
self.state.handle_timeout()
def start_timer(self):
# etc etc
Marko
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