Difference between revisions of "CSC111 Lab 11 2015"

From dftwiki3
Jump to: navigation, search
(Undo revision 26171 by Thiebaut (talk))
Line 759: Line 759:
 
</showafterdate>
 
</showafterdate>
 
<br />
 
<br />
 +
<onlydft>
 +
=VPL=
 +
 +
==vpl_run.sh==
 +
<br />
 +
<source lang="bash">
 +
#! /bin/bash
 +
 +
cat > vpl_execution <<EOF
 +
#! /bin/bash
 +
 +
# --- Python ----
 +
if [[ `hostname -s` = "beowulf2" ]]; then
 +
  python=/usr/bin/python3.3
 +
else
 +
  python=/usr/local/bin/python3.4
 +
fi
 +
 +
prog=lab11_4.py
 +
 +
\$python \$prog
 +
 +
 +
 +
EOF
 +
 +
chmod +x vpl_execution
 +
 +
 +
</source>
 +
<br />
 +
==vpl_debug.sh==
 +
<br />
 +
<source lang="bash">
 +
</source>
 +
<br />
 +
==vpl_evaluate.sh==
 +
<br />
 +
<source lang="bash">
 +
#! /bin/bash
 +
 +
cat  > vpl_execution <<EOF
 +
#! /bin/bash
 +
 +
# --- Python ----
 +
if [[ `hostname -s` = "beowulf2" ]]; then
 +
  python=/usr/bin/python3.3
 +
else
 +
  python=/usr/local/bin/python3.4
 +
fi
 +
 +
 +
\$python evaluate.py
 +
 +
EOF
 +
 +
chmod +x vpl_execution
 +
 +
 +
</source>
 +
<br />
 +
==vpl_evaluate.cases==
 +
<br />
 +
<source lang="text">
 +
</source>
 +
<br />
 +
==evaluate.py==
 +
<br />
 +
<source lang="python">
 +
# evaluate.py
 +
 +
listPres="""Clinton 2
 +
Ford 1
 +
Jefferson 2
 +
Arthur 1
 +
Taylor 1
 +
Roosevelt 4
 +
Taft 1
 +
Nixon 1
 +
Tyler 1
 +
Jackson 2
 +
Wilson 2
 +
Monroe 2
 +
Harding 1
 +
Eisenhower 1
 +
Adams 5
 +
Madison 2
 +
Buren 2
 +
Garfield 1
 +
Kennedy 3
 +
McKinley 1
 +
Hayes 1
 +
Bush 2
 +
Washington 1
 +
Reagan 1
 +
Cleveland 4
 +
Johnson 4
 +
Harrison 2
 +
Hoover 1
 +
Lincoln 1
 +
Grant 1
 +
Pierce 2
 +
Truman 3
 +
Coolidge 2
 +
Buchanan 2
 +
Fillmore 1
 +
Polk 2
 +
Carter 2"""
 +
 +
expectedOutputText = """ALPHABETICAL LIST
 +
Abraham Lincoln
 +
Andrew Jackson
 +
Andrew Johnson
 +
Benjamin Harrison
 +
Bill Clinton
 +
Calvin Coolidge
 +
Chester A. Arthur
 +
Dwight D. Eisenhower
 +
Franklin D. Roosevelt
 +
Franklin Pierce
 +
George H. W. Bush
 +
George W. Bush
 +
George Washington
 +
Gerald Ford
 +
Grover Cleveland
 +
Grover Cleveland
 +
Harry S. Truman
 +
Herbert Hoover
 +
James A. Garfield
 +
James Buchanan
 +
James K. Polk
 +
James Madison
 +
James Monroe
 +
Jimmy Carter
 +
John Adams
 +
John F. Kennedy
 +
John Quincy Adams
 +
John Tyler
 +
Lyndon B. Johnson
 +
Martin Van Buren
 +
Millard Fillmore
 +
Richard Nixon
 +
Ronald Reagan
 +
Rutherford B. Hayes
 +
Theodore Roosevelt
 +
Thomas Jefferson
 +
Ulysses S. Grant
 +
Warren G. Harding
 +
William Henry Harrison
 +
William Howard Taft
 +
William McKinley
 +
Woodrow Wilson
 +
Zachary Taylor
 +
 +
ALPHABETICAL LIST OF DEMOCRATS
 +
Andrew Jackson
 +
Andrew Johnson
 +
Bill Clinton
 +
Franklin D. Roosevelt
 +
Franklin Pierce
 +
Grover Cleveland
 +
Grover Cleveland
 +
Harry S. Truman
 +
James Buchanan
 +
James K. Polk
 +
James Madison
 +
James Monroe
 +
Jimmy Carter
 +
John F. Kennedy
 +
John Quincy Adams
 +
Lyndon B. Johnson
 +
Martin Van Buren
 +
Thomas Jefferson
 +
Woodrow Wilson
 +
 +
PRESIDENT IN OFFICE IN 1945
 +
Franklin D. Roosevelt
 +
Harry S. Truman
 +
 +
PRESIDENT FROM MASSACHUSETTS
 +
Calvin Coolidge
 +
John Adams
 +
John F. Kennedy
 +
John Quincy Adams
 +
 +
"""
 +
