[Tutor] using exceptions to implement case/switch?

Bill Campbell bill at celestial.net
Fri Oct 15 21:22:20 CEST 2004


On Fri, Oct 15, 2004, Kent Johnson wrote:
>A simple way to do this is to use a dictionary as a dispatcher:
>dispatch = {
>  curses.KEY_F1 : process_keyF1,   # Note - no parentheses here, this is a 
>reference to the fn, not a call
>  curses.KEY_F2 : process_keyF2,
>  curses.KEY_F3 : process_keyF3,
>  curses.KEY_F4 : process_keyF4,
>}
>
>        while True:
>                c = win.getch()
>                if ascii.isprint(c) : return c
>                fn = dispatch.get(c, curses.beep)
>                fn()

That's a good approach when one wants to run separate processes, but
doesn't handle the case where one wants to run in the current name space.

In the curses example below, key codes like left-arrow or right-arrow may
change the cursor position on the screen by changing y, x variables.  A
variation might be to have a dictionary of lambda functions that raise the
appropriate exceptions (hopefully I have the syntax correct :-):

dispatch = {
  curses.KEY_F1 : (lambda x: raise keyf1),
  curses.KEY_F2 : (lambda x: raise keyf2),
  curses.KEY_F3 : (lambda x: raise keyf3),
  curses.KEY_F4 : (lambda x: raise keyf4),
}

def getchar(win, keymap=dispatch):
	while True:
		c = win.getch()
		if curses.ascii.isprint(c) : return(c)
		fn = keymap.get(c, curses.beep)
		fn()

Using a dictionary has the advantage that it's easier to map multiple keys
to the same exception, and the addition of the optional keymap parameter to
getchar allows one to change the key map depending on the situation.

>Kent
>
>At 09:56 AM 10/15/2004 -0700, Bill Campbell wrote:
>>I'm looking for feedback on the pros and cons of using exceptions to
>>implement constructions similar to the C switch or other language's case
>>statements.
>>
>>I'm in the process of converting bunch of C code that makes extensive use
>>of switch() statements.  In particular I'm doing a python curses
>>implementation where it's parsing keys as they are pressed with different
>>case parts when special keys are hit.  Rather than doing this with a set of
>>``if'' statements, this could be done using exceptions, perhaps as in the
>>code below.
>>
>># start
>>import curses
>>import curses.ascii as ascii
>>
>>keyf1 = 'key F1 pressed'
>>keyf2 = 'key F2 pressed'
>>keyf3 = 'key F3 pressed'
>>keyf4 = 'key F4 pressed'
>># ...
>>
>>def getchar(win):
>>        '''Get printable character from curses window'''
>>        while True:
>>                c = win.getch()
>>                if ascii.isprint(c) : return c
>>                if c == curses.KEY_F1 : raise keyf1
>>                if c == curses.KEY_F2 : raise keyf2
>>                if c == curses.KEY_F3 : raise keyf3
>>                if c == curses.KEY_F4 : raise keyf4
>>                curses.beep()
>>
>>win = something_to_initialize_curses_window()
>>
>>while True:
>>        try:
>>                c = getchar(win)
>>        except keyf1:
>>                process_keyF1()
>>        except (keyf2, keyf3):
>>                process_keyf2andf3()
>>        except keyf4:
>>                process keyf4()
>>        # ...
>>#end...
>>
>>Bill
>>--
>>INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
>>UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
>>FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 
>>236-1676
>>URL: http://www.celestial.com/
>>
>>``I have no reason to suppose that he, who would take away my Liberty, 
>>would
>>not when he had me in his Power, take away everything else.''  John Locke
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>

-- 
Bill
--
INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

``If you make yourselves sheep, the wolves will eat you'' -- Benjamin Franklin


More information about the Tutor mailing list