-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_grade.py
107 lines (77 loc) · 3.1 KB
/
file_grade.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#NOTE dont forget
# from os import system as s
# s("cls")
class Grade:
def __init__(self, stu_id, crc_code, score):
self.student_id = stu_id
self.course_code = crc_code
self.score = score
def __str__(self) -> str:
return f"{self.student_id} {self.course_code} {self.score}"
class CourseUtil:
def set_file(self, address):
self.f = open(address, 'r+')
self.lines = self.f.readlines()
self.address = address
def load(self, line_number): #[x]
self.f = open(self.address, 'r+')
self.lines = self.f.readlines()
# print('self.lines:',self.lines) #debugger
len_line = len(self.lines)
if line_number <= len_line:
# print('len_line:',len_line) #debugger
# print('line_number:',line_number) #debugger
arbitary_line = self.lines[line_number - 1]
arbitary_line_split = arbitary_line.split(' ')
# print('result load:', arbitary_line_split) #debugger
st = int(arbitary_line_split[0])
co = int(arbitary_line_split[1])
score = float(arbitary_line_split[2])
return (Grade(st, co, score))
else:
return None
def calc_student_average(self, student_id):#[x]
lst_st = []
student_id = str(student_id)
for line in self.lines:
line_word = line.split(' ')[0] #return just student number
if student_id == line_word:
grade = line.split(' ')[2].split("\n")[0]
grade = float(grade)
lst_st.append(grade)
# print('lst_student average is :',lst_st) #debugger
avg_st = sum(lst_st)/len(lst_st)
return avg_st
def calc_course_average(self, course_code):#[x]
lst_co = []
course_code = str(course_code)
for line in self.lines:
line_word = line.split(' ')[1] #return just course number
if course_code == line_word:
grade = line.split(' ')[2].split("\n")[0]
grade = float(grade)
lst_co.append(grade)
# print('lst_course average is :',lst_co) #debugger
if len(lst_co) > 0:
avg_co = sum(lst_co) / len(lst_co)
else:
avg_co = 0
return avg_co
def count(self): #[x]
self.f = open(self.address, 'r+')
self.lines = self.f.readlines()
return len(self.lines)
def save(self, grade):
find = False
grade_str = str(grade)
course_code = grade_str.split(' ')[1]
student_code = grade_str.split(' ')[0]
for line in self.lines:
if line.find(student_code) != -1 and line.find(course_code) != -1:
find = True
if find == False:
self.f.write(f"\n{str(grade)}")
# file = "all_grades.txt"
# x = CourseUtil()
# x.set_file(file)
# print(x.calc_course_average(333))