Passed
Push — main ( ae3f1b...549f4c )
by Máté
01:09
created

quiz_helpers.get_grading_of_question()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
rs 9.8
c 0
b 0
f 0
cc 2
nop 1
1
import re
2
3
from bs4 import Tag
4
5
6
def get_grading_of_question(question: Tag) -> tuple[bool, float, float]:
7
    correctly_answered: bool
8
    
9
    found_tag = question.find("div", class_="grade")
10
    assert isinstance(found_tag, Tag)
11
    
12
    grading_text = found_tag.text
13
    numbers = re.findall(r"\d+\.\d+", grading_text)
14
    grade = float(numbers[0])
15
    maximum_points = float(numbers[1])
16
    
17
    if grade == maximum_points:
18
        correctly_answered = True
19
    else:
20
        correctly_answered = False
21
    
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