poll_awnser.py
1 from ea12lib.types import PollAwnserType 2 3 class TextAwnser: 4 def __init__(self, poll_awnser: PollAwnserType) -> None: 5 self.name: str = poll_awnser["L"] 6 self.original_data: list[list[str]] = [item.split("\x00 ") for item in poll_awnser["N"].split("\x00 ")] 7 self.awnser_value: str = poll_awnser["reponse"]["valeurReponse"] 8 9 class SingleChoiceAwnser: 10 def __init__(self, poll_awnser: PollAwnserType) -> None: 11 self.name: str = poll_awnser["L"] 12 self.original_data: list[list[str]] = [item.split("\x00 ") for item in poll_awnser["N"].split("\x00 ")] 13 self.awnser_index: int = int(poll_awnser["reponse"]["valeurReponse"]["V"].replace("[", "").replace("]", "")) - 1 14 if len(self.original_data) > self.awnser_index: 15 self.awnser_value = self.original_data[self.awnser_index] 16 else: # chosen index is out of range 17 raise IndexError(f"""The chosen index ({self.awnser_index}) is not an index of the list of options ({self.original_data})""") 18 19 class PollAwnsers: 20 def __init__(self, poll_awnsers: list[PollAwnserType]) -> None: 21 self.awnsers: dict[str, TextAwnser | SingleChoiceAwnser] = {} 22 for awnser in poll_awnsers: 23 match awnser["genreReponse"]: 24 case 1: # Text question 25 self.awnsers[awnser["L"]] = TextAwnser(awnser) 26 case 2: # Single choice 27 self.awnsers[awnser["L"]] = SingleChoiceAwnser(awnser) 28 case 3: # Multiple choices 29 raise NotImplementedError(f"""Multiple choices awnser are not implemeted yet (name of the question is : "{awnser["L"]}")""") 30 case _: 31 pass