-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquestengine.py
106 lines (80 loc) · 3.61 KB
/
questengine.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
95
96
97
98
99
100
101
102
103
104
105
106
import json
import random
from pathlib import Path
class QuestionEngine:
def __init__(self, jsonfile: str = 'bank/cissp_questions.json',
questionisrandom: bool = True,
answerisalpha: bool = True,
answerisrandom: bool = True):
"""Use JSON files to make exams"""
self._jsonFile = jsonfile
self._questionIsRandom = questionisrandom
self._answerIsAlpha = answerisalpha
self._answerIsRandom = answerisrandom
self._jsonData = self.__loadJsonFile()
self.correct = 0
self.incorrect = 0
self.totalQuestions = len(self._jsonData['questions'])
self.questionSet = self.__complieQuestions()
def __loadJsonFile(self) -> dict:
"""Load the json question file"""
jsonDataFile = Path(self._jsonFile)
with open(jsonDataFile) as f:
self._jsonData = json.load(f)
f.close()
return self._jsonData
def __complieQuestions(self) -> dict:
"""Create dictionary of questions and question number"""
if self._questionIsRandom:
questions = random.sample(range(0, self.totalQuestions),
self.totalQuestions)
else:
questions = list(range(0, self.totalQuestions))
questionSet = {}
currentAnswers = {}
for itr, question in enumerate(questions):
answers = self._jsonData['questions'][question]['answers']
questionSection = self._jsonData['questions'][question]
answerKeys = '123456789'
if self._answerIsAlpha:
answerKeys = 'abcdefghi'
answerValues = list(answers.keys())
if self._answerIsRandom:
random.shuffle(answerValues)
currentAnswers = {}
for answer in range(len(answerKeys)):
if answer >= len(answerValues):
break
else:
currentAnswers.update({answerKeys[answer]: {
answerValues[answer]: answers[answerValues[answer]]}})
questionSet[itr] = ({'question': questionSection['question'],
'answers': currentAnswers,
'solution': questionSection['solution'],
'explanation': questionSection['explanation']})
return questionSet
def getQuestion(self, questionnumber: int) -> str:
"""Return question from compiled questions"""
return self.questionSet[questionnumber]['question']
def getAnswers(self, questionnumber: int) -> dict:
"""Return dictionary with answers for given question"""
return self.questionSet[questionnumber]['answers']
def getExplanation(self, questionnumber: int) -> str:
"""Return solution for given question"""
return self.questionSet[questionnumber]['explanation']
def getSolutionText(self, questionnumber: int) -> str:
"""Return solution for given question"""
solution = self.questionSet[questionnumber]['solution']
answers = self.questionSet[questionnumber]['answers']
solutiontext = ""
for key, value in answers.items():
for k, v in value.items():
if solution == k:
solutiontext = v
return solutiontext
def compareSolution(self, questionnumber: int, answerguess: int) -> bool:
"""Compare value to solution"""
if answerguess == self.questionSet[questionnumber]['solution']:
return True
else:
return False