2020-01-09 12:11:34 +01:00
|
|
|
"""
|
|
|
|
|
Question utils functions
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
|
from random import choice
|
|
|
|
|
from typing import List
|
2023-02-05 14:51:34 -04:00
|
|
|
import re
|
2020-01-09 12:11:34 +01:00
|
|
|
|
2025-03-26 21:21:53 -01:00
|
|
|
README_PATH = pathlib.Path(__file__).parent.parent / "README.md"
|
|
|
|
|
EXERCISES_PATH = pathlib.Path(__file__).parent.parent / "exercises"
|
|
|
|
|
|
|
|
|
|
DETAILS_PATTERN = re.compile(r"<details>(.*?)</details>", re.DOTALL)
|
|
|
|
|
SUMMARY_PATTERN = re.compile(r"<summary>(.*?)</summary>", re.DOTALL)
|
|
|
|
|
B_PATTERN = re.compile(r"<b>(.*?)</b>", re.DOTALL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_content() -> str:
|
|
|
|
|
with README_PATH.open("r", encoding="utf-8") as f:
|
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_question_list(file_content: str) -> List[str]:
|
|
|
|
|
details = DETAILS_PATTERN.findall(file_content)
|
|
|
|
|
return [SUMMARY_PATTERN.search(detail).group(1) for detail in details if SUMMARY_PATTERN.search(detail)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_answered_questions(file_content: str) -> List[str]:
|
|
|
|
|
details = DETAILS_PATTERN.findall(file_content)
|
|
|
|
|
answered = []
|
|
|
|
|
for detail in details:
|
|
|
|
|
summary_match = SUMMARY_PATTERN.search(detail)
|
|
|
|
|
b_match = B_PATTERN.search(detail)
|
|
|
|
|
if summary_match and b_match and summary_match.group(1).strip() and b_match.group(1).strip():
|
|
|
|
|
answered.append(summary_match.group(1))
|
|
|
|
|
return answered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_answers_count() -> List[int]:
|
|
|
|
|
file_content = get_file_content()
|
|
|
|
|
answered = get_answered_questions(file_content)
|
|
|
|
|
all_questions = get_question_list(file_content)
|
|
|
|
|
return [len(answered), len(all_questions)]
|
2020-01-09 12:11:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_challenges_count() -> int:
|
2025-03-26 21:21:53 -01:00
|
|
|
return len(list(EXERCISES_PATH.glob("*.md")))
|
2020-01-09 12:11:34 +01:00
|
|
|
|
|
|
|
|
|
2025-03-26 21:21:53 -01:00
|
|
|
def get_random_question(question_list: List[str], with_answer: bool = False) -> str:
|
2020-01-09 12:11:34 +01:00
|
|
|
if with_answer:
|
2025-03-26 21:21:53 -01:00
|
|
|
return choice(get_answered_questions(get_file_content()))
|
|
|
|
|
return choice(get_question_list(get_file_content()))
|
2020-01-09 12:11:34 +01:00
|
|
|
|
|
|
|
|
|
2020-01-09 12:18:19 +01:00
|
|
|
"""Use this question_list. Unless you have already opened/worked/need the file, then don't or
|
2020-01-09 12:11:34 +01:00
|
|
|
you will end up doing the same thing twice.
|
2020-01-09 12:18:19 +01:00
|
|
|
eg:
|
2020-01-09 12:11:34 +01:00
|
|
|
#my_dir/main.py
|
|
|
|
|
from scripts import question_utils
|
2025-03-26 21:21:53 -01:00
|
|
|
print(question_utils.get_answered_questions(question_utils.get_question_list(question_utils.get_file_content()))
|
2020-01-09 12:11:34 +01:00
|
|
|
>> 123
|
2023-02-05 14:51:34 -04:00
|
|
|
# noqa: E501
|
2020-01-09 12:11:34 +01:00
|
|
|
"""
|
|
|
|
|
|
2023-02-05 14:51:34 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
|
|
doctest.testmod()
|