📄 listing10-11.py
字号:
# templates.pyimport fileinput, re# Matches fields enclosed in square brackets:field_pat = re.compile(r'\[(.+?)\]')# We'll collect variables in this:scope = {}# This is used in re.sub:def replacement(match): code = match.group(1) try: # If the field can be evaluated, return it: return str(eval(code, scope)) except SyntaxError: # Otherwise, execute the assignment in the same scope... exec code in scope # ...and return an empty string: return ''# Get all the text as a single string:# (There are other ways of doing this; see Chapter 11)lines = []for line in fileinput.input(): lines.append(line)text = ''.join(lines)# Substitute all the occurrences of the field pattern:print field_pat.sub(replacement, text)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -