Tutorial Moodle VPL Gremlins -- Python

From dftwiki
Jump to: navigation, search

--D. Thiebaut (talk) 12:33, 11 June 2014 (EDT)


Moodle VPL Tutorials

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

  1. ask the user for the name of a file (that must exists)
  2. open the file, read it, and replace all g- and G-letters with gremlin or Gremlin, and
  3. 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. Version 1 of the activity uses 2 python programs, the one submitted by the student, and a second one used to run the student's program. In this version the student can see both programs. In Version 2, the second python program is hidden from the student and included in the Execution files collection. The second version is preferable.


  • Moodle Version 2.7 + (Build: 20140529)
  • VPL Version 3.0.1
  • For details on how Moodle and VPL were installed, go to this page.

VPL Activity Description: Version 1


This program is provided and should NOT be modified by the student.

# 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!
""" )

#--- 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" )

#--- 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="" )


This is the program that the student must complete.

# Enter your code below (and remove this line!)

Execution files


#! /bin/bash
echo "#! /bin/bash"  > vpl_execution
echo "python3.4 runGremlin.py" >> vpl_execution
chmod +x vpl_execution


case = Test 1

output = "Gremlinreat news!
The egremlingremlins are gremlinreatly reduced today.
Gremlinood day!


  • 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()

text = text.replace( 'g', 'gremlin' )
text = text.replace( 'G', 'Gremlin' )

file = open( fileName, "w" )
file.write( text )

  • 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