[Edu-sig] Pythonic "case statement"
Dethe Elza
delza at livingcode.org
Thu Jun 15 03:08:42 CEST 2006
On 6/14/06, kirby urner <kirby.urner at gmail.com> wrote:
> Hi Bernie --
>
> Pythoneers (or Pythonistas as some say -- snake charmers) do have a
> case statement of sorts, thanks to functions (subprocedures) being top
> level.
>
[snipped]
I generally use a dict for this, mainly because my keys are more often
strings than
integers. For example, a skeleton for a CGI-based REST application:
def get(arg):
print 'responding to a GET'
def post(arg):
print 'responding to a POST'
def put(arg):
print 'responding to a PUT'
def delete(arg):
print 'responding to a DELETE'
def main():
import os
import cgi
method = os.environ.get('REQUEST_METHOD', 'GET')
arg = cgi.FieldStorage().getfirst('value')
# dispatch to the appropriate function
dict(GET=get, POST=post, PUT=put, DELETE=delete)[method](arg)
if __name__ == '__main__': main()
--Dethe
More information about the Edu-sig
mailing list