Marking translatable strings

Skip Montanaro skip at mojam.com
Tue Sep 21 22:26:24 EDT 1999


    > Wish there was a dictionary formatter that wouldn't throw a keyError
    > when it was missing a value. Then I could avoid the $& kluge. Better
    > yet accept an error handler function. If the function isn't provided
    > then throw the error.

How about something like (from memory):

    import UserDict
    class DefaultDict(UserDict.UserDict):
	  def __init__(self, default=""):
	      UserDict.UserDict.__init__(self)
	      self.default = default

	  def __getitem__(self, key):
	      return self.data.get(key, self.default)

?  Then you use it like

    d = DefaultDict("<default>")
    d.update(realdict)
    print "%(AA)s %(BB)s" % d

The extension to support an error function instead of a static default value
is straightforward and left as an exercise for the reader... ;-)

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/~skip/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list