GOTO (was Re: Appeal for python developers)

Anthra Norell anthra.norell at tiscalinet.ch
Mon Mar 7 22:39:28 CET 2005


> Heiko wrote:

> SETUP = object()
> ELSE = object()
> BREAK = object()
> 
> machine = {"WAITING FOR ACTION":
>               {customer_drops_coin:"COIN HAS BEEN DROPPED",
>                customer_selects_beverage:"ORDER RECEIVED",
>                customer_cancels_order:"ACCOUNT CLOSURE IS DUE"
>                ELSE:"WAITING FOR ACTION"},
>            "COIN HAS BEEN DROPPED":
>                {SETUP:identify_coin,
>                 credit_account:"PAYMENT DUE IS UNKNOWN"},
>             "ORDER RECEIVED":
>                {...
>
> Reading the state machine in the way presented above isn't any harder in my
> taste than reading your state table, and you should easily be able to run the 
> machine from there...

> --- Heiko.

I think I get your idea. I also see a few problems with your proposal. Let me try:

def customer_drops_coin (): ...
def customer_selects_beverage (): ...
def customer_cancels_order: ...
etc.

def ELSE: return True


machine = { "WAITING FOR ACTION":    ( ( customer_drops_coin,        "COIN HAS BEEN DROPPED" ),
                                       ( customer_selects_beverage,  "ORDER RECEIVED" ),
                                       ( customer_cancels_order,     "ACCOUNT CLOSURE IS DUE" ),
                                     # ( ELSE,                       "WAITING FOR ACTION" ),
                                     ),

            "COIN HAS BEEN DROPPED": ( ( identify_coin,              None ),
                                       ( credit_account,             "PAYMENT DUE IS UNKNOWN" ),
                                                                           ),

            "ORDER RECEIVED":
                                     ( ( ...
   

 def run_state_machine (machine, initial_state = "BEGIN"):

   state = initial_state   # Here would be "WAITING FOR ACTION"
   while True:
      for function, next_state in machine [state]:
         go_next = function ()
         if go_next:
            if next_state:
                           state = next_state
         if state == "END":
            return

This should work.

Frederic

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050307/07449b0f/attachment.html>


More information about the Python-list mailing list