forked from zhuli8805/CBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnswerSheet.py
94 lines (83 loc) · 3.66 KB
/
AnswerSheet.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 22 16:36:45 2016
@author: ZHULI
"""
import os, time, re
class AnswerSheet():
def __init__(self, isStop, isStem, testFileName = None):
self.set_filename(testFileName)
self.__correctAnswer = None
self.__totalQuestionNum = 0
self.__correctAnswerNum = 0
self.__answersMade = 0
self.__isStop = isStop
self.__isStem = isStem
def newQuestion(self, correctAnswer):
self.__correctAnswer = correctAnswer
self.__totalQuestionNum += 1
# print('CorrectAnswer = ', correctAnswer)
def submitAnswer(self, myAnswer):
if myAnswer:
self.__answersMade += 1
result = myAnswer == self.__correctAnswer
if result:
self.__correctAnswerNum += 1
# print('<%s> vs <%s>' % (myAnswer, self.__correctAnswer), result)
# print('=============================')
self.__correctAnswer = None
else:
self.__correctAnswer = None
def printResult(self):
if self.__totalQuestionNum != 0:
print('[Overall Score] = %3.3f ( %d / %d )' % (self.__correctAnswerNum/self.__totalQuestionNum,
self.__correctAnswerNum, self.__totalQuestionNum))
else:
print('No question was asked')
if self.__answersMade != 0:
print('[Score of attempted questions] = %3.3f ( %d / %d )' % (self.__correctAnswerNum/self.__answersMade,
self.__correctAnswerNum, self.__answersMade))
else:
print('No question was attempted')
def set_filename(self, filename):
filename = filename.split('\\').pop().replace('.txt', '')
if filename is None:
self.__testName = None
Regex_testname = re.compile('\\\([\w_0-9]*?).txt')
res = Regex_testname.findall(filename)
if res:
self.__testName = res[0]
else:
self.__testName = filename
def printToFile(self, comments, filename = 'Results.txt'):
if os.path.isfile('Results.txt') is False:
file = open(filename, 'w')
print('%24s\t%5s\t%5s\t%s\t\t\t\t\t\t%5s\t\t\t%16s\t\t\t%8s'
%('Time', 'isStop', 'isStem', 'Overall', 'Attempted', 'Test Name', 'Comments'), file = file)
else:
file = open(filename, 'a')
mytime = time.ctime()
overall = None
attempted = None
if self.__totalQuestionNum > 0:
overall = self.__correctAnswerNum/self.__totalQuestionNum * 100
if self.__answersMade > 0:
attempted = self.__correctAnswerNum/self.__answersMade * 100
print('%24s\t%5s\t%5s\t%3.5f%%\t(%d/%d)\t\t%3.5f%%\t(%d/%d)\t'
% (mytime, self.__isStop, self.__isStem, overall, self.__correctAnswerNum, self.__totalQuestionNum ,attempted, self.__correctAnswerNum, self.__answersMade)
+ self.__testName + ' ' + comments , file = file)
file.close()
def reset(self, testFileName = None):
self.set_filename(testFileName)
self.__correctAnswer = None
self.__totalQuestionNum = 0
self.__correctAnswerNum = 0
self.__answersMade = 0
def get_score(self):
overallScore = None
attemptedScore = None
if self.__totalQuestionNum > 0:
overallScore = self.__correctAnswerNum/self.__totalQuestionNum
if self.__answersMade > 0 :
attemptedScore = self.__correctAnswerNum/self.__answersMade
return overallScore, attemptedScore