# evaluate.py
 +
# D. Thiebaut
 +
# This program is used to test a student's python program on Moodle.
 +
 +
import sys
 +
import random
 +
import subprocess
 +
 +
#--- GLOBALS ---
 +
#--- define what the student program is called, and what the solution
 +
#--- program name is.
 +
 +
 +
module = "lab11_4"
 +
solutionModule = module + "sol"
 +
userOutSplitPattern = ""      # pattern used to start recording the user
 +
                              # output.  Useful when program does several
 +
                              # input() statements, and user output starts
 +
                              # after that.
 +
stripOutputsBeforeCompare = True
 +
                              # set to True if extra spaces at beginning or
 +
                              # end of user output is ok
 +
stripDoubleSpaceBeforeCompare = True
 +
                              # set to True if double spaces must be removed in
 +
                              # outputs
 +
interpreter = sys.executable
 +
 +
def commentLong( line ):
 +
    print( "<|--\n" + line  + "\n --|>" )
 +
 +
def commentShort( text ):
 +
    print( "Comment :=>> " + text )
 +
 +
def printGrade( grade ):
 +
    print( "Grade :=>> ", grade )
 +
 +
# generateInputFileWithRandomInputs
 +
# generate a file with name "inputFileName" with some random input
 +
# for the program.
 +
# MAKE SURE TO EDIT THIS TO MATCH THE PROGRAM BEING TESTED
 +
def generateInputFileWithRandomInputs( inputFileName ):
 +
    #--- we don't need an input file for stdin, but we'll generate a
 +
    #--- dummy one nonetheless
 +
    #--- generate random inputs ---
 +
    return ""
 +
 +
# removeIf__name__: re
 +
def removeIf__name__( moduleName  ):
 +
 +
    file = open( moduleName + ".py", "r" )
 +
    lines = file.readlines()
 +
    file.close()
 +
 +
    newLines = []
 +
    for line in lines:
 +
        if line.find( "if __name__" ) != -1:
 +
            line = "if True:\n"
 +
        newLines.append( line.rstrip() )
 +
 +
    file = open( moduleName + ".py", "w" )
 +
    file.write( "\n".join( newLines ) )
 +
    file.close()
 +
 +
# checkForFunctionPresence
 +
# checks that "functionName" is defined and called in the program.
 +
# MAKE SURE TO EDIT TO MATCH PROGRAM BEING TESTED
 +
def checkForFunctionPresence( module, functionName ):
 +
    foundDef = False
 +
    foundCall = False
 +
 +
    for line in open( module+".py", "r" ).readlines():
 +
        # remove comments
 +
        idx = line.find( "#" )
 +
        if ( idx >=0 ): line = line[0:idx]
 +
 +
        if line.startswith( "def " + functionName + "(" ):
 +
            foundDef = True
 +
            continue
 +
        if line.startswith( "def " + functionName + " (" ):
 +
            foundDef = True
 +
            continue
 +
        if line.find( functionName+"(" ) != -1:
 +
            foundCall = True
 +
            continue
 +
 +
    return (foundDef, foundCall)
 +
 +
 +
 +
# ==================================================================
 +
# NO EDITS NEEDED BELOW!
 +
# ==================================================================
 +
 +
def clearLog():
 +
    open( "log.txt", "w" ).write( "" )
 +
 +
