diff --git a/check_questions.py b/check_questions.py new file mode 100644 index 0000000..f42f12b --- /dev/null +++ b/check_questions.py @@ -0,0 +1,34 @@ +# Служебная проверка данных викторины: у каждого вопроса 4 варианта, +# номер правильного ответа существует, тексты не пустые. +# Запуск: python check_questions.py +import re +import sys + +with open("questions.js", encoding="utf-8") as f: + src = f.read() + +texts = re.findall(r'text:\s*"([^"]*)"', src) +options_blocks = re.findall(r'options:\s*\[([^\]]*)\]', src) +corrects = [int(m) for m in re.findall(r'correct:\s*(\d+)', src)] + +errors = [] + +if not (len(texts) == len(options_blocks) == len(corrects)): + errors.append("У какого-то вопроса не хватает поля (text/options/correct).") + +for n, (text, block, correct) in enumerate(zip(texts, options_blocks, corrects), 1): + opts = re.findall(r'"([^"]*)"', block) + if not text.strip(): + errors.append(f"Вопрос {n}: пустой текст.") + if len(opts) != 4: + errors.append(f"Вопрос {n}: вариантов {len(opts)}, а нужно 4.") + if not (0 <= correct < len(opts)): + errors.append(f"Вопрос {n}: correct={correct} указывает мимо вариантов.") + +if errors: + print("ПРОБЛЕМЫ:") + for e in errors: + print(" -", e) + sys.exit(1) + +print(f"OK: {len(texts)} вопросов, у всех 4 варианта и правильный ответ на месте.")