changing the function a bit (#314)

* changing the function a bit

using regex

* fixing path

fixing path to get README.md

* os module

removing os module

* Update question_utils.py

running flake8
This commit is contained in:
Melany Estevez Camata 2023-02-05 14:51:34 -04:00 committed by GitHub
parent 01e1dddd2f
commit 299a1fa939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,89 +5,85 @@ Question utils functions
import pathlib import pathlib
from random import choice from random import choice
from typing import List from typing import List
import re
p = pathlib.Path(__file__).parent.parent.joinpath('README.md') p = pathlib.Path(__file__).parent.parent.joinpath("README.md")
def get_file_list(): def get_file_list():
with open(p, 'rb') as f: file_list = ""
file_list = [line.rstrip() for line in f.readlines()] with open(p, "rb") as f:
for line in f.readlines():
file_list += line.rstrip().decode()
return file_list return file_list
def get_question_list(file_list: List[bytes]) -> list: def get_question_list(file_list: List[str]) -> list:
file_list = re.findall("<details>(.*?)</details>", file_list)
questions_list = [] questions_list = []
temp = [] for i in file_list:
after_summary_tag = False q = re.findall(r"<summary>(.*?)</summary>", i)[0]
questions_list.append(q)
for line in file_list:
if line.startswith(b'<details>'):
temp.append(line)
after_summary_tag = True
elif after_summary_tag and line != b'' and b'</details>' not in line:
temp.append(line)
elif after_summary_tag and b'</details>' in line:
temp.append(line)
after_summary_tag = False
questions_list.append(temp)
temp = []
return questions_list return questions_list
def get_answered_questions(question_list: List[List[bytes]]) -> list: def get_answered_questions(question_list: List[str]) -> list:
"""Dont let the type hint confuse you, problem of not using classes.
It takes the result of get_question_list(file_list)
Returns a list of questions that are answered.
"""
t = [] t = []
question_list = re.findall("<details>(.*?)</details>", question_list)
for q in question_list: for i in question_list:
q = re.findall(r"<summary>(.*?)</summary>", i)
index = 0 if q and q[0] == "":
continue
for i in q: a = re.findall(r"<b>(.*?)</b>", i)
if b'</summary>' in i: if a and a[0] == "":
index = q.index(i) continue
else:
if q[index+1: len(q) - 1]: t.append(q[0])
t.append(q)
return t 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]
def get_challenges_count() -> int: def get_challenges_count() -> int:
challenges_path = pathlib.Path(__file__).parent.parent.joinpath('exercises').glob('*.md') challenges_path = (
pathlib.Path(__file__).parent.parent.joinpath("exercises").glob("*.md")
)
return len(list(challenges_path)) return len(list(challenges_path))
# WIP WAITING FEEDBACK # WIP WAITING FEEDBACK
def get_random_question(question_list: List[List[bytes]], with_answer=False): 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(question_list))
return choice(question_list) return choice(get_question_list(question_list))
"""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
you will end up doing the same thing twice. 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.question_list)
>> 123 >> 123
# noqa: E501
""" """
question_list = get_question_list(get_file_list()) if __name__ == "__main__":
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))