def log( message ):
 +
    file = open( "log.txt", "a" )
 +
    file.write( message + "\n" )
 +
    file.flush()
 +
    file.close()
 +
 +
 +
# checkModuleRunsOK: runs the module as a shell subprocess and
 +
# look for errors in the output.  This is required, because otherwise
 +
# importing the module in this program will make this program crash.
 +
# It's not possible (as far as I can tell0 to catch exceptions from
 +
# the import or __module__ statements.
 +
# returns True, none if no errors, otherwise False, string if there's
 +
# an exception, and the error message (string) is in the returned 2nd
 +
# arg.
 +
# The module name is assumed to not include ".py"
 +
def checkModuleRunsOk( module, inputFileName ):
 +
    global interpreter
 +
    p = subprocess.Popen( [ interpreter, module+".py" ],
 +
                          stdout=subprocess.PIPE,
 +
                          stderr=subprocess.PIPE,
 +
                          stdin=subprocess.PIPE)
 +
 +
    #print( "inputFileName = ", inputFileName )
 +
    #print( "open( inputFileName, r).read() = ", open( inputFileName, "r" ).read() )
 +
 +
    p.stdin.write( bytes( "\n\n\n\n", 'UTF-8' ) )
 +
    data = p.communicate( )
 +
    p.stdin.close()
 +
 +
    error = data[1].decode( 'UTF-8' )
 +
    if len( error ) > 1:
 +
        return False, error
 +
    return True, None
 +
 +
 +
# extractTextFromErrorMessage( sys_exc_info ):
 +
def extractTextFromErrorMessage( sys_exc_info ):
 +
    print( "sys_exec_info = ", sys_exc_info )
 +
    text = ""
 +
    for field in sys_exc_info:
 +
        if type( field )==type( " " ):
 +
            text += field + "\n"
 +
    return text
 +
 +
# runModule:
 +
# runs the module, passes it data from the input file on its stdin
 +
# and get its output on stdout captured in outputFileName.
 +
# We assume the module will not crash, because we already tested
 +
# it with checkModuleRunsOk().
 +
def runModule( module, inputFileName, outputFileName ):
 +
    global userOutSplitPattern
 +
 +
    # remove the if __name__=="__main__": statement
 +
    removeIf__name__( module )
 +
 +
    error = False
 +
 +
    #--- make stdin read information from the text file
 +
    #sys.stdin = open( inputFileName, "r" )
 +
 +
    #--- capture the stdout of the program to test into a file
 +
    saveStdOut = sys.stdout
 +
    saveStdErr = sys.stderr
 +
 +
    sys.stdout = open( outputFileName, "w" )
 +
    sys.stderr = open( "errorOut", "w" )
 +
 +
    #--- run the student program ---
 +
    try:
 +
        _module = __import__(  module  )
 +
    except:
 +
        error = True
 +
        sys.stderr.close()
 +
        sys.stderr = saveStdErr
 +
        sys.stdout.close()
 +
        sys.stdout = saveStdOut
 +
        text = sys.exc_info()[0]
 +
        text = extractTextFromErrorMessage( text )
 +
        print( "*** sys.exc_info() = ", text )
 +
        text = open( outputFileName, "r" ).read() + "\n" + text
 +
        return text
 +
 +
    #--- filter out junk from output of program ---
 +
    sys.stdout.close()
 +
    sys.stdout = saveStdOut
 +
    sys.stderr.close()
 +
    sys.stderr = saveStdErr
 +
 +
    file = open( outputFileName, "r" )
 +
    text = file.read()
 +
    #print( "output = ", text )
 +
    file.close()
 +
    return text
 +
 +
def removeBlankLines( lines ):
 +
    newLines = []
 +
    log( "removeBlankLines: lines = " + str( lines ) )
 +
    for line in lines.split( "\n" ):
 +
        if len( line )==0:
 +
            continue
 +
        newLines.append( line )
 +
 +
    return ( "\n".join( newLines ) ) + "\n"
 +
 +
