Sch*_*hof 7 python regex algorithm optimization
我的目标是创建一个非常简单的模板语言.目前,我正在使用值替换变量,如下所示:
这个输入:
网络
应该产生这个输出:
Web这是一个测试变量
我有它的工作.但是看着我的代码,我在相同的字符串上运行多个相同的正则表达式 - 这只是冒犯了我的效率感.必须有更好,更Pythonic的方式.(这是真正冒犯的两个"while"循环.)
这确实通过了单元测试,所以如果这是愚蠢的过早优化,请告诉我 - 我愿意放手.在文档中可能有许多这些变量定义和用法,但不是数百个.但我怀疑(对其他人)显而易见的改善方法,我很好奇StackOverflow人群会想出什么.
def stripMatchedQuotes(item):
MatchedSingleQuotes = re.compile(r"'(.*)'", re.LOCALE)
MatchedDoubleQuotes = re.compile(r'"(.*)"', re.LOCALE)
item = MatchedSingleQuotes.sub(r'\1', item, 1)
item = MatchedDoubleQuotes.sub(r'\1', item, 1)
return item
def processVariables(item):
VariableDefinition = re.compile(r'<%(.*?)=(.*?)%>', re.LOCALE)
VariableUse = re.compile(r'<%(.*?)%>', re.LOCALE)
Variables={}
while VariableDefinition.search(item):
VarName, VarDef = VariableDefinition.search(item).groups()
VarName = stripMatchedQuotes(VarName).upper().strip()
VarDef = stripMatchedQuotes(VarDef.strip())
Variables[VarName] = VarDef
item = VariableDefinition.sub('', item, 1)
while VariableUse.search(item):
VarName = stripMatchedQuotes(VariableUse.search(item).group(1).upper()).strip()
item = VariableUse.sub(Variables[VarName], item, 1)
return item
Run Code Online (Sandbox Code Playgroud)
Bri*_*ian 10
可能改进的第一件事是将re.compile移到函数之外.编译是缓存的,但检查它是否有速度命中,以查看它是否已编译.
另一种可能性是使用单个正则表达式如下:
MatchedQuotes = re.compile(r"(['\"])(.*)\1", re.LOCALE)
item = MatchedQuotes.sub(r'\2', item, 1)
Run Code Online (Sandbox Code Playgroud)
最后,您可以将其组合到processVariables中的正则表达式中.考虑到Torsten Marek建议使用re.sub函数,这可以显着改善和简化事物.
VariableDefinition = re.compile(r'<%(["\']?)(.*?)\1=(["\']?)(.*?)\3%>', re.LOCALE)
VarRepl = re.compile(r'<%(["\']?)(.*?)\1%>', re.LOCALE)
def processVariables(item):
vars = {}
def findVars(m):
vars[m.group(2).upper()] = m.group(4)
return ""
item = VariableDefinition.sub(findVars, item)
return VarRepl.sub(lambda m: vars[m.group(2).upper()], item)
print processVariables('<%"TITLE"="This Is A Test Variable"%>The Web <%"TITLE"%>')
Run Code Online (Sandbox Code Playgroud)
以下是我100000次运行的时间:
Original : 13.637
Global regexes : 12.771
Single regex : 9.095
Final version : 1.846
Run Code Online (Sandbox Code Playgroud)
[编辑]添加缺少的非贪婪说明符
[Edit2]添加.upper()调用,使其不像原始版本那样不区分大小写