77from typing import List
88import re
99
10- p = pathlib .Path (__file__ ).parent .parent .joinpath ("README.md" )
11-
12-
13- def get_file_list ():
14- file_list = ""
15- with open (p , "rb" ) as f :
16- for line in f .readlines ():
17- file_list += line .rstrip ().decode ()
18- return file_list
19-
20-
21- def get_question_list (file_list : List [str ]) -> list :
22- file_list = re .findall ("<details>(.*?)</details>" , file_list )
23- questions_list = []
24- for i in file_list :
25- q = re .findall (r"<summary>(.*?)</summary>" , i )[0 ]
26- questions_list .append (q )
27- return questions_list
28-
29-
30- def get_answered_questions (question_list : List [str ]) -> list :
31- t = []
32- question_list = re .findall ("<details>(.*?)</details>" , question_list )
33- for i in question_list :
34- q = re .findall (r"<summary>(.*?)</summary>" , i )
35- if q and q [0 ] == "" :
36- continue
37- a = re .findall (r"<b>(.*?)</b>" , i )
38- if a and a [0 ] == "" :
39- continue
40- else :
41- t .append (q [0 ])
42- return t
43-
44-
45- def get_answers_count () -> List :
46- """
47- Return [answer_questions,all_questions] ,PASS complete. FAIL incomplete.
48- >>> get_answers_count()
49- [463, 463]
50- """
51- ans_questions = get_answered_questions (get_file_list ())
52- len_ans_questions = len (ans_questions )
53- all_questions = get_question_list (get_file_list ())
54- len_all_questions = len (all_questions )
55- return [len_ans_questions , len_all_questions ]
10+ README_PATH = pathlib .Path (__file__ ).parent .parent / "README.md"
11+ EXERCISES_PATH = pathlib .Path (__file__ ).parent .parent / "exercises"
12+
13+ DETAILS_PATTERN = re .compile (r"<details>(.*?)</details>" , re .DOTALL )
14+ SUMMARY_PATTERN = re .compile (r"<summary>(.*?)</summary>" , re .DOTALL )
15+ B_PATTERN = re .compile (r"<b>(.*?)</b>" , re .DOTALL )
16+
17+
18+ def get_file_content () -> str :
19+ with README_PATH .open ("r" , encoding = "utf-8" ) as f :
20+ return f .read ()
21+
22+
23+ def get_question_list (file_content : str ) -> List [str ]:
24+ details = DETAILS_PATTERN .findall (file_content )
25+ return [SUMMARY_PATTERN .search (detail ).group (1 ) for detail in details if SUMMARY_PATTERN .search (detail )]
26+
27+
28+ def get_answered_questions (file_content : str ) -> List [str ]:
29+ details = DETAILS_PATTERN .findall (file_content )
30+ answered = []
31+ for detail in details :
32+ summary_match = SUMMARY_PATTERN .search (detail )
33+ b_match = B_PATTERN .search (detail )
34+ if summary_match and b_match and summary_match .group (1 ).strip () and b_match .group (1 ).strip ():
35+ answered .append (summary_match .group (1 ))
36+ return answered
37+
38+
39+ def get_answers_count () -> List [int ]:
40+ file_content = get_file_content ()
41+ answered = get_answered_questions (file_content )
42+ all_questions = get_question_list (file_content )
43+ return [len (answered ), len (all_questions )]
5644
5745
5846def get_challenges_count () -> int :
59- challenges_path = (
60- pathlib .Path (__file__ ).parent .parent .joinpath ("exercises" ).glob ("*.md" )
61- )
62- return len (list (challenges_path ))
47+ return len (list (EXERCISES_PATH .glob ("*.md" )))
6348
6449
65- # WIP WAITING FEEDBACK
66- def get_random_question (question_list : List [str ], with_answer = False ):
50+ def get_random_question (question_list : List [str ], with_answer : bool = False ) -> str :
6751 if with_answer :
68- return choice (get_answered_questions (question_list ))
69- return choice (get_question_list (question_list ))
52+ return choice (get_answered_questions (get_file_content () ))
53+ return choice (get_question_list (get_file_content () ))
7054
7155
7256"""Use this question_list. Unless you have already opened/worked/need the file, then don't or
7357you will end up doing the same thing twice.
7458eg:
7559#my_dir/main.py
7660from scripts import question_utils
77- print(question_utils.get_answered_questions(question_utils.question_list )
61+ print(question_utils.get_answered_questions(question_utils.get_question_list(question_utils.get_file_content()) )
7862>> 123
7963 # noqa: E501
8064"""
@@ -83,7 +67,3 @@ def get_random_question(question_list: List[str], with_answer=False):
8367 import doctest
8468
8569 doctest .testmod ()
86- # print(get_question_list(get_file_list()))
87- # print(get_answered_questions(get_file_list()))
88- # print(get_random_question(get_file_list(),True))
89- # print(get_random_question(get_file_list(),False))
0 commit comments