减少嵌套循环中re.sub()的处理时间

时间:2019-04-19 04:16:08

标签: python regex recursion preprocessor mutual-recursion

我正在预处理一组文件,其中一个文件包含在另一个文件中,并带有如下所示的include标签:

文件A

include file B
include file C
 contents of file A
include file D
 contents of file A

在这里,我需要用每个文件内容替换include标签。就像编译器一样。我有两个功能

    def parseContent(self, rdbFilePath, content):
        finalContent = self.removeComments(content)
        includeFileSearch = re.compile(r'(?P<tag>(\s)*include+(\s*)\"(\s*)(?P<file>[a-zA-Z0-9\.\_/]*)\")')
        for includes in includeFileSearch.finditer(finalContent):
            finalContent = re.sub(includes.group('tag'),self.parseIncludes(rdbFilePath, includes.group('file')), finalContent)
        return finalContent

    def parseIncludes(self, rdbFilePath, file):
        path = rdbFilePath + "/" + file
        f = open(path)
        pathDir = os.path.dirname(path)
        includedFileContent = self.parseContent(pathDir, f.read())
        return includedFileContent

如您所见,函数parseContentparseIncludes相互递归调用,以替换每个文件中的所有include标记。逻辑工作正常。但是执行需要一些时间。有没有什么更好的方法可以缩短执行时间呢?

0 个答案:

没有答案