devops-exercises/scripts/question_utils.py

90 lines
2.4 KiB
Python
Raw Normal View History

2020-01-09 12:11:34 +01:00
"""
Question utils functions
"""
import pathlib
from random import choice
from typing import List
import re
2020-01-09 12:11:34 +01:00
p = pathlib.Path(__file__).parent.parent.joinpath("README.md")
2020-01-09 12:11:34 +01:00
def get_file_list():
file_list = ""
with open(p, "rb") as f:
for line in f.readlines():
file_list += line.rstrip().decode()
2020-01-09 12:11:34 +01:00
return file_list
def get_question_list(file_list: List[str]) -> list:
file_list = re.findall("<details>(.*?)</details>", file_list)
2020-01-09 12:11:34 +01:00
questions_list = []
for i in file_list:
q = re.findall(r"<summary>(.*?)</summary>", i)[0]
questions_list.append(q)
2020-01-09 12:11:34 +01:00
return questions_list
def get_answered_questions(question_list: List[str]) -> list:
2020-01-09 12:11:34 +01:00
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
2020-01-09 12:11:34 +01:00
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]
2020-01-09 12:11:34 +01:00
def get_challenges_count() -> int:
challenges_path = (
pathlib.Path(__file__).parent.parent.joinpath("exercises").glob("*.md")
)
2020-01-09 12:11:34 +01:00
return len(list(challenges_path))
# WIP WAITING FEEDBACK
def get_random_question(question_list: List[str], with_answer=False):
2020-01-09 12:11:34 +01:00
if with_answer:
return choice(get_answered_questions(question_list))
return choice(get_question_list(question_list))
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
print(question_utils.get_answered_questions(question_utils.question_list)
>> 123
# noqa: E501
2020-01-09 12:11:34 +01:00
"""
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))