[Tutor] using a dictionary??

Kent Johnson kent37 at tds.net
Thu Apr 21 05:20:09 CEST 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')
> else:
>  print "Unknown News Database!",
>  print "%s" % db
> return printed
> 
> This script works fine as is, no problem... BUT
> 
> 
> 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...

Yes, something like

headlines = {
   "organicnews":'Organics in the News',
   "ovnews":'Our Stuff in the News',
   "pressreleases":'Press Releases',
}

db = request.db
head = headlines.get(db, "Unknown News Database!")
print "%s" % html_quote(head)

Kent



More information about the Tutor mailing list