-1

I'm trying to find a nice way to automate grading some students' code. Each of the students is going to submit a file with a name something like homework1_studentName.py, and I'm going to download them all and put them in a folder.

Now each student should have a bunch of functions like def question1(args): in their file that I'm trying to grade. My goal is to make a grading script in that directory that iterates through each student's module, imports the code, runs the functions against some predefined test cases and then prints the output. Is there a way to do this nicely?

It'd save a ton of time to just run the grading script once (after downloading all of their code) than to have to grade each file separately.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
timisplump
  • 33
  • 4

1 Answers1

0

Once you import the module, you can access the functions dynamically using strings as follows:

import students_code

def run(function_name, module_name):
  getattr(module_name, function_name)()
  #this will call module_name.function_name()

run("question_1", students_code)

Maybe if you then had a config file for each assignment, you could automatically run things. You could also loop through dir(the_module) with this run function.

For testing their code automatically, check out: https://docs.python.org/2/library/unittest.html

Heman Gandhi
  • 1,343
  • 9
  • 12