Tutorial Moodle VPL Gremlins -- Python
--D. Thiebaut (talk) 12:33, 11 June 2014 (EDT)
This tutorial builds on the first one in the series, which should be done first, as the present tutorial skips some steps. In this tutorial we setup a VPL activity that tests a Python program that is supposed to
- ask the user for the name of a file (that must exists)
- open the file, read it, and replace all g- and G-letters with gremlin or Gremlin, and
- save the new content in the original file.
We perform the evaluation and automatic grading of the program by using a second Python program that creates the sample text file and feeds its name via a redirected stdin to the student program.
VPL Activity Description: Version 1
runGremlin.py
This program is provided and should NOT be modified by the student.
#runGremlin.py # students, do not modify this file. It will be used to test # your program. Instead, copy/paste or enter your code in # gremlin.py (next tab over) import sys #--- create a sample text file to use in the test --- file = open( "sample.txt", "w" ) file.write( """Great news! The eggs are greatly reduced today. Good day! """ ) file.close() #--- put the name of the sample file into another text file that #--- will serve as stdin for the program to test file = open( "fakestdin.txt", "w" ) file.write( "sample.txt\n" ) file.close() #--- make stdin read information from the text file file = open( "fakestdin.txt", "r" ) sys.stdin = file #--- capture the stdout of the program to test into a file saveStdOut = sys.stdout sys.stdout = open( "fakestdout.txt", "w" ) #--- run the program to test --- import gremlin #--- get rid of whatever it printed on the stdout --- sys.stdout = saveStdOut #--- display the contents of the sample.txt file which should #--- have been modified by the program under test. file = open( "sample.txt", "r" ) print( file.read(), end="" ) file.close()
gremlin.py
This is the program that the student must complete.
# Enter your code below (and remove this line!)
Execution files
vpl_run.sh
#! /bin/bash echo "#! /bin/bash" > vpl_execution echo "python3.4 runGremlin.py" >> vpl_execution chmod +x vpl_execution
vpl_evaluate.cases
case = Test 1 output = "Gremlinreat news! The egremlingremlins are gremlinreatly reduced today. Gremlinood day! "
Testing
- In the VPL Administration box, click on Test activity, Edit, and enter this code for gremlin.py:
# Enter your code below (and remove this line!) fileName=input( "File name? " ) file = open( fileName, "r" ) text = file.read() file.close() text = text.replace( 'g', 'gremlin' ) text = text.replace( 'G', 'Gremlin' ) file = open( fileName, "w" ) file.write( text ) file.close()
- Click on Evaluate and verify that the program passes the test.
VPL Activity Description: Version 2
In this version the runGremlin.py program is hidden from the student and included in the Execution files of the VPL maintenance module.
- Set the number of files the student has to upload to 1, and make the file name gremlin.py. In the Execution files window of the VPL maintenance module, add a new file along with vpl_run.sh, vpl_debug.sh, vpl_evaluate.sh, and vpl_evaluate.cases: runGremlin.py.
- Set its contents to the same contents as in Version 1 above.
- The vpl_run.sh does not change, and still reads:
#! /bin/bash echo "#! /bin/bash" > vpl_execution echo "python3.4 runGremlin.py" >> vpl_execution chmod +x vpl_execution
The adventage of this method is that the student is unaware that there is a second python program, and therefore has fewer chances of getting confused.
This concludes this tutorial