"SM" == Steven Majewski
writes:
SM> Since PEP 216 on string interpolation is still active, I'ld
SM> appreciate it if some of it's supporters would comment on my
SM> revised alternative solution (posted on comp.lang.python and
SM> at google thru):
[Steve's EvalDict]
For completeness, here's a simplified version of Mailman's _()
function which does auto-interpolation from locals and globals of the
calling context. This version works in Python 2.1 or beyond and has
the i18n translation stuff stripped out. For the full deal, see
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/mailman/mailman/Mailman/i18n.py?rev=2.4&content-type=text/plain
Cheers,
-Barry
-------------------- snip snip --------------------dollar.py
import sys
from UserDict import UserDict
from types import StringType
class SafeDict(UserDict):
"""Dictionary which returns a default value for unknown keys."""
def __getitem__(self, key):
try:
return self.data[key]
except KeyError:
if isinstance(key, StringType):
return '%('+key+')s'
else:
return '