我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用pygments.util.format_lines()。
def update_consts(filename, constname, content): with open(filename) as f: data = f.read() # Line to start/end inserting re_match = re.compile(r'^%s\s*=\s*\($.*?^\s*\)$' % constname, re.M | re.S) m = re_match.search(data) if not m: raise ValueError('Could not find existing definition for %s' % (constname,)) new_block = format_lines(constname, content) data = data[:m.start()] + new_block + data[m.end():] with open(filename, 'w') as f: f.write(data)
def regenerate(filename, natives): with open(filename) as fp: content = fp.read() header = content[:content.find('FUNCTIONS = (')] footer = content[content.find("if __name__ == '__main__':")-1:] with open(filename, 'w') as fp: fp.write(header) fp.write(format_lines('FUNCTIONS', natives)) fp.write(footer)