Tutorial Moodle VPL Gremlins -- Python

From dftwiki3
Revision as of 12:43, 11 June 2014 by Thiebaut (talk | contribs) (gremlin.py)
Jump to: navigation, search

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


MoodleVPLLogo.png


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.


VPL Activity Description


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.


MoodleVPLGremlinEvaluate.png





This concludes this tutorial