Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor question utils for enhanced performance and readability #10551

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 39 additions & 59 deletions scripts/question_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,58 @@
from typing import List
import re

p = pathlib.Path(__file__).parent.parent.joinpath("README.md")


def get_file_list():
file_list = ""
with open(p, "rb") as f:
for line in f.readlines():
file_list += line.rstrip().decode()
return file_list


def get_question_list(file_list: List[str]) -> list:
file_list = re.findall("<details>(.*?)</details>", file_list)
questions_list = []
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:
t = []
question_list = re.findall("<details>(.*?)</details>", question_list)
for i in question_list:
q = re.findall(r"<summary>(.*?)</summary>", i)
if q and q[0] == "":
continue
a = re.findall(r"<b>(.*?)</b>", i)
if a and a[0] == "":
continue
else:
t.append(q[0])
return t


def get_answers_count() -> List:
"""
Return [answer_questions,all_questions] ,PASS complete. FAIL incomplete.
>>> get_answers_count()
[463, 463]
"""
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]
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)]


def get_challenges_count() -> int:
challenges_path = (
pathlib.Path(__file__).parent.parent.joinpath("exercises").glob("*.md")
)
return len(list(challenges_path))
return len(list(EXERCISES_PATH.glob("*.md")))


# WIP WAITING FEEDBACK
def get_random_question(question_list: List[str], with_answer=False):
def get_random_question(question_list: List[str], with_answer: bool = False) -> str:
if with_answer:
return choice(get_answered_questions(question_list))
return choice(get_question_list(question_list))
return choice(get_answered_questions(get_file_content()))
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
you will end up doing the same thing twice.
eg:
#my_dir/main.py
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
# noqa: E501
"""
Expand All @@ -83,7 +67,3 @@ def get_random_question(question_list: List[str], with_answer=False):
import doctest

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