代码实现
# -*- coding: utf-8 -*- import re class Template(object): pattern = r"\{(\w+)\}" def __init__(self, template): self.template = template self.re_pattern = re.compile(self.pattern) def compile(self, **kwargs): def foo(mo): return kwargs[mo.group(1)] return self.re_pattern.sub(foo, self.template) if __name__ == '__main__': t = Template('/student/{uid}/{name}') data = { "uid": '1', "name": "Tom" } print(t.compile(**data)) # /student/1/Tom