Spaces:
Runtime error
Runtime error
Added utility to serve up the test questions
Browse files- src/testing.py +39 -0
src/testing.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from random import choices
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
from src.common import data_dir
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestGenerator:
|
| 11 |
+
"""
|
| 12 |
+
Wrapper to hold testing questions and serve up examples
|
| 13 |
+
"""
|
| 14 |
+
questions: List[str] = None
|
| 15 |
+
|
| 16 |
+
@classmethod
|
| 17 |
+
def load_questions(cls, reload=False) -> None:
|
| 18 |
+
"""
|
| 19 |
+
Load the available questions from the json file.
|
| 20 |
+
Default to not re-loading if already done, but allow for the option to do so
|
| 21 |
+
"""
|
| 22 |
+
if cls.questions is not None and not reload:
|
| 23 |
+
return
|
| 24 |
+
question_file = os.path.join(data_dir, 'json', 'test_questions.json')
|
| 25 |
+
with open(question_file, 'r') as f:
|
| 26 |
+
question_json = json.load(f)
|
| 27 |
+
cls.questions = question_json['questions']
|
| 28 |
+
|
| 29 |
+
@classmethod
|
| 30 |
+
def question_count(cls) -> int:
|
| 31 |
+
cls.load_questions()
|
| 32 |
+
return len(cls.questions)
|
| 33 |
+
|
| 34 |
+
@classmethod
|
| 35 |
+
def get_random_questions(cls, n: int):
|
| 36 |
+
"""
|
| 37 |
+
Return n random questions
|
| 38 |
+
"""
|
| 39 |
+
return choices(cls.questions, k=n)
|