Passed
Push — main ( 7d42dd...dc9e44 )
by Máté
01:09
created

quiz_helpers.get_question_text()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import re
2
3
from bs4 import Tag
4
5
from src.question import Question
6
7
8
def get_grading_of_question(question: Tag) -> tuple[bool, float, float]:
9
    correctly_answered: bool
10
    
11
    found_tag = question.find("div", class_="grade")
12
    assert isinstance(found_tag, Tag)
13
    
14
    grading_text = found_tag.text
15
    numbers = re.findall(r"\d+\.\d+", grading_text)
16
    grade = float(numbers[0])
17
    maximum_points = float(numbers[1])
18
    if grade == maximum_points:
19
        correctly_answered = True
20
    else:
21
        correctly_answered = False
22
    return correctly_answered, grade, maximum_points
23
24
25
def complete_correct_answers(answer_texts: list[str], correct_answers: list[int], grade: float,
26
                             maximum_points: float, question_text: str) -> None:
27
    print(f"""
28
29
Question: '{question_text}'
30
31
I see that answers {correct_answers} are correct, but this list may be incomplete because you only got {grade:g} points out of {maximum_points:g}.
32
33
The answers are:""")
34
    assert isinstance(answer_texts, list)
35
    # report false positive to mypy developers
36
    for j, answer in enumerate(answer_texts):  # type: ignore
37
        print(f"#{j + 1}\t{answer}")
38
    print()
39
    while True:
40
        additional_correct_answer = input(
41
            f"Please enter a missing correct answer (if there is any remaining) then press Enter: ")
42
        if additional_correct_answer == "" or len(correct_answers) == len(answer_texts) - 1:
43
            break
44
        correct_answers.append(int(additional_correct_answer))
45
46
47
def get_answers(question: Tag) -> tuple[list[str], list[int]]:
48
    answers = question.find("div", class_="answer")
49
    assert isinstance(answers, Tag)
50
    answer_texts: list[str] = []
51
    correct_answers: list[int] = []
52
    i = 1
53
    for answer in answers:
54
        try:
55
            assert isinstance(answer, Tag)
56
        except AssertionError:
57
            continue
58
        found_tag = answer.find("div", class_="ml-1")
59
        assert isinstance(found_tag, Tag)
60
        answer_texts.append(found_tag.text.rstrip("."))
61
        if "correct" in answer["class"]:
62
            correct_answers.append(i)
63
        i += 1
64
    return answer_texts, correct_answers
65
66
67
def get_question_text(question: Tag) -> str:
68
    found_tag = question.find("div", class_="qtext")
69
    assert isinstance(found_tag, Tag)
70
    question_text = found_tag.text
71
    return question_text
72
73
74
def question_already_exists(existing_question: Question, question_text: str) -> bool:
75
    return existing_question.text == question_text
76
77
78
def add_answers_to_existing_question(answer_texts: list[str], correct_answers: list[int],
79
                                     existing_question: Question) -> None:
80
    # report false positive to mypy developers
81
    for k, answer in enumerate(answer_texts):  # type: ignore
82
        if answer not in existing_question.answers:
83
            assert isinstance(answer, str)
84
            existing_question.answers.append(answer)
85
            if k + 1 in correct_answers:
86
                existing_question.correct_answers.append(len(existing_question.answers))
87