mirror of
https://github.com/bregman-arie/devops-exercises.git
synced 2026-02-04 23:59:11 +00:00
Refactor question utils for enhanced performance, readability, and extended functionality while preserving backward compatibility (#10551)
This commit is contained in:
parent
692a680ccb
commit
70f382d5b1
@ -7,66 +7,50 @@ from random import choice
|
|||||||
from typing import List
|
from typing import List
|
||||||
import re
|
import re
|
||||||
|
|
||||||
p = pathlib.Path(__file__).parent.parent.joinpath("README.md")
|
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_list():
|
def get_file_content() -> str:
|
||||||
file_list = ""
|
with README_PATH.open("r", encoding="utf-8") as f:
|
||||||
with open(p, "rb") as f:
|
return f.read()
|
||||||
for line in f.readlines():
|
|
||||||
file_list += line.rstrip().decode()
|
|
||||||
return file_list
|
|
||||||
|
|
||||||
|
|
||||||
def get_question_list(file_list: List[str]) -> list:
|
def get_question_list(file_content: str) -> List[str]:
|
||||||
file_list = re.findall("<details>(.*?)</details>", file_list)
|
details = DETAILS_PATTERN.findall(file_content)
|
||||||
questions_list = []
|
return [SUMMARY_PATTERN.search(detail).group(1) for detail in details if SUMMARY_PATTERN.search(detail)]
|
||||||
for i in file_list:
|
|
||||||
q = re.findall(r"<summary>(.*?)</summary>", i)[0]
|
|
||||||
questions_list.append(q)
|
|
||||||
return questions_list
|
|
||||||
|
|
||||||
|
|
||||||
def get_answered_questions(question_list: List[str]) -> list:
|
def get_answered_questions(file_content: str) -> List[str]:
|
||||||
t = []
|
details = DETAILS_PATTERN.findall(file_content)
|
||||||
question_list = re.findall("<details>(.*?)</details>", question_list)
|
answered = []
|
||||||
for i in question_list:
|
for detail in details:
|
||||||
q = re.findall(r"<summary>(.*?)</summary>", i)
|
summary_match = SUMMARY_PATTERN.search(detail)
|
||||||
if q and q[0] == "":
|
b_match = B_PATTERN.search(detail)
|
||||||
continue
|
if summary_match and b_match and summary_match.group(1).strip() and b_match.group(1).strip():
|
||||||
a = re.findall(r"<b>(.*?)</b>", i)
|
answered.append(summary_match.group(1))
|
||||||
if a and a[0] == "":
|
return answered
|
||||||
continue
|
|
||||||
else:
|
|
||||||
t.append(q[0])
|
|
||||||
return t
|
|
||||||
|
|
||||||
|
|
||||||
def get_answers_count() -> List:
|
def get_answers_count() -> List[int]:
|
||||||
"""
|
file_content = get_file_content()
|
||||||
Return [answer_questions,all_questions] ,PASS complete. FAIL incomplete.
|
answered = get_answered_questions(file_content)
|
||||||
>>> get_answers_count()
|
all_questions = get_question_list(file_content)
|
||||||
[463, 463]
|
return [len(answered), len(all_questions)]
|
||||||
"""
|
|
||||||
ans_questions = get_answered_questions(get_file_list())
|
|
||||||
len_ans_questions = len(ans_questions)
|
|
||||||
all_questions = get_question_list(get_file_list())
|
|
||||||
len_all_questions = len(all_questions)
|
|
||||||
return [len_ans_questions, len_all_questions]
|
|
||||||
|
|
||||||
|
|
||||||
def get_challenges_count() -> int:
|
def get_challenges_count() -> int:
|
||||||
challenges_path = (
|
return len(list(EXERCISES_PATH.glob("*.md")))
|
||||||
pathlib.Path(__file__).parent.parent.joinpath("exercises").glob("*.md")
|
|
||||||
)
|
|
||||||
return len(list(challenges_path))
|
|
||||||
|
|
||||||
|
|
||||||
# WIP WAITING FEEDBACK
|
def get_random_question(question_list: List[str], with_answer: bool = False) -> str:
|
||||||
def get_random_question(question_list: List[str], with_answer=False):
|
|
||||||
if with_answer:
|
if with_answer:
|
||||||
return choice(get_answered_questions(question_list))
|
return choice(get_answered_questions(get_file_content()))
|
||||||
return choice(get_question_list(question_list))
|
return choice(get_question_list(get_file_content()))
|
||||||
|
|
||||||
|
|
||||||
"""Use this question_list. Unless you have already opened/worked/need the file, then don't or
|
"""Use this question_list. Unless you have already opened/worked/need the file, then don't or
|
||||||
@ -74,7 +58,7 @@ you will end up doing the same thing twice.
|
|||||||
eg:
|
eg:
|
||||||
#my_dir/main.py
|
#my_dir/main.py
|
||||||
from scripts import question_utils
|
from scripts import question_utils
|
||||||
print(question_utils.get_answered_questions(question_utils.question_list)
|
print(question_utils.get_answered_questions(question_utils.get_question_list(question_utils.get_file_content()))
|
||||||
>> 123
|
>> 123
|
||||||
# noqa: E501
|
# noqa: E501
|
||||||
"""
|
"""
|
||||||
@ -83,7 +67,3 @@ if __name__ == "__main__":
|
|||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
# print(get_question_list(get_file_list()))
|
|
||||||
# print(get_answered_questions(get_file_list()))
|
|
||||||
# print(get_random_question(get_file_list(),True))
|
|
||||||
# print(get_random_question(get_file_list(),False))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user