def compareUserExpectedGrep( inputLines, userOutText, expectedOutText ):
 +
    list = []
 +
    for line in listPres.split( "\n" ):
 +
        fields = line.split( )
 +
        if len( fields ) != 2: continue
 +
        pres = fields[0].strip().lower()
 +
        count = int( fields[1].strip() )
 +
        list.append( (pres, count) )
 +
 +
    input = userOutText.lower()
 +
    #print( "input = ", input )
 +
 +
    sumUser    = 0
 +
    sumExpected = 0
 +
    for pres, count in list:
 +
        c = input.count( pres )
 +
        sumUser += c
 +
        sumExpected += count
 +
        #print( "pres=%s count=%d c=%d sumuser=%d sumExpected=%d"
 +
        #      % ( pres, count, c, sumUser, sumExpected ) )
 +
    return (sumUser * 100.0 )/sumExpected
 +
 +
 +
 +
def compareUserExpected( inputLines, userOutText, expectedOutText ):
 +
    import re
 +
    global stripOutputsBeforeCompare
 +
 +
    userOutText = removeBlankLines( userOutText )
 +
    expectedOutText = removeBlankLines( expectedOutText )
 +
    misMatchLineNumbers = []
 +
    userTextOutLines = userOutText.split( "\n" )
 +
    expectedOutTextLines = expectedOutText.split( "\n" )
 +
 +
 +
    for i in range( len( userTextOutLines ) ):
 +
        lineNo = i+1
 +
        userLine = userTextOutLines[i]
 +
        if i >= len( expectedOutTextLines ):
 +
            misMatchLineNumbers.append( lineNo )
 +
            break
 +
        expectedLine = expectedOutTextLines[i]
 +
        log( "comparing:\n  "+userLine+"\n  "+expectedLine )
 +
 +
        if stripOutputsBeforeCompare:
 +
            userLine = userLine.strip()
 +
            expectedLine = expectedLine.strip()
 +
 +
        if stripDoubleSpaceBeforeCompare:
 +
            userLine = re.sub(' +',' ', userLine )
 +
            expectedLine = re.sub( ' +', ' ', expectedLine )
 +
 +
        if userLine != expectedLine:
 +
            log( "\ndifference: user    >" + userTextOutLines[i] + "<" )
 +
            log( "expected >" + expectedOutTextLines[i] + "<" )
 +
            misMatchLineNumbers.append( lineNo )
 +
 +
    return misMatchLineNumbers
 +
 +
 +
 +
def main():
 +
    global module
 +
    global solutionModule
 +
 +
    #--- clear debug log ---
 +
    clearLog()
 +
 +
    #--- check that the main module uses a main() function
 +
    """
 +
    foundDef, foundCall = checkForFunctionPresence( module, "main" )
 +
 +
    if (not foundDef) or (not foundCall):
 +
        commentShort( "-Missing main() program" )
 +
        commentShort( "Your program must use a main() function." )
 +
        commentShort( "(make sure you spell it exactly \"main()\"!" )
 +
        printGrade( 40 )
 +
        return
 +
    """
 +
    inputFileName = "cats.csv"
 +
 +
    #--- generate input file with random data ---
 +
    inputLines = generateInputFileWithRandomInputs( inputFileName )
 +
 +
 +
    Ok, errorMessage = checkModuleRunsOk( module, inputFileName )
 +
    if not Ok:
 +
        commentLong( "- Your program crashed...\n"
 +
                    + "Error message:\n"
 +
                    + errorMessage + "\n" )
 +
        printGrade( 50 )
 +
        return
 +
 +
    expectedOutText = runModule( solutionModule, inputFileName, "expectedOut" )
 +
    userOutText = runModule( module, inputFileName, "userOut" )
 +
 +
    #print( "expectedOutText = ", expectedOutText )
 +
    #print( "userOutText = ", userOutText )
 +
 +
    #missMatches = compareUserExpected( inputLines, userOutText, expectedOutText )
 +
    percentMatches = compareUserExpectedGrep( inputLines, userOutText, expectedOutText )
 +
 +
 +
    if abs(100-percentMatches) < 10: printGrade( 100 )
 +
    elif abs(100-percentMatches) <20: printGrade( 90 )
 +
    elif abs(100-percentMatches) <30: printGrade( 80 )
 +
    elif abs(100-percentMatches) <40: printGrade( 65 )
 +
    elif abs(100-percentMatches) <50: printGrade( 50 )
 +
    else: printGrade( 10 )
 +
 +
    if abs( 100 - percentMatches ) >= 10:
 +
        commentLong( "Your output does not match the expected output:\n"+expectedOutputText )
 +
    else:
 +
        commentLong( "Congrats, your output is correct." )
 +
 +
 +
    #if len( missMatches ) != 0:
 +
    #    commentLong( "- Incorrect output...\n"
 +
    #                +"Expected output:\n"
 +
    #                +expectedOutText + "\n"
 +
    #                +"Your output:\n"
 +
    #                +userOutText + "\n" )
 +
    #    printGrade( 75 )
 +
    #else:
 +
    #    commentLong( "- Your program passes the test\n" )
 +
    #    printGrade( 100 )
 +
 +
 +
