Python code written in 1998, how to improve/change it?

Carl Cerecke cdc at maxnet.co.nz
Sun Jan 22 22:15:48 EST 2006


Petr Jakes wrote:
> Sorry, I can't get in. Can you please show me, how to use your approach
> on the simple push/push ON/OFF button for example please?
> 
> PS: seriously it is not a homework :) and I feel it like a shame I am
> asking such a simple questions :(
> 
> States: ON, OFF
> Transition event: "push", "lift"
> 
> transition diagram:
> =========================
> 
>            ___ lift
>           |   |
>          _V___|__
>      ,->|  ON    |__
>      |  |________|  |
> lift |              | push
>      |   ________   |
>      '--|  OFF   |<-'
>         |________|
>           ^   |
>           |___|push
> 

As a starting point, how about:

l = 'lift'
p = 'push'

action_sequence = [l,p,p,l,l,p,l,p,None]
next_action = iter(action_sequence).next

s_on = compile('''
print 'on'
action = next_action()
if action == 'lift':
     state = s_on
elif action == 'push':
     state = s_off
else:
     state = None
''','','exec')

s_off = compile('''
print 'off'
action = next_action()
if action == 'lift':
     state = s_on
elif action == 'push':
     state = s_off
else:
     state = None
''','','exec')


state = s_on # start state
while state:
     eval(state)



Cheers,
Carl



More information about the Python-list mailing list