recipe-146460.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:code 作者: ActiveState 项目源码 文件源码
def parse(self, dict, header=TRUE):
        """parse(dict) -> string
        This method parses the open file object passed, replacing any keys
        found using the replacement dictionary passed."""
        if type(dict) != types.DictType:
            raise TypeError, "Second argument must be a dictionary"
        if not self.template:
            raise OpagMissingPrecondition, "template path is not set"
        # Open the file if its not already open. If it is, seek to the
        # beginning of the file.
        if not self.template_file:
            self.template_file = open(self.template, "r")
        else:
            self.template_file.seek(0)
        # Instantiate a new bound method to do the replacement.
        replacer = Replacer(dict).replace
        # Read in the entire template into memory. I guess we'd better keep
        # the templates a reasonable size if we're going to keep doing this.
        buffer = self.template_file.read()
        replaced = ""
        if header:
            replaced = "Content-Type: text/html\n\n"
        replaced = replaced + re.sub("%%(\w+)%%", replacer, buffer)
        return replaced
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号