def test_file_reader(self) -> None: """ In this function, we will the file_reader() function """ result = [['123', 'Jin He', 'Computer Science'], ['234', 'Nanda Koka', 'Software Engineering'], ['345', 'Benji Cai', 'Software Engineering']] self.assertEqual( list(file_reader('./student_majors.txt', 3, '|', True)), result) self.assertNotEqual( list(file_reader('./student_majors.txt', 3, '|')), result) with self.assertRaises(ValueError): list(file_reader('./student_majors.txt', 4, '|', True)) with self.assertRaises(FileNotFoundError): list(file_reader('abc.txt', 3, '|', True))
def _read_grades(self) -> None: """ In this method, we will be reading the grades of each student. """ try: for stud_cwid, course, grade, prof_cwid in file_reader( os.path.join(self._path, "grades.txt"), fields=4, sep='\t', header=False): if stud_cwid in self._students: # handle the key error if a new student s: Student = self._students[stud_cwid] s.add_course(course=course, grade=grade) else: raise KeyError(f"No such Student with {stud_cwid}") if prof_cwid in self._instructors: # handle the key error if a new instructor p: Instructor = self._instructors[prof_cwid] p.inst_courses_add(course) else: raise KeyError(f"No such Student with {prof_cwid}") except FileNotFoundError: print(f"Cannot open file at {self._path}") except ValueError: print("Wrong input")
def _read_majors(self) -> None: """ Reading each major and creating its instance """ try: for major, type, course in file_reader(os.path.join(self._path, "majors.txt"), 3, sep='\t', header=True): if major not in self._majors: self._majors[major] = Major(major) self._majors[major].add_course(type, course) except FileNotFoundError: print(f"Cannot open file at {self._path}")
def _read_instructors(self) -> None: """ In this method, we will be reading each student and creating instances of each instructor as soon as it is read. """ try: for cwid, name, dept in file_reader(os.path.join (self._path, "instructors.txt"), 3, sep='|', header=True): if cwid in self._instructors: print("Instructor with CWID is already in the file") self._instructors[cwid] = Instructor(cwid, name, dept) except (FileNotFoundError, ValueError) as e: print(f"Cannot open file at {self._path}") except ValueError: print("Missing field")
def _read_students(self) -> None: """ In this method, we will be reading each student and creating instances of each student as soon as it is read. """ try: for cwid, name, major in file_reader(os.path.join( self._path, "students.txt"), fields=3, sep='\t', header=False): if cwid in self._students: raise KeyError("Student with CWID is already in the file") self._students[cwid] = Student(cwid, name, major) except FileNotFoundError: print(f"Cannot open file at {self._path}") except ValueError: print("Missing field")
def _read_students(self) -> None: """ In this method, we will be reading each student and creating instances of each student as soon as it is read. """ try: for cwid, name, major in file_reader(os.path.join(self._path, "students.txt"), 3, sep=';', header=True): if cwid in self._students: print("Student with CWID is already in the file") required: List[str] = self._majors[major].get_required() electives: List[str] = self._majors[major].get_electives() self._students[cwid] = Student( cwid, name, major, required, electives) except FileNotFoundError: print(f"Cannot open file at {self._path}") except ValueError: print("Missing field")