'static analysis without a file
I some 300,000 snippets of code, submitted by 700 students, which I have imported into a pandas data frame.
My goal is to run static analysis on all of these codes to find out what sort of errors they are facing. And I'm trying to do it without having to write these to actual files (300k files).
things I have tried :
directly calling pylint on data frame item
pl.run_pylint(['--errors-only',dt.iloc[0][0]])
and output is
************* 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
whereas if I output the dataFrame content to a file, and run
pl.run_pylint(['--errors-only','test.py'])
the output is
************* 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
I'm actually out of ideas. Any help is greatly appreciated!!
Solution 1:[1]
I guess that is possible to run pylint programatically (eg. using run_pylint method), but under the hood pylint using subprocess to re-run lint as external command, so I used same way to run pylint as external command:
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"))
We call pylint as external command with --from-stdin parameter to be able read file contents from stdin.dummy.py may be any free filename
Output is:
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 ------------------
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rzlvmp |
