不带文件的静态分析



我将700名学生提交的大约30万段代码导入到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

而如果我输出dataFrame内容到一个文件,并运行

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"))

我们调用pylint作为外部命令,--from-stdin参数可以从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 ------------------

最新更新