[ANN] istring 1.0.1 released; announce list created
Kirill Simonov
kirill_simonov at mail.ru
Fri Mar 8 12:06:44 EST 2002
* Steven D. Arnold <stevena at neosynapse.net>:
> 2. Some have asked why one would not use vars() and the mapping format
> all the time, since vars returns a dictionary of the local namespace?
> The problem with vars() is that (at least as far as I know) it doesn't
> support dictionaries, lists, object attributes and function calls.
### xstring.py ###
import sys
class XDict:
def __init__(self, globals, locals):
self.globals = globals
self.locals = locals
def __getitem__(self, key):
return eval(key, self.globals, self.locals)
def xstring(string):
frame = sys._getframe(1)
return string % XDict(frame.f_globals, frame.f_locals)
def test():
import math
x = y = 2
s1 = "%(x)s + %(y)s = %(x+y)s"
s2 = "sin(pi/4) = %(math.sin(math.pi/4))0.4f"
s3 = "Python-%(sys.version[:3])s"
for s in [s1, s2, s3]:
print "xstring(%r) == %r" % (s, xstring(s))
if __name__ == '__main__':
test()
### EOF ###
--
xi
More information about the Python-list
mailing list