-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.py
More file actions
353 lines (303 loc) · 11.7 KB
/
project1.py
File metadata and controls
353 lines (303 loc) · 11.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""
advanced_student_system.py
Advanced Student Management System (single-file)
Features:
- Student class with per-subject marks
- Gradebook manager with Add/Edit/Delete/Search
- Compute averages, top students, grade distribution
- Save/Load JSON persistence
- Export CSV
- Input validation and helpful prompts
"""
import json
import csv
from typing import Dict, List, Optional, Tuple
class Student:
"""Represents a student with name, roll and a dict of subject->marks."""
def __init__(self, name: str, roll: int, marks: Optional[Dict[str, float]] = None):
self.name = name.strip()
self.roll = int(roll)
self.marks = marks or {} # e.g. {"Math": 85.0, "Physics": 78.5}
def set_mark(self, subject: str, mark: float):
assert 0 <= mark <= 100, "Mark must be between 0 and 100."
self.marks[subject.strip()] = float(mark)
def get_average(self) -> Optional[float]:
if not self.marks:
return None
total = sum(self.marks.values())
return total / len(self.marks)
def to_dict(self) -> Dict:
return {"name": self.name, "roll": self.roll, "marks": self.marks}
@staticmethod
def from_dict(d: Dict):
return Student(name=d["name"], roll=d["roll"], marks=d.get("marks", {}))
def __repr__(self):
avg = self.get_average()
avg_str = f"{avg:.2f}" if avg is not None else "N/A"
return f"Student(name={self.name!r}, roll={self.roll}, avg={avg_str})"
class Gradebook:
"""Manages many Student objects and provides utility operations."""
def __init__(self):
self.students: Dict[int, Student] = {} # key = roll number
# ---------------- Basic CRUD ----------------
def add_student(self, student: Student) -> None:
if student.roll in self.students:
raise ValueError(f"Roll {student.roll} already exists.")
self.students[student.roll] = student
def get_student(self, roll: int) -> Optional[Student]:
return self.students.get(int(roll))
def delete_student(self, roll: int) -> bool:
return self.students.pop(int(roll), None) is not None
def edit_student_name(self, roll: int, new_name: str) -> bool:
s = self.get_student(roll)
if not s:
return False
s.name = new_name.strip()
return True
# --------------- Searching / Listing ---------------
def search_by_name(self, name_substring: str) -> List[Student]:
q = name_substring.strip().lower()
return [s for s in self.students.values() if q in s.name.lower()]
def list_all(self) -> List[Student]:
return sorted(self.students.values(), key=lambda s: s.roll)
# ---------------- Statistics ----------------
def class_average(self) -> Optional[float]:
avgs = [s.get_average() for s in self.students.values() if s.get_average() is not None]
if not avgs:
return None
return sum(avgs) / len(avgs)
def top_n_students(self, n: int = 3) -> List[Tuple[Student, float]]:
scored = [(s, (s.get_average() or 0.0)) for s in self.students.values()]
scored.sort(key=lambda x: x[1], reverse=True)
return scored[:n]
def grade_distribution(self) -> Dict[str, int]:
"""
Returns a grade bucket distribution:
- 'A' : avg >= 80
- 'B' : 60 <= avg < 80
- 'C' : 50 <= avg < 60
- 'D' : 40 <= avg < 50
- 'F' : avg < 40 or no marks
"""
buckets = {"A": 0, "B": 0, "C": 0, "D": 0, "F": 0}
for s in self.students.values():
avg = s.get_average()
if avg is None:
buckets["F"] += 1
elif avg >= 80:
buckets["A"] += 1
elif avg >= 60:
buckets["B"] += 1
elif avg >= 50:
buckets["C"] += 1
elif avg >= 40:
buckets["D"] += 1
else:
buckets["F"] += 1
return buckets
# ---------------- Persistence ----------------
def save_to_json(self, filepath: str) -> None:
payload = [s.to_dict() for s in self.list_all()]
with open(filepath, "w", encoding="utf-8") as f:
json.dump(payload, f, indent=2)
print(f"Saved {len(payload)} students to {filepath}.")
def load_from_json(self, filepath: str) -> None:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
loaded = 0
for item in data:
s = Student.from_dict(item)
self.students[s.roll] = s
loaded += 1
print(f"Loaded {loaded} students from {filepath}.")
def export_to_csv(self, filepath: str) -> None:
# Collect all subjects to make columns consistent
subjects = set()
for s in self.students.values():
subjects.update(s.marks.keys())
subjects = sorted(subjects)
headers = ["roll", "name"] + subjects + ["average"]
with open(filepath, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(headers)
for s in self.list_all():
row = [s.roll, s.name]
for sub in subjects:
row.append(s.marks.get(sub, ""))
avg = s.get_average()
row.append(f"{avg:.2f}" if avg is not None else "")
writer.writerow(row)
print(f"Exported {len(self.students)} students to {filepath}.")
# ----------------- Helper functions for CLI -----------------
def input_int(prompt: str) -> int:
while True:
try:
val = int(input(prompt))
return val
except ValueError:
print("Please enter a valid integer.")
def input_float(prompt: str) -> float:
while True:
try:
val = float(input(prompt))
return val
except ValueError:
print("Please enter a valid number.")
def create_student_interactive() -> Student:
name = input("Name: ").strip()
roll = input_int("Roll (integer): ")
s = Student(name=name, roll=roll)
print("Enter marks for subjects. Type 'done' when finished.")
while True:
subj = input(" Subject name (or 'done'): ").strip()
if subj.lower() == "done":
break
mark_in = input(f" Mark for {subj}: ").strip()
try:
mark = float(mark_in)
s.set_mark(subj, mark)
except (ValueError, AssertionError) as e:
print(" Invalid mark:", e)
return s
def show_student_details(s: Student):
print("--------")
print(f"Name: {s.name}")
print(f"Roll: {s.roll}")
if not s.marks:
print("Marks: No marks recorded.")
else:
for sub, m in s.marks.items():
print(f" {sub}: {m}")
avg = s.get_average()
print(f"Average: {avg:.2f}")
def sample_data() -> Gradebook:
gb = Gradebook()
s1 = Student("Aman Koli", 101, {"Math": 85, "Physics": 78, "Chemistry": 90})
s2 = Student("Bala Rao", 102, {"Math": 65, "Physics": 70})
s3 = Student("Chitra Sen", 103, {"Math": 92, "English": 88, "Biology": 85})
for s in (s1, s2, s3):
gb.add_student(s)
return gb
# ----------------- Command-line menu -----------------
def main_menu():
gb = Gradebook()
print("Would you like to load sample data? (y/n)")
if input().strip().lower() == "y":
gb = sample_data()
print("Sample data loaded.")
while True:
print("\n--- Advanced Student Management ---")
print("1) Add student")
print("2) View all students")
print("3) Search by name")
print("4) View student by roll")
print("5) Edit student name or marks")
print("6) Delete student")
print("7) Class statistics")
print("8) Save to JSON")
print("9) Load from JSON (replace current)")
print("10) Export CSV")
print("11) Exit")
choice = input("Choose (1-11): ").strip()
if choice == "1":
try:
s = create_student_interactive()
gb.add_student(s)
print("Student added.")
except Exception as e:
print("Failed to add student:", e)
elif choice == "2":
all_students = gb.list_all()
if not all_students:
print("No students.")
for s in all_students:
show_student_details(s)
elif choice == "3":
q = input("Enter name substring to search: ").strip()
results = gb.search_by_name(q)
if not results:
print("No matches.")
for s in results:
show_student_details(s)
elif choice == "4":
r = input_int("Enter roll: ")
s = gb.get_student(r)
if not s:
print("Not found.")
else:
show_student_details(s)
elif choice == "5":
r = input_int("Enter roll to edit: ")
s = gb.get_student(r)
if not s:
print("No student with that roll.")
continue
print("1) Edit name")
print("2) Add/Edit marks")
print("3) Remove a subject mark")
subchoice = input("Choose (1-3): ").strip()
if subchoice == "1":
newname = input("New name: ").strip()
gb.edit_student_name(r, newname)
print("Name updated.")
elif subchoice == "2":
subj = input("Subject name: ").strip()
try:
mark = input_float("Mark (0-100): ")
s.set_mark(subj, mark)
print("Mark set.")
except AssertionError as e:
print("Error:", e)
elif subchoice == "3":
subj = input("Subject name to remove: ").strip()
if subj in s.marks:
s.marks.pop(subj)
print("Removed.")
else:
print("Subject not found.")
else:
print("Invalid option.")
elif choice == "6":
r = input_int("Enter roll to delete: ")
if gb.delete_student(r):
print("Deleted.")
else:
print("Not found.")
elif choice == "7":
avg = gb.class_average()
if avg is None:
print("No averages available yet.")
else:
print(f"Class average: {avg:.2f}")
print("Top 3 students:")
for s, sc in gb.top_n_students(3):
print(f" Roll {s.roll} - {s.name} => {sc:.2f}")
print("Grade distribution:")
for k, v in gb.grade_distribution().items():
print(f" {k}: {v}")
elif choice == "8":
path = input("JSON filepath to save (e.g. students.json): ").strip()
try:
gb.save_to_json(path)
except Exception as e:
print("Error saving:", e)
elif choice == "9":
path = input("JSON filepath to load (e.g. students.json): ").strip()
try:
gb = Gradebook() # replace current
gb.load_from_json(path)
except Exception as e:
print("Error loading:", e)
elif choice == "10":
path = input("CSV filepath to export (e.g. students.csv): ").strip()
try:
gb.export_to_csv(path)
except Exception as e:
print("Error exporting:", e)
elif choice == "11":
print("Bye!")
break
else:
print("Invalid choice. Please pick 1-11.")
if __name__ == "__main__":
main_menu()