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

quiz.Quiz.add_question_no_duplicates()   B

Complexity

Conditions 7

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 18
rs 8
c 0
b 0
f 0
cc 7
nop 5
1
import os
2
3
from bs4 import BeautifulSoup, Tag
4
5
from src.grading_types import GradingType
6
from src.question import Question
7
from src.question_types import QuestionType
8
from src.quiz_helpers import complete_correct_answers, get_answers, get_grading_of_question, get_question_text
9
10
11
class Quiz:
12
    def __init__(self, parent_article: str, title: str, grading: GradingType | None = None):
13
        self.parent_article = parent_article
14
        self.title = title
15
        self.grading = grading
16
        
17
        self.questions: set[Question] = set()
18
    
19
    def __str__(self) -> str:
20
        text = f"{{{{Vissza | {self.parent_article}}}}}"
21
        text += f"""
22
23
{{{{Kvízoldal
24
|cím={self.title}"""
25
        if self.grading:
26
            text += f"\n|pontozás={self.grading.value}"
27
        text += "\n}}"
28
        for question in self.questions:
29
            text += f"\n\n\n{question}"
30
        text += "\n"
31
        return text
32
    
33
    def import_files(self, directory: str) -> None:
34
        for subdir, dirs, files in os.walk(directory):
35
            for file in files:
36
                self.import_questions(file, subdir)
37
    
38
    def import_questions(self, file: str, subdir: str) -> None:
39
        file_path = os.path.join(subdir, file)
40
        with open(file_path, "rb") as source_file:
41
            webpage = BeautifulSoup(source_file, "html.parser")
42
            
43
            multichoice_questions = webpage.find_all("div", class_="multichoice")
44
            for question in multichoice_questions:
45
                self.import_question(question)
46
    
47
    def import_question(self, question: Tag) -> None:
48
        correctly_answered, grade, maximum_points = get_grading_of_question(question)
49
        question_text = get_question_text(question)
50
        answer_texts, correct_answers = get_answers(question)
51
        if not correctly_answered:
52
            complete_correct_answers(answer_texts, correct_answers, grade, maximum_points, question_text)
53
        has_illustration = True if question.find("img", class_="img-responsive") else False
54
        self.add_question_no_duplicates(answer_texts, correct_answers, has_illustration, question_text)
55
    
56
    def add_question_no_duplicates(self, answer_texts: list[str], correct_answers: list[int], has_illustration: bool,
57
                                   question_text: str) -> None:
58
        for existing_question in self.questions:
59
            if existing_question.text == question_text:
60
                # report false positive to mypy developers
61
                for k, answer in enumerate(answer_texts):  # type: ignore
62
                    if answer not in existing_question.answers:
63
                        assert isinstance(answer, str)
64
                        existing_question.answers.append(answer)
65
                        if k + 1 in correct_answers:
66
                            existing_question.correct_answers.append(len(existing_question.answers))
67
                break
68
        else:
69
            self.questions.add(
70
                Question(q_type=QuestionType.MultipleChoice, text=question_text,
71
                         illustration=has_illustration,
72
                         answers=answer_texts,
73
                         correct_answers=correct_answers))
74