Skip to content

Commit

Permalink
📤 Extracted methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gy-mate committed Jun 4, 2024
1 parent 549f4cc commit 28dabd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
14 changes: 5 additions & 9 deletions src/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from src.grading_types import GradingType
from src.question import Question
from src.question_types import QuestionType
from src.quiz_helpers import complete_correct_answers, get_answers, get_grading_of_question, get_question_text
from src.quiz_helpers import add_answers_to_existing_question, complete_correct_answers, get_answers, \
get_grading_of_question, get_question_text, \
question_already_exists


class Quiz:
Expand Down Expand Up @@ -56,14 +58,8 @@ def import_question(self, question: Tag) -> None:
def add_question_no_duplicates(self, answer_texts: list[str], correct_answers: list[int], has_illustration: bool,
question_text: str) -> None:
for existing_question in self.questions:
if existing_question.text == question_text:
# report false positive to mypy developers
for k, answer in enumerate(answer_texts): # type: ignore
if answer not in existing_question.answers:
assert isinstance(answer, str)
existing_question.answers.append(answer)
if k + 1 in correct_answers:
existing_question.correct_answers.append(len(existing_question.answers))
if question_already_exists(existing_question, question_text):
add_answers_to_existing_question(answer_texts, correct_answers, existing_question)
break
else:
self.questions.add(
Expand Down
19 changes: 17 additions & 2 deletions src/quiz_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from bs4 import Tag

from src.question import Question


def get_grading_of_question(question: Tag) -> tuple[bool, float, float]:
correctly_answered: bool
Expand All @@ -13,12 +15,10 @@ def get_grading_of_question(question: Tag) -> tuple[bool, float, float]:
numbers = re.findall(r"\d+\.\d+", grading_text)
grade = float(numbers[0])
maximum_points = float(numbers[1])

if grade == maximum_points:
correctly_answered = True
else:
correctly_answered = False

return correctly_answered, grade, maximum_points


Expand Down Expand Up @@ -69,3 +69,18 @@ def get_question_text(question: Tag) -> str:
assert isinstance(found_tag, Tag)
question_text = found_tag.text
return question_text


def question_already_exists(existing_question: Question, question_text: str) -> bool:
return existing_question.text == question_text


def add_answers_to_existing_question(answer_texts: list[str], correct_answers: list[int],
existing_question: Question) -> None:
# report false positive to mypy developers
for k, answer in enumerate(answer_texts): # type: ignore
if answer not in existing_question.answers:
assert isinstance(answer, str)
existing_question.answers.append(answer)
if k + 1 in correct_answers:
existing_question.correct_answers.append(len(existing_question.answers))

0 comments on commit 28dabd1

Please sign in to comment.