-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_db_mange.py
More file actions
162 lines (128 loc) · 5.55 KB
/
student_db_mange.py
File metadata and controls
162 lines (128 loc) · 5.55 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pandas as pd
import os
class StudentDataBase:
def __init__(self,filename='student-dataset.csv'):
self.filename = filename
self.clean_data_base()
self.add_avg()
self.save()
def clean_data_base(self):
'''
clean the Data set from col (info) that i dont care.
We have 2 Option:
Option 1: give the header of the col and remove it with columns
Option 2: give the index of col we want to remove
'''
self.students= pd.read_csv(self.filename)
# Remvoe those col in the begnning (and check if it's not already if i rerun it..)
columns_to_drop = ['latitude', 'longitude', 'ethnic.group', 'language.grade', 'portfolio.rating', 'coverletter.rating', 'refletter.rating']
existing_columns_to_drop = [col for col in columns_to_drop if col in self.students.columns]
if existing_columns_to_drop:
self.students.drop(columns=existing_columns_to_drop, inplace=True)
# removing with Index
# indices_to_drop = list(range(8, 15)) + list(range(3, 6))
# existing_indices_to_drop = [idx for idx in indices_to_drop if idx in self.students.index]
# if existing_indices_to_drop:
# self.students.drop(index=existing_indices_to_drop, inplace=True)
# Mix the removing:
# list_idx_remove = list(range(8,13)) + list(range(3,5))
def save(self):
self.students.to_csv(self.filename,index=False)
def add_avg(self):
self.students['Average Grade'] = self.students[["math.grade", "english.grade", "sciences.grade"]].mean(axis=1)
def removre_student(self,name):
self.students = self.students[self.students['name'] != name]
# For memory efficiency :
#self.students.drop(self.students[self.students["name"] == name].index, inplace=True)
def update_grade(self,name,english = None ,math=None, science = None):
if self.students[self.students['name'] == name].empty:
print("No such student exist")
return
if english:
self.students.loc[self.students['name'] == name ,'english.grade'] = english
if math:
self.students.loc[self.students['name'] == name ,'math.grade'] = math
if science:
self.students.loc[self.students['name'] == name ,'sciences.grade'] = science
self.save()
def get_student_as_panda(self,id):
'''
returning a panda type
'''
student_data = self.students[self.students['id'] == id]
if student_data.empty:
return None
return student_data
def get_student_as_Student_class(self,id):
'''
using argws to pass as arguments to the constructor for the Student class (**stu_dic)
'''
student_data = self.students[self.students['id'] == id]
if student_data.empty:
return None
stu_dic = student_data.iloc[0].to_dict()
# Replace the key "english.grade" with "english"
if 'english.grade' in stu_dic:
stu_dic['english'] = stu_dic.pop('english.grade')
if 'math.grade' in stu_dic:
stu_dic['math'] = stu_dic.pop('math.grade')
if 'sciences.grade' in stu_dic:
stu_dic['science'] = stu_dic.pop('sciences.grade')
stu_dic.pop('Average Grade')
print(stu_dic)
return Student(**stu_dic)
def get_database(self):
return self.students
def count_country(self, nationality):
'''
Option 1: shape will return the dimension of the Data, so if i sort the shape
of rows will be the number
Option 2: count() will return the summary of each col to the querey is use:
# filtered_df = self.students[self.students['nationality'] == nationality]
# return filtered_df['nationality'].count()
'''
return self.students[self.students['nationality'] == nationality].shape[0]
class Student(StudentDataBase) :
def __init__(self,id=None,name=None,nationality=None,city=None,gender=None,age=None,english=None,math=None,science=None):
super().__init__()
self.id = id
self.name = name
self.nationality = nationality
self.city = city
self.age = age
self.gender = gender
self.english_grade = english
self.math_grade = math
self.science_grade = science
if id != None:
self.add_student()
def add_student(self):
temp_db = pd.DataFrame([{
"id" : self.get_next_id(),
"name": self.name,
"nationality": self.nationality,
"city": self.city,
"gender": self.gender,
"age": self.age,
"english.grade": self.english_grade,
"math.grade": self.math_grade,
"sciences.grade": self.science_grade,
"Average Grade" : self.avg()
}])
self.students = pd.concat([self.students,temp_db ] , ignore_index=True)
self.save()
def __str__(self):
return f"Student name {self.name} have GPA of {self.avg()}"
def get_next_id(self):
if self.students.empty:
return 1
else:
return self.students['id'].max()+1
def avg(self):
return (self.math_grade + self.english_grade + self.science_grade) / 3
DB = StudentDataBase()
Student(12504,"John Doe", "USA", "New York", "Male", 20, 85, 90, 88)
df = DB.get_database()
print(DB.count_country('Mexico'))
stu = DB.get_student_as_Student_class(179)
(print(stu))