-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathValidateAnswer.py
More file actions
76 lines (69 loc) · 2.45 KB
/
ValidateAnswer.py
File metadata and controls
76 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Script to validate output for OMS 6250 Project 2
# Based on Project 2 Autograder by Michael Brown
# Copyright 2017 Kelly Parks
import os
import getopt
import sys
import decimal
#Helper Function: Opens a logfile and reads line by line
# Returns a dictionary representing the final result
# Key will be switch ID
# Entry will be the entire line in file to ensure correct order is used
def getResultFromLog(filename):
with open(filename) as f:
st = {}
for line in f:
line = line.rstrip()
line = line.replace(" ","")
if line != "":
label = ""
label = line.split("-")[0]
st[label] = line.split(",")
return st
#Helper Function: Checks two output dictionaries for equivalence
#Returns True if the two topologies are equivalent, False if not.
def tablesEquivalent(refST, studentST):
#Shallow Check - do both STs have the same number of key value pairs?
if len(refST) != len(studentST):
print "Different number of lines present in two files"
return False
else:
#Ensure all nodes in reference are present in student log
for node in refST:
if studentST.has_key(node):
for entry in refST[node]:
if entry not in studentST[node]:
print "Link " + entry + " missing from student spanning tree"
return False
else:
print "Switch " + node + " missing in student spanning tree"
return False
#Ensure all nodes in student log are present in reference log
for node in studentST:
if refST.has_key(node):
for entry in studentST[node]:
if entry not in refST[node]:
print "Link " + entry + " extra in student spanning tree"
return False
else:
print "Switch " + node + " extra in student spanning tree"
return False
return True
def main(argv):
student_file = ' '
ref_file = ' '
try:
opts, args = getopt.getopt(argv,"s:r:",["student_file=","ref_file="])
except getopt.GetoptError:
print 'ValidateAnswer.py -s <student_file> -r <ref_file>'
sys.exit(2)
for opt, arg in opts:
if opt in ("-s", "--student_file"):
student_file = arg
elif opt in ("-r", "--ref_file"):
ref_file = arg
refST1 = getResultFromLog(ref_file)
studentST1 = getResultFromLog(student_file)
comp = tablesEquivalent(refST1, studentST1)
if __name__ == "__main__":
main(sys.argv[1:])