-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmt.py
42 lines (32 loc) · 912 Bytes
/
fmt.py
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
import inspect,re
def get_local_vars(tvar):
'''
extract only simple local variables from the frame
'''
dic = {}
allowed = [str,int,float,bool]
for k,v in tvar.iteritems():
if type(k) != str: continue
if not type(v) in allowed: continue
dic[k] = v
del tvar
return dic
def substitute_values(data,dic):
'''
substitute values for place holders
'''
#replace {key} with value
for key in dic.iterkeys():
if not '{%s}'%key in data: continue
data = data.replace('{%s}'%key, str(dic[key]))
#check all placeholders were replaced
for m in re.finditer('{.*}',data):
print m.group(0), 'not matched'
raise Exception
return data
def fmt(s):
'''
fill out place holders
'''
dic = get_local_vars(inspect.currentframe().f_back.f_locals)
return substitute_values(s,dic)