blob: cdf7c3ee27ff391db65aa573f918a1a339304519 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# interp.py
import UserDict
class Chainmap(UserDict.DictMixin):
"""Combine multiple mappings for sequential lookup. Raymond Hettinger,
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305268 """
def __init__(self, *maps):
self._maps = maps
def __getitem__(self, key):
for mapping in self._maps:
try:
return mapping[key]
except KeyError:
pass
raise KeyError(key)
import sys
from string import Template
def interp(text, repldic=None, safe_substitute=True):
caller = sys._getframe(1)
if repldic:
mapping = Chainmap(repldic, caller.f_locals, caller.f_globals)
else:
mapping = Chainmap(caller.f_locals, caller.f_globals)
t = Template(text)
if safe_substitute:
return t.safe_substitute(mapping)
else:
return t.substitute(mapping)
## Example:
language="Python"
def printmsg():
opinion = "favorite"
print interp("My $opinion language is $language.")
|