>>>>> "SM" == Steven Majewski <sdm7g@Virginia.EDU> 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 '<Missing key: %s>' % `key` def _(s): frame = sys._getframe(1) d = SafeDict(frame.f_globals.copy()) d.update(frame.f_locals) return s % d BIRD = 'parrot' def examples(thing): bird = 'dead ' + BIRD print _('It used to be a %(BIRD)s') print _('But now it is a %(bird)s') print _('%(BIRD)s or %(bird)s?') print _('You are not %(morg)s, you are not %(imorg)s') print _('%(thing)s, %(thing)s, what is %(thing)s?') examples(sys.argv[1]) -------------------- snip snip -------------------- % python /tmp/dollar.py brain It used to be a parrot But now it is a dead parrot parrot or dead parrot? You are not %(morg)s, you are not %(imorg)s brain, brain, what is brain?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4