Refact: use __post_init__ for dataclass

This commit is contained in:
Bertrand Benjamin 2022-12-21 07:24:48 +01:00
parent 4e8addd6cb
commit 8911f8ddeb
2 changed files with 28 additions and 30 deletions

View File

@ -1,6 +1,6 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass, field
from datetime import date from datetime import date
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
@ -8,13 +8,14 @@ if TYPE_CHECKING:
from backend.model.tribe import Tribe from backend.model.tribe import Tribe
@dataclass
class Assessment: class Assessment:
def __init__(self, name: str, tribe: Tribe, term: int) -> None: name: str
self.tribe = tribe tribe: Tribe
self.term = term term: int
self.name = name exercises: list[Exercise] = field(default_factory=list)
self.exercises = []
def __post_init__(self) -> None:
self.tribe.register_assessment(self) self.tribe.register_assessment(self)
def register_exercise(self, exercise: Exercise): def register_exercise(self, exercise: Exercise):
@ -25,13 +26,14 @@ class Assessment:
return sum([exercise.questions for exercise in self.exercises]) return sum([exercise.questions for exercise in self.exercises])
@dataclass
class Exercise: class Exercise:
def __init__(self, name: str, assessment: Assessment, date: Optional[date]) -> None: name: str
self.name = name assessment: Assessment
self.assessment = assessment date: Optional[date]
self.date = date questions: list[Question] = field(default_factory=list)
self.questions = []
def __post_init__(self) -> None:
self.assessment.register_exercise(self) self.assessment.register_exercise(self)
def register_question(self, question: Question): def register_question(self, question: Question):
@ -50,22 +52,18 @@ class Skill:
description: str description: str
@dataclass
class Question: class Question:
def __init__( name: str
exercise: Exercise
description: str
skill: Skill
domain: Domain
is_leveled: bool
scale: int
def __post_init__(
self, self,
name: str,
exercise: Exercise,
description: str,
skill: Skill,
domain: Domain,
is_leveled: bool,
scale: int,
) -> None: ) -> None:
self.name = name
self.exercise = exercise
self.exercise.register_question(self) self.exercise.register_question(self)
self.description = description
self.skill = skill
self.domain = domain
self.is_leveled = is_leveled
self.scale = scale

View File

@ -9,11 +9,11 @@ if TYPE_CHECKING:
@dataclass @dataclass
class Student: class Student:
def __init__(self, id: str, name: str, tribe: Tribe) -> None: id: str
self.id = id name: str
self.name = name tribe: Tribe
self.tribe = tribe
def __post_init__(self) -> None:
self.tribe.register_student(self) self.tribe.register_student(self)
def __eq__(self, other: object) -> bool: def __eq__(self, other: object) -> bool: