【问题标题】:Python Coverity Complexity PrintPython Coverity 复杂性打印
【发布时间】:2018-01-04 21:25:40
【问题描述】:

我使用的是 Synopsys 创建的 Coverity,主要由静态代码分析和动态代码分析工具组成。我目前正在针对我的代码运行它,因此它会生成一个 .xml 文件。

.xml 包含数字字符串,每个数字都有自己的含义。我想制作一个 Python33 脚本来打印直接映射到函数复杂性的第七个数字。我可以很好地将它打印到控制台,但由于我是 C 编码器,而且不仅是 Python 中的一部分,这就是我所拥有的:

*.xml 的行示例:

    <fnmetric>
    <file>FILE1.C</file>
    <fnmet>function1;1;12;10;21;8;9;5;1441.75;0.0557199;;;318</fnmet>
    </fnmetric>
    <fnmetric>
    <file>FILE2.C</file>
    <fnmet>function2;0;0;1;11;8;3;1;184.638;0.0175846;;;352</fnmet>
    </fnmetric>

所以第一个函数的第 7 个数字是 9,第二个是 3

到目前为止我有:

import fileinput
import glob
import re
import sys

#Read in all files in the directory ending in .xml
files = glob.glob('*.xml')
#search for the line beginning in <fnmet> and ending in </fnmet>
pattern = re.compile(r'<fnmet>\s+;([_A-Z]+)</fnmet>\s+$') #this line is wrong
realstdout = sys.stdout
#create .bak files of the .h files unchanged (backups)
for line in fileinput.input(files, inplace=True, backup='.bak'):
    sys.stdout.write(line)
    m = pattern.match(line)
    #print the 7th #; as  function + complextity through all .xml lines. 
    if m:
        sys.stdout.write('\n <fnmet>\%s</fnmet>\n' % m.group(1)) #this line is wrong
        realstdout.write('%s: %s\n'%(fileinput.filename(),m.group(1))) #this line is wrong

基本上我只是想在 function_name 之后打印第 6 个数字;

类似:

函数1:9 函数2:3

感谢任何帮助,我的 python 太可怕了。

【问题讨论】:

标签: python xml python-3.3 coverity


【解决方案1】:

看来您的问题在于正则表达式模式。看看这个样本:

import re

source= '<fnmet>function1;1;12;10;21;8;9;5;1441.75;0.0557199;;;318</fnmet>'

pattern = re.compile(r'(\<fnmet\>)(\S+)(\<\/fnmet\>)')

match = re.findall(pattern, source)
result = match[0][1].split(';')

# result now holds
# ['function1', '1', '12', '10', '21', '8', '9', '5',
#   '1441.75', '0.0557199', '', '', '318']

func_name = 0
pos = 6
print('{}: {}'.format(result[func_name], result[pos]))

这将输出:

function1: 9

我认为使用这个正则表达式和字符串拆分将很容易解决您的问题,因为您将能够索引列中的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2018-04-09
    相关资源
    最近更新 更多