main()
 +
 +
 +
 +
</source>
 +
<br />
 +
==lab11_4sol.py==
 +
<br />
 +
<source lang="python">
 +
# presidents.py
 +
# D. Thiebaut
 +
 +
text="""Presidency ,President, Took office ,Left office ,Party , Home State
 +
1, George Washington, 30/04/1789, 4/03/1797, Independent, Virginia
 +
2, John Adams, 4/03/1797, 4/03/1801, Federalist, Massachusetts
 +
3, Thomas Jefferson, 4/03/1801, 4/03/1809, Democratic-Republican, Virginia
 +
4, James Madison, 4/03/1809, 4/03/1817, Democratic-Republican, Virginia
 +
5, James Monroe, 4/03/1817, 4/03/1825, Democratic-Republican, Virginia
 +
6, John Quincy Adams, 4/03/1825, 4/03/1829, Democratic-Republican/National Republican, Massachusetts
 +
7, Andrew Jackson, 4/03/1829, 4/03/1837, Democratic, Tennessee
 +
8, Martin Van Buren, 4/03/1837, 4/03/1841, Democratic, New York
 +
9, William Henry Harrison, 4/03/1841, 4/04/1841, Whig, Ohio
 +
10, John Tyler, 4/04/1841, 4/03/1845, Whig, Virginia
 +
11, James K. Polk, 4/03/1845, 4/03/1849, Democratic, Tennessee
 +
12, Zachary Taylor, 4/03/1849, 9/07/1850, Whig, Louisiana
 +
13, Millard Fillmore, 9/07/1850, 4/03/1853, Whig, New York
 +
14, Franklin Pierce, 4/03/1853, 4/03/1857, Democratic, New Hampshire
 +
15, James Buchanan, 4/03/1857, 4/03/1861, Democratic, Pennsylvania
 +
16, Abraham Lincoln, 4/03/1861, 15/04/1865, Republican/National Union, Illinois
 +
17, Andrew Johnson, 15/04/1865, 4/03/1869, Democratic/National Union, Tennessee
 +
18, Ulysses S. Grant, 4/03/1869, 4/03/1877, Republican, Ohio
 +
19, Rutherford B. Hayes, 4/03/1877, 4/03/1881, Republican, Ohio
 +
20, James A. Garfield, 4/03/1881, 19/09/1881, Republican, Ohio
 +
21, Chester A. Arthur, 19/09/1881, 4/03/1885, Republican, New York
 +
22, Grover Cleveland, 4/03/1885, 4/03/1889, Democratic, New York
 +
23, Benjamin Harrison, 4/03/1889, 4/03/1893, Republican, Indiana
 +
24, Grover Cleveland, 4/03/1893, 4/03/1897, Democratic, New York
 +
25, William McKinley, 4/03/1897, 14/9/1901, Republican, Ohio
 +
26, Theodore Roosevelt, 14/9/1901, 4/3/1909, Republican, New York
 +
27, William Howard Taft, 4/3/1909, 4/03/1913, Republican, Ohio
 +
28, Woodrow Wilson, 4/03/1913, 4/03/1921, Democratic, New Jersey
 +
29, Warren G. Harding, 4/03/1921, 2/8/1923, Republican, Ohio
 +
30, Calvin Coolidge, 2/8/1923, 4/03/1929, Republican, Massachusetts
 +
31, Herbert Hoover, 4/03/1929, 4/03/1933, Republican, Iowa
 +
32, Franklin D. Roosevelt, 4/03/1933, 12/4/1945, Democratic, New York
 +
33, Harry S. Truman, 12/4/1945, 20/01/1953, Democratic, Missouri
 +
34, Dwight D. Eisenhower, 20/01/1953, 20/01/1961, Republican, Texas
 +
35, John F. Kennedy, 20/01/1961, 22/11/1963, Democratic, Massachusetts
 +
36, Lyndon B. Johnson, 22/11/1963, 20/1/1969, Democratic, Texas
 +
37, Richard Nixon, 20/1/1969, 9/8/1974, Republican, California
 +
38, Gerald Ford, 9/8/1974, 20/01/1977, Republican, Michigan
 +
39, Jimmy Carter, 20/01/1977, 20/01/1981, Democratic, Georgia
 +
40, Ronald Reagan, 20/01/1981, 20/01/1989, Republican, California
 +
41, George H. W. Bush, 20/01/1989, 20/01/1993, Republican, Texas
 +
42, Bill Clinton, 20/01/1993, 20/01/2001, Democratic, Arkansas
 +
43, George W. Bush, 20/01/2001, 20/01/2009, Republican, Texas"""
 +
 +
