This repository was archived by the owner on Mar 9, 2025. It is now read-only.
forked from ArturoAmaya/ExploratoryCurricularAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff_prereqs.py
More file actions
348 lines (302 loc) · 11.5 KB
/
diff_prereqs.py
File metadata and controls
348 lines (302 loc) · 11.5 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
"""
This is run by the Makefile.
python3 diff_prereqs.py > reports/output/prereq-diffs-fragment.html
python3 diff_prereqs.py timeline > reports/output/prereq-timeline-fragment.html
"""
from typing import List, NamedTuple, Optional, Tuple, TypeVar, Union
from common_prereqs import parse_int
from parse import prereqs, terms
from parse_defs import CourseCode, Prerequisite, TermCode
Prereqs = List[List[Prerequisite]]
T = TypeVar("T")
def remove_duplicates(ls: List[T]) -> List[T]:
return [item for i, item in enumerate(ls) if item not in ls[0:i]]
course_codes = sorted(
{course for term in terms() for course in prereqs(term).keys()},
key=lambda subject_code: (subject_code.subject, *parse_int(subject_code.number)),
)
# Ignore special and medical summer, which seems to often omit prereqs only for
# them to be readded in fall
term_codes = sorted(
term_code
for term_code in terms()
if term_code.quarter() != "S3" and term_code.quarter() != "SU"
)
def find_requirement_with_course(
prereqs: Prereqs, course_code: CourseCode
) -> Optional[List[Prerequisite]]:
for requirement in prereqs:
for course, _ in requirement:
if course == course_code:
return requirement
class Change(NamedTuple):
unchanged: List[Prerequisite]
flipped_concurrent: List[Prerequisite]
removed: List[Prerequisite]
added: List[Prerequisite]
class Changed(NamedTuple):
term: TermCode
added: Prereqs
removed: Prereqs
changes: List[Change]
class NewCourse(NamedTuple):
term: TermCode
prereqs: Prereqs
class RemovedCourse(NamedTuple):
term: TermCode
Diff = Union[Changed, NewCourse, RemovedCourse]
def diff_prereqs(
term: TermCode, old: Optional[Prereqs], new: Optional[Prereqs]
) -> Optional[Diff]:
if old is None:
if new is None:
return None
else:
return NewCourse(term, new)
elif new is None:
return RemovedCourse(term)
old_only = [req for req in old if req not in new]
new_only = [req for req in new if req not in old]
if not old_only and not new_only:
return None
changes: List[Change] = []
for old_req in old_only[:]:
for course, _ in old_req:
new_req = find_requirement_with_course(new_only, course)
if not new_req:
continue
old_only.remove(old_req)
new_only.remove(new_req)
old_req = old_req.copy()
new_req = new_req.copy()
unchanged: List[Prerequisite] = []
flipped_concurrent: List[Prerequisite] = []
for prereq in old_req[:]:
if prereq in new_req:
old_req.remove(prereq)
new_req.remove(prereq)
unchanged.append(prereq)
continue
for new_prereq in new_req:
if new_prereq.course_code == prereq.course_code:
old_req.remove(prereq)
new_req.remove(new_prereq)
flipped_concurrent.append(new_prereq)
break
changes.append(Change(unchanged, flipped_concurrent, old_req, new_req))
break
return Changed(term, new_only, old_only, changes)
class History(NamedTuple):
course_code: CourseCode
has_changed: bool
prereq_history: List[Tuple[TermCode, Optional[Prereqs]]] = []
diffs: List[Diff] = []
still_exists: bool = False
def get_history(course_code: CourseCode) -> History:
prereq_history = [
(
term_code,
(
remove_duplicates(
[
remove_duplicates(req)
for req in prereqs(term_code)[course_code]
if req
]
)
if course_code in prereqs(term_code)
else None
),
)
for term_code in term_codes
]
diffs: List[Diff] = []
for i, (term_code, reqs) in enumerate(prereq_history):
diff = diff_prereqs(
term_code,
prereq_history[i - 1][1] if i > 0 else None,
reqs,
)
if diff:
diffs.append(diff)
if len(diffs) <= 1:
return History(course_code, False)
still_exists = prereq_history[-1][1] is not None
return History(course_code, True, prereq_history, diffs, still_exists)
def get_changed_courses() -> List[History]:
return [get_history(course_code) for course_code in course_codes]
def print_prereq_diff(course_id: str, diff: Diff) -> None:
if isinstance(diff, NewCourse):
if diff.term != term_codes[0]:
print(f'<h3 id="{course_id}-{diff.term.lower()}">{diff.term}</h3>')
if diff.prereqs:
print("<p>Course introduced requiring:</p>")
else:
print("<p>Course introduced with no prerequisites.</p>")
elif diff.prereqs:
print(f'<p id="{course_id}-{diff.term.lower()}">Originally requiring:</p>')
else:
print(
f'<p id="{course_id}-{diff.term.lower()}">Originally with no prerequisites.</p>'
)
if diff.prereqs:
print('<ul className="changes">')
for req in diff.prereqs:
print(
f'<li className="change-item">{" or ".join(str(alt.course_code) for alt in req)}</li>'
)
print("</ul>")
return
print(f'<h3 id="{course_id}-{diff.term.lower()}">{diff.term} changes</h3>')
if isinstance(diff, RemovedCourse):
print(f"<p>Course removed.</p>")
return
print('<ul className="changes">')
for req in diff.removed:
print(
f'<li className="change-item removed">{" or ".join(str(alt.course_code) for alt in req)}</li>'
)
for unchanged, flipped_concurrent, removed, added in diff.changes:
items = " or ".join(
[str(course) for course, _ in unchanged]
+ [
f'{course} (<span className="change">{"now" if allow_concurrent else "no longer"}</span> allows concurrent)'
for course, allow_concurrent in flipped_concurrent
]
)
if added:
if items:
items += " or "
items += f'<span className="added">{" or ".join(str(course) for course, _ in added)}</span>'
if removed:
if items:
items += " · removed: "
items += f'<span className="removed">{", ".join(str(course) for course, _ in removed)}</span>'
print(f'<li className="change-item changed">{items}</li>')
for req in diff.added:
print(
f'<li className="change-item added">{" or ".join(str(alt.course_code) for alt in req)}</li>'
)
print("</ul>")
def print_diff() -> None:
changed_courses = get_changed_courses()
print("<body>")
print('<nav className="sidebar">')
prev_subj = ""
for course_code, has_changed, *_, still_exists in changed_courses:
if course_code.subject != prev_subj:
if prev_subj:
print("</ul></details>")
print(f"<details><summary>{course_code.subject}</summary><ul>")
prev_subj = course_code.subject
if has_changed:
print(
f'<li><a href="#{"".join(course_code).lower()}">{course_code}</a></li>'
)
else:
print(f'<li title="Prerequisites have not changed.">{course_code}</li>')
print("</ul></details>")
print('<a href="#" className="top-link">↑ Back to top</a>')
print("</nav>")
print('<main className="main">')
print("<h1>Changes made to course prerequisites over time by course</h1>")
print("<p>Only courses whose prerequisites have changed are shown.</p>")
for course_code, has_changed, _, diffs, still_exists in changed_courses:
if not has_changed:
continue
if not still_exists:
print(f"<details><summary>{course_code} no longer exists</summary>")
course_id = "".join(course_code).lower()
print(f'<h2 id="{course_id}">{course_code}</h2>')
for diff in diffs:
print_prereq_diff(course_id, diff)
if not still_exists:
print("</details>")
print("</main>")
print("</body>")
def print_changed_course(
term_code: TermCode, course_code: CourseCode, diff: Changed
) -> None:
added_badge = (
f' <span className="added">+{len(diff.added)}</span>' if diff.added else ""
)
removed_badge = (
f' <span className="removed">−{len(diff.removed)}</span>'
if diff.removed
else ""
)
changed_badge = (
f' <span className="changed">±{len(diff.changes)}</span>'
if diff.changes
else ""
)
print(
f'<li className="course"><a href="./prereq-diffs.html#{"".join(course_code).lower()}-{term_code.lower()}">{course_code}</a>:{added_badge}{removed_badge}{changed_badge}</li>'
)
def print_timeline() -> None:
"""
Can I ask for a companion view/report that does this by term? (e.g. Fa22,
following courses changes, Sp22, following courses changed, Wi22... etc)
"""
changed_courses = get_changed_courses()
terms = " ".join(
f'<a href="#{term_code.lower()}">{term_code}</a>' for term_code in term_codes
)
print(f"<p>Jump to: {terms}</p>")
# Skip first term because we assume FA12 is when the prereqs have always
# existed (it won't print the first time a course's prereqs are added to
# ISIS)
for term_code in term_codes[1:]:
changed = [
(course_code, diff)
for course_code, _, _, diffs, _ in changed_courses
for diff in (
next(
(
diff
for i, diff in enumerate(diffs)
if i > 0 and diff.term == term_code
),
None,
),
)
if diff
]
print(f'<h2 id="{term_code.lower()}">{term_code}</h2>')
if not changed:
print("<p>No prerequisites changed.</p>")
continue
if any(diff for _, diff in changed if isinstance(diff, Changed)):
print("<ul>")
for course_code, diff in changed:
if isinstance(diff, Changed):
print_changed_course(term_code, course_code, diff)
print("</ul>")
new_courses = [
course for course, diff in changed if isinstance(diff, NewCourse)
]
if new_courses:
courses = ", ".join(
f'<a href="./prereq-diffs.html#{"".join(course).lower()}-{term_code.lower()}">{course}</a>'
for course in new_courses
)
print(
f'<p>New courses (<span className="added">+{len(new_courses)}</span>): {courses}</p>'
)
removed_courses = [
course for course, diff in changed if isinstance(diff, RemovedCourse)
]
if removed_courses:
courses = ", ".join(
f'<a href="./prereq-diffs.html#{"".join(course).lower()}-{term_code.lower()}">{course}</a>'
for course in removed_courses
)
print(
f'<p>Removed courses (<span className="removed">-{len(removed_courses)}</span>): {courses}</p>'
)
if __name__ == "__main__":
import sys as sus
if len(sus.argv) > 1:
print_timeline()
else:
print_diff()