@@ -73,60 +73,43 @@ def build_courses(html: str, course_names: dict) -> list[Course]:
7373
7474 timings = create_timings (table )
7575 rows = table .find_all ('tr' )
76+
7677 for row in rows :
7778 day_cell : PageElement = row .find ('td' , {'valign' : 'top' })
7879 if not day_cell :
7980 continue
80- day = DAYS_MAP .get (day_cell .get_text (), day_cell .get_text ())
81-
8281 # This is the previously parsed course/time slot. Used to merge timeslots occurring adjacent to each other and initialize course objects
82+ day = DAYS_MAP .get (day_cell .get_text (), day_cell .get_text ())
83+
8384 prev : Course | None = None
8485 cells = [cell for cell in row .find_all ('td' ) if cell .attrs .get ('valign' ) != 'top' ]
85-
86+
8687 for index , cell in enumerate (cells ):
87- # Empty cell means either previous class just ended or no class is scheduled
88- if cell . get_text (). strip () == "" :
88+ text = cell . get_text (). strip ()
89+ if not text : # encountered empty cell: either commit the prev course or skip
8990 if prev :
9091 # Previous slot ended
9192 courses .append (prev )
9293 prev = None
9394 continue
94-
95- code = cell .get_text ()[:7 ].strip ()
96- if not code : continue # continue if cell has no course in it
97-
98- # Special exception: CS10003 is the actual code, but it is written as CS10001 in the timetable
99- if code == "CS10001" :
100- code = "CS10003"
101-
102- location = cell .get_text ()[7 :]
103- cell_duration = int (cell .attrs .get ('colspan' ))
104-
105- # Either new class started just after previous one or previous class is continued
106- if prev is not None :
107- if code == prev .code :
108- # Previous class is same as current class, add the duration to the previous class
109- prev .duration += cell_duration
110- else :
111- # The previous class is different from the current one, meaning previous class ended. Clean up, add to
112- # list and initialize a new course object for the current class
95+
96+ code = text [:7 ] if text [:7 ] != "CS10001" else "CS10003" # original code is CS10003 but the timetable has CS10001
97+ location = text [7 :]
98+ cell_duration = int (cell .attrs .get ('colspan' , 1 ))
99+
100+ if prev and code == prev .code : # encounterd a continuation of the previous course
101+ prev .duration += cell_duration
102+ else :
103+ if prev : # encountered a new course, commit the previous course
113104 courses .append (prev )
114- prev = None
115-
116- # Either new class started after break or after a different previous course
117- if prev is None :
118- prev = Course (
119- code = code ,
120- name = course_names [code ],
121- day = day ,
122- start_time = timings [index ],
123- location = location ,
124- duration = cell_duration
125- )
126-
127- # Day ended, add the last class
128- if prev is not None :
105+ # reinstantiate the prev course
106+ prev = Course (code = code , name = course_names .get (code , "" ), day = day ,
107+ start_time = timings [index ],
108+ location = location )
109+ prev .duration = cell_duration
110+
111+ # end of the day: commit the last course
112+ if prev :
129113 courses .append (prev )
130- prev = None
131-
114+
132115 return courses
0 commit comments