Marking translatable strings

Darrell news at dorb.com
Wed Sep 22 00:59:28 EDT 1999


Good suggestion Skip. It works with one detractor. It has to assume the ")s"
is the trailing format type. From with in PyString_Format this wasn't a
problem.

import UserDict
class DefaultDict(UserDict.UserDict):
    def __init__(self, dict):
        UserDict.UserDict.__init__(self, dict)

    def __getitem__(self, key):
        return self.data.get(key,'%('+key+')s')

d1=DefaultDict({'AA':'aa'})
d2=DefaultDict({'BB':'bb'})
d3=DefaultDict({'CC':'cc'})

s="%(AA)s %(BB)s %(CC)s"%d1
print s
s=s%d2
print s
s= s%d3
print s

###############
aa %(BB)s %(CC)s
aa bb %(CC)s
aa bb cc

--
--Darrell
Skip Montanaro <skip at mojam.com> wrote in message
news:14312.15952.824486.865824 at dolphin.mojam.com...
>     > 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