/
student.py
84 lines (63 loc) · 2.17 KB
/
student.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from functools import reduce
from subject import Subject, Grading
from constant import students_dict, electives, core_subjects
from typing import TypedDict, List, Dict
class StudentDict(TypedDict):
regno: str
classcode: str
classno: int
chname: str
enname: str
class Term(TypedDict):
score: List[dict]
ratio: float
gradings: List[Grading]
class Performance(TypedDict):
score: float # average score
grade: float # average predicted grade
class Student:
def __init__(self, regno: str):
self.regno = regno
@property
def info(self) -> StudentDict:
return students_dict[self.regno]
@property
def subjects(self) -> List[str]:
return core_subjects + electives[self.regno]
def getPerformances(self, terms: List[Term]) -> Dict[str, Performance]:
result = {}
for subj in self.subjects:
result[subj] = self.getSubjectPerformance(subj, terms)
return result
def getSubjectPerformance(self, subject_id: str, terms: List[Term]) -> Performance:
regno = self.regno
performance: Performance = {"score": 0, "grade": 0}
def reducer(acc, term):
subj = Subject(subject_id)
score = term["score"][regno][subject_id]
gradings = term["gradings"]
ratio = term["ratio"]
grade = subj.scoreToGrade(score, gradings)
acc["score"] += score * ratio
acc["grade"] += grade * ratio
return acc
return reduce(reducer, terms, performance)
if __name__ == "__main__":
from constant import (
f6_report_dict,
f5_term1_report_dict,
f5_term2_report_dict,
gradings_f5_term1,
gradings_f5_term2,
gradings_f6,
)
terms: List[Term] = [
{"score": f6_report_dict, "ratio": 0.5, "gradings": gradings_f6},
{"score": f5_term2_report_dict, "ratio": 0.25, "gradings": gradings_f5_term2},
{"score": f5_term1_report_dict, "ratio": 0.25, "gradings": gradings_f5_term1},
]
sts = Student("1211017")
print(sts.info)
print(sts.subjects)
performances = sts.getPerformances(terms)
print(performances)