-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.py
58 lines (42 loc) · 1.47 KB
/
question.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
class OpenQuestion(object):
def __init__(self, question, display_hints = {}, images = [], **kwargs):
self._question = question
self._display_hints = display_hints
self._images = images
def get_question(self):
return self._question
def get_answers(self):
return []
def get_choices(self, *args):
return []
def get_display_hints(self):
return self._display_hints
def get_images(self):
return self._images
def __str__(self):
return self.get_question()
class ChoiceQuestion(OpenQuestion):
def __init__(self, question, answers, **kwargs):
super().__init__(question, **kwargs)
self.__answers = answers
def get_answers(self):
return self.__answers
def get_choices(self, rng, count = 1):
return rng.sample(self.__answers, min(count, len(self.__answers)))
class FixedChoiceQuestion(ChoiceQuestion):
def __init__(self, question, answers, fixed_answers, **kwargs):
super().__init__(question, answers, **kwargs)
self.__fixed_answers = fixed_answers
def get_answers(self):
return super().get_answers() + self.__fixed_answers
def get_choices(self, rng, count = 1):
return super().get_choices(rng, count) + self.__fixed_answers
def create_question(type, *args, **kwargs):
if type == 'open':
return OpenQuestion(*args, **kwargs)
elif type == 'choice':
return ChoiceQuestion(*args, **kwargs)
elif type == 'fixed':
return FixedChoiceQuestion(*args, **kwargs)
else:
raise ValueError('Unknown type of question: {}'.format(type))