forked from ArturoAmaya/ExploratoryCurricularAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 2
/
courses_req_by_majors.py
89 lines (76 loc) · 2.46 KB
/
courses_req_by_majors.py
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
"""
Lists what majors require each course. I might've done this already, I forgot.
python3 courses_req_by_majors.py 2022
python3 courses_req_by_majors.py 2022 json > courses_req_by_majors.json
"""
import json
from sys import stdout
from typing import NamedTuple
from parse import major_plans
from parse_defs import CourseCode
from university import university
from util import partition, sorted_dict
class CourseTaker(NamedTuple):
year: int
quarter: int
major_code: str
college_code: str
for_major: bool
MOST = 0.5
'Minimum percentage considered to be "most" colleges/majors (to allow for errors)'
def print_json(year: int) -> None:
# TODO: partition by term
majors = major_plans(year)
courses = partition(
(
course.course_code,
CourseTaker(
course.term_index // 3,
course.term_index % 3,
major_code,
college,
course.for_major,
),
)
for major_code, plans in majors.items()
# Exclude undeclared majors
if len(plans.colleges) >= len(university.college_codes) * MOST
for college in university.college_codes
if college in plans.colleges
for course in plans.plan(college)
if course.course_code
)
json.dump(
{
"colleges": list(university.college_names.items()),
"quarterNames": university.terms,
"courses": [
{
"courseCode": str(course_code),
"takers": takers,
}
for course_code, takers in sorted_dict(courses, key=CourseCode.parts)
],
},
stdout,
)
def print_readable(year: int) -> None:
courses = partition(
(course.course_code, major_code)
for major_code, plans in major_plans(year).items()
for college in plans.colleges
for course in plans.plan(college)
if course.for_major and course.course_code
)
for course_code, major_codes in sorted_dict(courses, key=CourseCode.parts):
majors = ", ".join(sorted(set(major_codes)))
print(f"[{course_code}] {majors}")
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
raise ValueError("Need year: python3 courses_req_by_majors.py <year> (json)")
year = int(sys.argv[1])
if len(sys.argv) > 2 and sys.argv[2] == "json":
print_json(year)
else:
print_readable(year)