-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGenBase.py
78 lines (58 loc) · 2.05 KB
/
GenBase.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
import re
from collections import Counter
import EGE.Random
class EGEError(Exception):
pass
class Question:
rnd: EGE.Random.Random
def __init__(self, rnd: EGE.Random.Random, text: str = None, correct = None):
self.rnd = rnd
self.text = text
self.correct = correct
def generate(self):
raise ValueError()
def post_process(self):
pass
def export_type(self):
raise EGEError()
class SingleChoice(Question):
def __init__(self, rnd: EGE.Random.Random, text: str = None, correct: int = 0):
super().__init__(rnd, text, correct)
self.variants: list = []
def export_type(self):
return 'sc'
def set_variants(self, variants):
self.variants = variants
return self
def set_formatted_variants(self, format: str, args):
self.variants = [ format % a for a in args ]
return self
def check_distinct_variants(self):
duplicates = [ v for v, cnt in Counter(self.variants).items() if cnt > 1 ]
if duplicates:
raise EGEError(f"Duplicate variants: {', '.join(duplicates)}")
def shuffle_variants(self):
if not self.variants:
raise EGEError()
permutation = self.rnd.shuffle(list(range(len(self.variants))))
self.correct = permutation.index(self.correct)
self.variants = [ self.variants[i] for i in permutation ]
def post_process(self):
self.check_distinct_variants()
self.shuffle_variants()
class DirectInput(Question):
def __init__(self, rnd: EGE.Random.Random, text: str = None, correct: int = 0):
super().__init__(rnd, text, correct)
self.accept = r".+"
self.variants = []
pass
def export_type(self):
return 'di'
def accept_number(self):
self.accept = r"^\d+$"
pass
def post_process(self):
if re.search(self.accept, self.correct):
return
# TODO: ask ask if message is correct
raise EGEError(f"Correct answer is not acceptable in {self}: {self.correct}")