[Tutor] engines

alan.gauld@bt.com alan.gauld@bt.com
Fri, 19 Jul 2002 13:34:31 +0100


Hi Gregor,

Yes it sounds like you are writing an engine but....

> eventlist = []
> def taste(event):
>     key = event.keysym
>     if key in goodkeys:
>         self.eventlist.append(key)
> 
> def run():
>     taste = None
>     # 1. suck one command from the eventlist
>     if self.eventlist:
>          taste = self.eventlist[0]
>          self.eventlist = self.eventlist[1:]

Why not just use the Tkinter event list? 
ie just catch the event and process it directly in 
the run code? Maintaining your own event list when 
you don't change the order of the received events 
seems like redundant effort.

>     # 2. then process some sort of finite state machine:
>     if state == "start":
>     elif state = "running":
>     elif state = "over":

This is pretty standard state machine processing but you 
might get a performance improvement if there are many 
states by writing lots of small state handling functions 
and storing them in a dictionary. Even if not for performance 
its much easier to extend the state machine this way, 
simply add a new state handler and an entry in the dictionary.
Often the same habdler can be used for several states...

states = {"start":dostart,
          "running":doRunning,
          "over":doOver,....}

then just call the functions like so:

state = states[state](param)

This assumes the doXXX functions can be made to take a common 
parameter set and that they return the next state.

Just some thoughts,

Alan g.