Passed
Push — main ( dc9e44...1e34ac )
by Máté
01:11
created

question_already_exists()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import re
2
3
from bs4 import Tag
4
5
from src.moodle_to_vikwikiquiz.question import Question
6
7
8 View Code Duplication
def get_grading_of_question(question: Tag) -> tuple[bool, float, float]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 View Code Duplication
def complete_correct_answers(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
26
    answer_texts: list[str],
27
    correct_answers: list[int],
28
    grade: float,
29
    maximum_points: float,
30
    question_text: str,
31
) -> None:
32
    print(
33
        f"""
34
35
Question: '{question_text}'
36
37
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}.
38
39
The answers are:"""
40
    )
41
    assert isinstance(answer_texts, list)
42
    # report false positive to mypy developers
43
    for j, answer in enumerate(answer_texts):  # type: ignore
44
        print(f"#{j + 1}\t{answer}")
45
    print()
46
    while True:
47
        additional_correct_answer = input(
48
            f"Please enter a missing correct answer (if there is any remaining) then press Enter: "
49
        )
50
        if (
51
            additional_correct_answer == ""
52
            or len(correct_answers) == len(answer_texts) - 1
53
        ):
54
            break
55
        correct_answers.append(int(additional_correct_answer))
56
57
58 View Code Duplication
def get_answers(question: Tag) -> tuple[list[str], list[int]]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
    answers = question.find("div", class_="answer")
60
    assert isinstance(answers, Tag)
61
    answer_texts: list[str] = []
62
    correct_answers: list[int] = []
63
    i = 1
64
    for answer in answers:
65
        try:
66
            assert isinstance(answer, Tag)
67
        except AssertionError:
68
            continue
69
        found_tag = answer.find("div", class_="ml-1")
70
        assert isinstance(found_tag, Tag)
71
        answer_texts.append(found_tag.text.rstrip("."))
72
        if "correct" in answer["class"]:
73
            correct_answers.append(i)
74
        i += 1
75
    return answer_texts, correct_answers
76
77
78
def get_question_text(question: Tag) -> str:
79
    found_tag = question.find("div", class_="qtext")
80
    assert isinstance(found_tag, Tag)
81
    question_text = found_tag.text
82
    return question_text
83
84
85
def question_already_exists(existing_question: Question, question_text: str) -> bool:
86
    return existing_question.text == question_text
87
88
89 View Code Duplication
def add_answers_to_existing_question(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
90
    answer_texts: list[str], correct_answers: list[int], existing_question: Question
91
) -> None:
92
    # report false positive to mypy developers
93
    for k, answer in enumerate(answer_texts):  # type: ignore
94
        if answer not in existing_question.answers:
95
            assert isinstance(answer, str)
96
            existing_question.answers.append(answer)
97
            if k + 1 in correct_answers:
98
                existing_question.correct_answers.append(len(existing_question.answers))
99