[Tutor] using a dictionary??

Bob Gailer bgailer at alum.rpi.edu
Thu Apr 21 05:22:14 CEST 2005

At 06:56 PM 4/20/2005, PFraterdeus wrote:
>Here'a little py script for a plone page template...
>I just want to translate the 'key' (organicnews) to a 'value' (Organics in the
>News), for presentation purposes. (the key, of course, is in the URL request)
>db = request.db
>if db=="organicnews":
>  print "%s" % html_quote('Organics in the News')
>elif db=="ovnews":
>  print "%s" % html_quote('Our Stuff in the News')
>elif db=="pressreleases":
>  print "%s" % html_quote('Press Releases')
>  print "Unknown News Database!",
>  print "%s" % db
>return printed

Use a dictionary:

responses = {"organicnews": "Organics in the News'", "ovnews" : "'Our Stuff 
in the News'", "pressreleases" : "'Press Releases'"}
print html_quote(responses.get(db, "Unknown News Database! " + db))

BTW there is no benefit to print '%s' % blahblah. print blahblah does the 
same thing.

>I know there's a proper way to do this with a tuple, or something :)
>For the three values, it's not a problem, but if I want to scale up to many, I
>imagine the dictionary is the way to go...

Bob Gailer
mailto:bgailer at alum.rpi.edu
510 558 3275 home
720 938 2625 cell  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050420/072cda0d/attachment.html

More information about the Tutor mailing list