我收集了 700 名学生提交的约 300,000 段代码,并将其导入到 pandas 数据框中。
我的目标是对所有这些代码运行静态分析,以找出它们面临的错误类型。我试图做到这一点,而不必将这些写入实际文件(300k 文件)。
我尝试过的事情:
直接在数据框项上调用 pylint
pl.run_pylint(['--errors-only',dt.iloc[0][0]])
输出是
************* Module number = int(input('Please enter a number: '));
if number < 0 {
print(number*-1);
} else {
print(number);
}
number = int(input('Please enter a number: '));
if number < 0 {
print(number*-1);
} else {
print(number);
}:1:0: F0001: No module named number = int(input('Please enter a number: '));
if number < 0 {
print(number*-1);
} else {
print(number);
} (fatal)
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
而如果我将数据帧内容输出到文件中,然后运行
pl.run_pylint(['--errors-only','test.py'])
输出为
************* Module test
test.py:1:15: E0001: invalid syntax (<unknown>, line 1) (syntax-error)
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
我真的没有主意了。非常感谢任何帮助!!
最佳答案
我猜想可以以编程方式运行pylint
(例如使用run_pylint
方法),但在底层pylint
使用子进程来重新-将 lint 作为外部命令运行,因此我使用相同的方式将 pylint
作为外部命令运行:
from subprocess import run, PIPE
bad_code = '''
def a(a = 1):
print( a )
'''
result = run(['pylint', '--from-stdin', 'dummy.py'], input=bad_code.encode(), stdout=PIPE, stderr=PIPE)
print('STDOUT ------------------')
print(result.stdout.decode("utf8"))
print('STDERR ------------------')
print(result.stderr.decode("utf8"))
我们使用 --from-stdin
参数调用 pylint
作为外部命令,以便能够从 stdin 读取文件内容。
dummy.py
可以是任何免费文件名
输出是:
STDOUT ------------------
************* Module dummy
dummy.py:3:21: C0303: Trailing whitespace (trailing-whitespace)
dummy.py:3:0: W0311: Bad indentation. Found 1 spaces, expected 4 (bad-indentation)
dummy.py:1:0: C0114: Missing module docstring (missing-module-docstring)
dummy.py:2:0: C0103: Function name "a" doesn't conform to snake_case naming style (invalid-name)
dummy.py:2:6: C0103: Argument name "a" doesn't conform to snake_case naming style (invalid-name)
dummy.py:2:0: C0116: Missing function or method docstring (missing-function-docstring)
dummy.py:2:6: W0621: Redefining name 'a' from outer scope (line 2) (redefined-outer-name)
----------------------------------------------------------------------
Your code has been rated at -25.00/10 (previous run: -25.00/10, +0.00)
STDERR ------------------
关于python - 无文件静态分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72297001/