def _read_instructors(self, path: str):
        """read each line from instructors file and create instance of class instructor
        and if the file not found or any value error, it raises an exception"""
        try:
            for cwid, name, dept in file_reader(path, 3, sep='\t',
                                                header=True):
                if cwid not in self._instructors:
                    self._instructors[cwid] = Instructor(cwid, name, dept)
                else:
                    print(f" Duplicate instructor {cwid}")

        except (FileNotFoundError, ValueError) as e:
            print(e)
 def _read_students(self, path: str) -> None:
     """ read each line from student file and create instance of class student
     and if the file not found or any value error, it raises an exception """
     try:
         for cwid, name, major in file_reader(path,
                                              3,
                                              sep='\t',
                                              header=True):
             if cwid not in self._students:
                 self._students[cwid] = Student(cwid, name, major)
             else:
                 print(f" Duplicate student {cwid}")
     except (FileNotFoundError, ValueError) as e:
         print(e)
Пример #3
0
 def test_file_reading_gen(self):
     """ Test cases for file reading with header and seperated by | """
     self.assertEqual([
         a for a in file_reader(
             "/Users/anerishah/Desktop/SSW810/WEEK 8/file.txt", 3, "|",
             True)
     ], [("123", "Jin He", "Computer Science"),
         ("234", "Nanda Koka", "Software Engineering"),
         ("345", "Benji Cai", "Software Engineering")])
     self.assertEqual([
         a for a in file_reader(
             "/Users/anerishah/Desktop/SSW810/WEEK 8/file.txt", 3, "|",
             False)
     ], [("CWID", "Name", "Major"), ("123", "Jin He", "Computer Science"),
         ("234", "Nanda Koka", "Software Engineering"),
         ("345", "Benji Cai", "Software Engineering")])
     self.assertEqual([a for a in file_reader("", 3, "|", False)], [])
     self.assertNotEqual([
         a for a in file_reader(
             "/Users/anerishah/Desktop/SSW810/WEEK 8/file.txt", 3, "|",
             False)
     ], [("123", "Jin He", "Computer Science"),
         ("234", "Nanda Koka", "Software Engineering"),
         ("345", "Benji Cai", "Software Engineering")])
    def _read_major(self, path: str) -> None:
        """ read each line from majors file and create instance of class major
        and if the file not found or any value error, it raises an exception """
        try:
            for major, req, course in file_reader(path,
                                                  3,
                                                  sep='\t',
                                                  header=True):
                if major not in self._major:
                    self._major[major] = Major(major)

                self._major[major].add_course_re(req, course)

        except (FileNotFoundError, ValueError) as e:
            print(e)
 def _read_grades(self, path: str) -> None:
     """ read the student_cwid, course, grade, instructor_cwid from the grades file
     and checks for the individual student and the individual instructor.
     Raises an exception when unknown student is found"""
     try:
         for student_cwid, course, grade, instructor_cwid in file_reader(
                 path, 4, sep='\t', header=True):
             try:
                 s: Student = self._students[student_cwid]
                 s.add_course_grade(course, grade)
             except KeyError:
                 print(f"Found grade for unknown student {student_cwid}")
             try:
                 inst: Instructor = self._instructors[instructor_cwid]
                 inst.add_course_student(course)
             except KeyError:
                 print(f"Found grade for unknown student {instructor_cwid}")
     except (FileNotFoundError, ValueError) as e:
         print(e)