Files
  • main.py
  • areaofcircle - Oka Kurniawan.py
  • areaofcircle1.py
  • areaofcircle2.py
  • areaofcircle3.py
  • scores.xlsx
main.py
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
#1 import assignments for marking
import os
modulesname = []
for f in os.listdir():
  fname, fext = os.path.splitext(f)
  if "areaofcircle" in fname:
    modulesname.append(fname)
modules = map(__import__, modulesname)

#2 Create Model solution
from math import pi
def modelsol(r):
  try:
    if r<0 or not type(r) in (int, float):
      raise Exception
  except:
    return ("invalid")
  else:
    return (pi*r*r) 

#3 Handle crashing of assignments gracefully
def crashgracefully(f):
  def helper(r):
    try: f(r)
    except: return "Crashed"
    else: return f(r)
  return helper

#4 Mark assignments againsts test cases
testcases = [3, 3.1, 0, -3, -3.1, "two", True, 1+3j]
results = []
for m in modules:
  print(m.name, m.index)
  scores, total = [], 0
  for test in testcases:
    f = crashgracefully(m.areaOfCircle)
    if f(test) == modelsol(test):
      total += 1
      scores.append(1)
    else:
      scores.append(0)
  result = [m.name, m.index]+[x for x in scores]+[total]
  results.append(result)
  print("\tTotal score = {}".format(total))
    
#5 Export scores to excel spreadsheet 
import openpyxl
wb = openpyxl.Workbook()
sh = wb.active
rowHeader = ["Name", "Index"]+[str(x) for x in testcases]+["Total Score"]
sh.append(rowHeader)
for row in results:
  sh.append(row)
wb.save("scores.xlsx")
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux