Easy "here documents" ??
Fredrik Lundh
fredrik at pythonware.com
Sun Dec 19 18:08:14 EST 2004
Scott David Daniels wrote:
> And if you enjoy building insecure stuff, try:
>
> def fix(text, globals_=None, locals=None, quote='"'):
> d = (globals_ or locals or globals()).copy()
> source = text.split(quote)
> source[1::2] = (str(eval(expr, d, locals or d))
> for expr in source[1::2])
> return ''.join(source)
>
>
> amount = 1
> cost = 2.0
> what = 'potato'
> print fix("""I'll have "amount" "what"s
> for "'$%.2f' % cost"s please""", locals())
And if you prefer not to type so much:
def I(*args): return "".join(map(str, args))
def F(v, fmt): return ("%" + fmt) % v
print I("I'll have ", amount, " ", what, "s for $", F(cost, ".2f"), "s please")
</F>
More information about the Python-list
mailing list