This is a Python script that can be used to grade Python code. It also shows how to run Pylint from an auto-grading script. This script would be called from an Advanced Code Test and it is set up to return partial points.
import os, requests, random, re, io, subprocess, shutil, sys
from subprocess import Popen, PIPE, STDOUT
sys.path.append('/usr/share/codio/assessments')
from lib.grade import send_partial_v2, FORMAT_V2_MD, FORMAT_V2_HTML, FORMAT_V2_TXT
score = 0
feedback = ""
## check function of code using output - keyboard flag to indicate input type
def check_output(file, arguments, expected_output, keyboard=False):
expected_output = expected_output.rstrip('\x00')
if keyboard:
#https://stackoverflow.com/questions/33976094/subprocess-stdin-input
p = Popen(['python3', file], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
student_output = p.communicate(input=str.encode(arguments))[0]
else:
student_output = subprocess.check_output(['python3', file]).rstrip()
#check generated output
if student_output.decode("utf-8").strip() == expected_output:
return True
else:
return False
## Run the test cases
result = check_output('filename.py',"Input here", "Output_here\n",True)
if result == True:
score +=10 # functionality matched
else:
feedback+="Your output does not match the expected output.\n"
feedback+="<br><br><h3>Pylint test</h3>\n"
pylint_output = subprocess.check_output(['pylint', 'filename.py', '--disable=all', '--enable=invalid-name', '--const-naming-style=any', '-s', 'n']).rstrip()
if pylint_output.decode("utf-8").strip() == '':
score+=6
else:
feedback+='pylint should report no errors on variable names.\n'
feedback+= "<h2>On this question you earned " + str(score) + " out of 10</h2>\n"
possible = 10
percent = (score/possible)*100
res = send_partial_v2(percent, feedback, FORMAT_V2_HTML)
exit(0 if res else 1)