def main():
 +
    # remove the first line from the list of lines
 +
    lines = text.split( "\n" )
 +
    lines = lines[1:]
 +
 +
    # create a list of tuples
 +
    list = []
 +
    for line in lines:
 +
        fields = line.split( "," )
 +
 +
        # skip invalid lines
 +
        if len( fields ) != 6:
 +
            continue
 +
 +
        # create tuple
 +
        name = fields[1].strip()
 +
        yearStart = int( fields[2].split( "/" )[2] )
 +
        yearEnd  = int( fields[3].split( "/" )[2] )
 +
        party    = fields[-2].strip()
 +
        state    = fields[-1].strip()
 +
        tuple = ( name, yearStart, yearEnd, party, state )
 +
 +
        # add tuple to list
 +
        list.append( tuple )
 +
 +
    allPres = {}
 +
    #=========================================================
 +
    # Display the list of presidents in alphabetical order
 +
    #=========================================================
 +
    print( "\n\nAlphabetical List".upper() )
 +
    list.sort()
 +
    for name, y1, y2, party, s in list:
 +
        print( name )
 +
        lastName = name.split()[-1].strip()
 +
        try:
 +
            allPres[lastName] += 1
 +
        except:
 +
            allPres[lastName] = 1
 +
 +
    print()
 +
 +
    #=========================================================
 +
    # Display the list of presidents associated with the democratic
 +
    # party
 +
    #=========================================================
 +
    print( "\n\nAlphabetical List of Democrats".upper() )
 +
    for name, y1, y2, party, s in list:
 +
        if party.lower().find( "democra" )!= -1:
 +
            print( name )
 +
            lastName = name.split()[-1].strip()
 +
            try:
 +
                allPres[lastName] += 1
 +
            except:
 +
                allPres[lastName] = 1
 +
 +
    print()
 +
    #=========================================================
 +
    # Display the president in office in 1945
 +
    #=========================================================
 +
    print( "\n\nPresident in office in 1945".upper() )
 +
    for name, y1, y2, party, s in list:
 +
        if y1 <= 1945 <= y2:
 +
            print( name )
 +
            lastName = name.split()[-1].strip()
 +
            try:
 +
                allPres[lastName] += 1
 +
            except:
 +
                allPres[lastName] = 1
 +
 +
    print()
 +
    #=========================================================
 +
    # Display the president whose home state was Massachusetts
 +
    #=========================================================
 +
    print( "\n\nPresident from Massachusetts".upper() )
 +
    for name, y1, y2, party, s in list:
 +
        if s.capitalize().find( "Massa" ) != -1:
 +
            print( name )
 +
            lastName = name.split()[-1].strip()
 +
            try:
 +
                allPres[lastName] += 1
 +
            except:
 +
                allPres[lastName] = 1
 +
 +
    #print()
 +
    #for name in allPres.keys():
 +
    #    print( name, allPres[name ] )
 +
 +
if True:
 +
    main()
 +
 +
</source>
 +
<br />
 +
\
 +
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 16:00, 12 April 2015

--D. Thiebaut (talk) 19:36, 11 April 2015 (EDT)



...


...