Marking translatable strings
Barry A. Warsaw
bwarsaw at cnri.reston.va.us
Wed Sep 22 10:51:11 EDT 1999
>>>>> "D" == Darrell <news at dorb.com> writes:
D> Wish there was a dictionary formatter that wouldn't throw a
D> keyError when it was missing a value.
A while back I posted what we use in Mailman:
-------------------- snip snip --------------------
from UserDict import UserDict
from types import StringType
class SafeDict(UserDict):
"""Dictionary which returns a default value for unknown keys.
"""
def __init__(self, d):
# optional initial dictionary is a Python 1.5.2-ism. Do it this way
# for portability
UserDict.__init__(self)
self.update(d)
def __getitem__(self, key):
try:
return self.data[key]
except KeyError:
if type(key) == StringType:
return '%('+key+')s'
else:
return '<Missing key: %s>' % `key`
-------------------- snip snip --------------------
>>> 'Well %(hello)s there %(name)s' % sd
'Well howdy there %(name)s'
Hope that helps,
-Barry
More information about the Python-list
mailing list