-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.py
51 lines (39 loc) · 1.41 KB
/
question.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations
import re
from typing import Any
from typing import List
from typing import Optional
class Question:
def __init__(self, question: List[str], answers: List[str], category: Optional[str] = None, **kwargs: Any) -> None:
assert len(answers) > 0
self._question = question
self._answers = answers
self._category = category
@property
def question(self) -> str:
return ', '.join(self._question)
@property
def answer(self) -> str:
return self._answers[0]
@property
def answers(self) -> str:
return ', '.join(self._answers)
def get_question(self, category: bool = False) -> str:
if category:
return f'{self.question} ({self._category})'
else:
return self.question
def is_correct(self, answer: str) -> bool:
return answer in [self.get_parenless(answer) for answer in self._answers]
def __str__(self) -> str:
return f'{self.question} - {self.answers}'
def dumps(self) -> str:
return '{question} - {answers}'.format(
question=self.question,
answers=self.answers,
)
def reversed(self) -> 'Question':
return Question(question=self._answers, answers=self._question, category=self._category)
@staticmethod
def get_parenless(expression: str) -> str:
return re.sub(' ?\(\w+\)', '', expression)