python - static analysis 没有文件

我已将 700 名学生提交的大约 300,000 段代码导入到 pandas 数据框中。

我的目标是在所有这些代码上运行 static analysis 以找出它们面临的错误类型。我正在尝试这样做,而不必将这些写入实际文件(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

我实际上没有想法。任何帮助是极大的赞赏!!

回答1

我想可以以编程方式运行 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 参数的外部命令,以便能够从标准输入读取文件内容。

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

相似文章

随机推荐

最新文章