68 lines
3.1 KiB
Python
68 lines
3.1 KiB
Python
from ..data import data as data_dir
|
|
|
|
import json
|
|
from random import shuffle
|
|
|
|
def load(filename:str):
|
|
with open(f'./{data_dir}/{filename}') as file:
|
|
return json.load(file)
|
|
|
|
def save(data:dict, filename:str):
|
|
with open(f'./{data_dir}/{filename}', 'w') as file:
|
|
json.dump(data, file, indent=4)
|
|
|
|
def check_is_json(file):
|
|
if not '.' in file.filename or not file.filename.rsplit('.',1)[-1] == 'json': return False
|
|
return True
|
|
|
|
def validate_json(file):
|
|
file.stream.seek(0)
|
|
data = json.loads(file.read())
|
|
if not isinstance(data, list): return False
|
|
for block in data:
|
|
block_type = block.pop('type', None)
|
|
if block_type not in ['block', 'question']: return False
|
|
if block_type == 'question':
|
|
if not all (key in block for key in ['q_no', 'text', 'options', 'correct', 'q_type', 'tags']): return False
|
|
if not isinstance(block['q_no'], int): return False
|
|
if not isinstance(block['text'], str): return False
|
|
if not isinstance(block['options'], list): return False
|
|
for option in block['options']:
|
|
if not isinstance(option, str): return False
|
|
if not isinstance(block['correct'], int): return False
|
|
if not isinstance(block['q_type'], str): return False
|
|
if block['q_type'] not in ['Multiple Choice', 'Yes/No', 'List']: return False
|
|
if not isinstance(block['tags'], list): return False
|
|
for tag in block['tags']:
|
|
if not isinstance(tag, str): return False
|
|
if block_type == 'block':
|
|
if not all (key in block for key in ['question_header', 'questions']): return False
|
|
if not isinstance(block['question_header'], str): return False
|
|
if not isinstance(block['questions'], list): return False
|
|
for question in block['questions']:
|
|
if not all (key in question for key in ['q_no', 'text', 'options', 'correct', 'q_type', 'tags']): return False
|
|
if not isinstance(question['text'], str): return False
|
|
if not isinstance(question['q_no'], int): return False
|
|
if not isinstance(question['options'], list): return False
|
|
for option in question['options']:
|
|
if not isinstance(option, str): return False
|
|
if not isinstance(question['correct'], int): return False
|
|
if not isinstance(question['q_type'], str): return False
|
|
if question['q_type'] not in ['Multiple Choice', 'Yes/No', 'List']: return False
|
|
if not isinstance(question['tags'], list): return False
|
|
for tag in question['tags']:
|
|
if not isinstance(tag, str): return False
|
|
return True
|
|
|
|
def randomise_list(list:list):
|
|
_list = list.copy()
|
|
shuffle(_list)
|
|
return(_list)
|
|
|
|
def get_tag_list(dataset:list):
|
|
output = []
|
|
for block in dataset:
|
|
if block['type'] == 'question': output = list(set(output) | set(block['tags']))
|
|
if block['type'] == 'block':
|
|
for question in block['questions']: output = list(set(output) | set(question['tags']))
|
|
return output |