-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsetMaker.py
32 lines (24 loc) · 980 Bytes
/
qsetMaker.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
import hashlib, json
from typing import List
def make_qset(questions: List[dict]) -> dict:
qset = {}
for question in [q for q in questions if validate(q)]:
q_id = hashlib.md5(json.dumps(question).encode()).hexdigest()
if q_id not in qset.keys():
qset[q_id] = question
return qset
def update_qset(qset: dict, questions: List[dict]) -> dict:
for q in [_q for _q in questions if validate(_q)]:
q_id = hashlib.md5(json.dumps(q).encode()).hexdigest()
if q_id not in qset.keys():
qset[q_id] = q
return qset
def write_qset(qset: dict, fname: str) -> None:
with open(f"{fname}.mcquiz", "w") as _f:
json.dump(qset, _f)
def validate(question: dict) -> bool:
return True if (question["text"] and
question["answers"] and
len(question["answers"]) > 1 and
sum([a[0] for a in question["answers"]]) > 0
) else False