我编写了一个简单的程序来计算 Java 项目中某些特定包的测试覆盖率的平均值。一个巨大的 html 文件中的原始数据是这样的:
<body>
package pkg1 <line_coverage>11/111,<branch_coverage>44/444<end>
package pkg2 <line_coverage>22/222,<branch_coverage>55/555<end>
package pkg3 <line_coverage>33/333,<branch_coverage>66/666<end>
...
</body>
例如,给定指定的包“pkg1”和“pkg3”,平均线路覆盖率为:
(11+33)/(111+333)
平均分支覆盖率是:
(44+66)/(444+666)
我编写了以下程序来获得结果,并且效果很好。但是如何以函数式的方式实现这个计算呢?类似于“(x,y) for x in ... for b in ... if ...”。我知道一点 Erlang、Haskell 和 Clojure,所以这些语言的解决方案也很受欢迎。非常感谢!
from __future__ import division
import re
datafile = ('abc', 'd>11/23d>34/89d', 'e>25/65e>13/25e', 'f>36/92f>19/76')
core_pkgs = ('d', 'f')
covered_lines, total_lines, covered_branches, total_branches = 0, 0, 0, 0
for line in datafile:
for pkg in core_pkgs:
ptn = re.compile('.*'+pkg+'.*'+'>(\d+)/(\d+).*>(\d+)/(\d+).*')
match = ptn.match(line)
if match is not None:
cvln, tlln, cvbh, tlbh = match.groups()
covered_lines += int(cvln)
total_lines += int(tlln)
covered_branches += int(cvbh)
total_branches += int(tlbh)
print 'Line coverage:', '{:.2%}'.format(covered_lines / total_lines)
print 'Branch coverage:', '{:.2%}'.format(covered_branches/total_branches)