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