-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramclasses.py
More file actions
443 lines (333 loc) · 15.9 KB
/
programclasses.py
File metadata and controls
443 lines (333 loc) · 15.9 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
""" Contains the Program Class, Credit Requirement Class and the Limit Class, that all help parse Program data
Also contains a plethora of helper functions
"""
from __future__ import annotations
from typing import Optional, Any, Union
from CourseParse import parse_courses, get_credits, CourseCollection, CourseOr
from initializer import Graph, generate_graph_from_file, CourseVertex, CourseOrVertex, CourseAndVertex, course_in_list
import csv
import os
PRINT = False # bool to check whether to print during doctests
def get_full_courselist(filename: str = 'courses.csv') -> list[str]:
"""Get a list of all course codes"""
courselist = []
with open(filename, "r", newline="", encoding='utf-8') as f:
reader = csv.reader(f, delimiter="|")
for row in reader:
courselist.append(row[0])
return courselist
def get_keyword_courselist(keyword: str, filename: str = 'courses.csv') -> list[str]:
"""Get a list of all course codes given a keyword, ie. 300-level CSC courses by CSC3XX"""
courselist = []
with open(filename, "r", newline="", encoding='utf-8') as f:
reader = csv.reader(f, delimiter="|")
for row in reader:
if row[0][:4] == keyword:
courselist.append(row[0])
return courselist
def get_courses_from_course_list(course_list: list[list[CourseVertex]]) -> list[CourseVertex]:
"""Get a list of Course Vertices from a given Course_list"""
courses = []
for year in course_list:
for course in year:
courses.append(course)
return courses
def get_programs_list() -> dict[str, list[tuple]]:
"""Returns a Dict with Specialist, Major, Minor as keys
and the values as Tuples including the name of the Program and the filepath
"""
programs = {
'Specialist': [],
'Major': [],
'Minor': []
}
for typeP in programs:
progs = os.listdir(os.curdir + f"/Programs/{typeP}/")
for p in progs:
programs[typeP].append((os.curdir + f"/Programs/{typeP}/{p}", p.replace('_', ' ')[:-4]))
return programs
def get_program(progFile: str, progName: str) -> Program:
"""Returns the program based on the progFile and program Name"""
with open(progFile, 'r') as f:
s = f.read()
return Program(s, progName)
class Limit:
"""
Limits on Credit Requirements, it may be a minimum or a maximum limit on given courses.
Instance Attributes:
- courses: list of courses that are part of the limit
- credits: the number of credits it limits
- typemin: Tells the type of the limit, max or min
Representation Invariants:
- self.credits > 0
"""
courses: list[str]
credits: float
typemin: bool
def __init__(self, credits: float, typemin: bool) -> None:
"""Initialize a Limit"""
self.credits = credits
self.typemin = typemin
self.courses = []
def add_courses(self, courses: str) -> None:
"""Add course options to self.courses
if course is like CSC3XX, it will add all 300-level CSC courses"""
if courses[4:6] == 'XX':
self.courses.extend(get_keyword_courselist(courses[:4]))
return
c = parse_courses(courses)
if isinstance(c, CourseCollection):
self.courses.extend(c.courses)
elif c is None:
return
else:
self.courses.append(c)
def check_limit(self, courses: list[str]) -> bool:
"""Makes sure all the limits are being adhered to by a given list of courses"""
credit_count = 0
if self.typemin:
for course in courses:
if course in self.courses:
credit_count += get_credits(course)
return credit_count >= self.credits
for course in courses:
if course in self.courses:
credit_count += get_credits(course)
return credit_count <= self.credits
def __str__(self) -> str:
"""String Representation of the limit"""
return str(self.credits) + f"({'min' if self.typemin else 'max'}): " + str(self.courses)
class CreditRequirement:
"""
Contains Data that helps pick a few courses from self.courses list within self.limits worth self.credits for 3rd and 4th year
Instance Attributes:
- courses: list of courses that are part of the Credit Requirement
- credits: Number of credits that need to be completed
- limits: Various limits applied on the Credit Requirement
Representation Invariants:
- self.credits > 0
"""
courses: list[str]
credits: float
limits: list[Limit]
def __init__(self, credits: float) -> None:
"""Initialize a Credit Requirement"""
self.credits = credits
self.courses = []
self.limits = []
def add_courses(self, courses: str) -> None:
"""Add a course option to the Credit Requirement
If course is like CSC3XX, it will add all 300-level CSC courses
"""
if courses[4:6] == 'XX':
self.courses.extend(get_keyword_courselist(courses[:4]))
return
c = parse_courses(courses)
if isinstance(c, CourseCollection):
self.courses.extend(c.courses)
else:
self.courses.append(c)
def add_exclusions(self, courses: str) -> None:
"""Add exclusion to the course options
Removes Courses from the course list
"""
for course in courses.split(', '):
if course in self.courses:
self.courses.remove(course.strip())
def add_limit(self, courses: list[str], credits: float, limit_type: str = 'min'):
"""Add Limits to the Credit Requirements"""
limit = Limit(credits, limit_type == 'min')
for course in courses:
limit.add_courses(course.strip())
self.limits.append(limit)
def check_validity(self, courses: list, debug: bool = False) -> bool:
"""Checks the validity of the courses and sees if they pass all the requirements and limits
>>> cr = CreditRequirement(5.0)
>>> courses = "CSC3XX, CSC4XX, MAT3XX, MAT4XX, STA3XX, STA4XX, BCB410H1, BCB420H1, BCB330Y1/ BCB430Y1, MAT224H1/ MAT247H1, MAT235Y1/ MAT237Y1/ MAT257Y1".split(', ')
>>> for course in courses:
... cr.add_courses(course)
>>> cr.add_limit(["CSC4XX", "BCB4XX"], 1.5, 'min')
>>> cr.add_limit("MAT3XX, STA4XX, STA3XX, STA4XX".split(", "), 2.0, 'max')
>>> cr.add_limit("CSC490H1, CSC491H1, CSC494H1, CSC495H1, CSC494Y1, BCB330Y1/ BCB430Y1".split(', '), 1.0, 'max')
>>> cr.add_limit("CSC301H1, CSC302H1, CSC318H1, CSC404H1, CSC413H1, CSC417H1, CSC418H1, CSC419H1, CSC420H1, CSC428H1, CSC454H1, CSC485H1, CSC490H1, CSC491H1, CSC494H1, CSC495H1, CSC494Y1".split(', '), 0.5, 'min')
>>> v = cr.check_validity("CSC301H1, CSC302H1, CSC318H1, CSC404H1, CSC413H1, CSC417H1, CSC428H1, CSC419H1, CSC420H1, CSC490H1".split(', '))
>>> v2 = cr.check_validity("MAT354H1, MAT363H1, MAT367H1, MAT377H1, MAT382H1, CSC417H1, CSC428H1, CSC419H1, CSC420H1, CSC490H1".split(', '))
>>> print(v)
True
>>> print(v2)
False
"""
# Check if every course in courses exists as an option
credits = 0.0
for course in courses:
if course not in self.courses:
continue
elif isinstance(course, str):
credits += get_credits(course)
if credits < self.credits:
if PRINT:
print("Inadequate credits requirement:", credits, "/", self.credits)
return False
# raise ValueError
for limit in self.limits:
if not limit.check_limit(courses):
if PRINT:
print("Does not pass limit-", str(limit))
return False
# raise ValueError
return True
def __str__(self) -> str:
"""String representation of the Credit Requirement"""
s = str(self.credits) + ": " + str(self.courses)
for limit in self.limits:
s += '\n\t' + str(limit)
return s
class Program:
"""
Contains Data of all required courses in each year, along with all the Credit Requirements.
Instance Attributes:
- y1: Contains all the courses required by the program in year 1
- y2: Contains all the courses required by the program in year 2
- y3: Contains all the courses required by the program in year 3
- y4: Contains all the courses required by the program in year 4
- credReqs: Contains all the Credit Requirements for the course
- name: Name of the Program
Representation Invariants:
- self.name != ''
"""
y1: Optional[Union[CourseCollection, str]]
y2: Optional[Union[CourseCollection, str]]
y3: Optional[Union[CourseCollection, str]]
y4: Optional[Union[CourseCollection, str]]
creditReqs: list[CreditRequirement]
name: str
def __init__(self, programtxt: str, fname: str = 'Unamed Program') -> None:
"""
Intialize a Program by parsing the programtxt in the following manner:
Line 1: Required Courses in Year 1
Line 2: Required Courses in Year 2
Line 3: Required Courses in Year 3
Line 4: Required Courses in Year 4
Line 4+: Contains Credit Requirements
IF line N is not indented: - New Credit Requirement, with given Credits and Courses at line N
- line N+1 is the exclusions of the Credit Requirement
- indented lines till the next Credit Requirements contain the Limits
IF line is indented: - Line is either exclusion or Limit
Format of Credit Requirement: <No. of Credits>: <List of Courses>
Format of exclusion: exclusion: <List of Courses>
Format of Limit: <No. of Credits>(<Type of Limit>):<List of Courses>
"""
programlst = programtxt.split('\n')
self.y1 = parse_courses(programlst[0])
self.y2 = parse_courses(programlst[1])
self.y3 = parse_courses(programlst[2])
self.y4 = parse_courses(programlst[3])
self.creditReqs = []
self.name = fname
reqs = []
for i, line in enumerate(programlst[4::]):
if not line[0].isspace():
reqs.append(i + 4)
for j in range(len(reqs)):
rlst = programlst[reqs[j]:reqs[j + 1]] if j + 1 < len(reqs) else programlst[reqs[j]:]
colon = rlst[0].index(':')
credits = float(rlst[0][0:colon])
courses = rlst[0][colon + 1:].split(', ')
cr = CreditRequirement(credits)
for course in courses:
cr.add_courses(course.strip())
for line in rlst[1:]:
colon = line.index(':')
if line[:colon].strip() == 'exclusion':
exclusion = line[colon + 1:]
cr.add_exclusions(exclusion)
continue
limit_credits = float(line[:colon - 5].strip())
limit_type = line[colon - 4: colon - 1]
limit_courses = line[colon + 1:].split(', ')
cr.add_limit(limit_courses, limit_credits, limit_type)
self.creditReqs.append(cr)
def __str__(self) -> str:
"""String Representation of Programs"""
s = 'Year 1: ' + str(self.y1) + '\n'
s += 'Year 2: ' + str(self.y2) + '\n'
s += 'Year 3: ' + str(self.y3) + '\n'
s += 'Year 4: ' + str(self.y4) + '\n'
for cr in self.creditReqs:
s += str(cr) + '\n'
return s
def get_required_courses(self, g: Graph) -> list[list[CourseVertex]]:
"""Returns a list of Required Course vertices for each year for self.program"""
if isinstance(self.y1, CourseOr) or isinstance(self.y1, str):
y1 = [g.get_course(self.y1)]
else:
y1 = [g.get_course(c) for c in self.y1.courses if c is not None] if self.y1 is not None else []
if isinstance(self.y2, CourseOr) or isinstance(self.y2, str):
y2 = [g.get_course(self.y2)]
else:
y2 = [g.get_course(c) for c in self.y2.courses if c is not None] if self.y2 is not None else []
if isinstance(self.y3, CourseOr) or isinstance(self.y3, str):
y3 = [g.get_course(self.y3)]
else:
y3 = [g.get_course(c) for c in self.y3.courses if c is not None] if self.y3 is not None else []
if isinstance(self.y4, CourseOr) or isinstance(self.y4, str):
y4 = [g.get_course(self.y4)]
else:
y4 = [g.get_course(c) for c in self.y4.courses if c is not None] if self.y4 is not None else []
return [y1, y2, y3, y4]
def isComplete(self, course_list: list[list[CourseVertex]], g: Graph) -> bool:
"""
Checks whether all required Courses are completed
Checks if all Credit Requirements are completed
Checks if all Limits are adhered to
"""
required = self.get_required_courses(g)
for year in required:
for c in required:
if isinstance(c, CourseVertex):
if not course_in_list(c, course_list):
return False
elif isinstance(c, CourseOrVertex):
if not any([course_in_list(course, course_list) for course in c.get_neighbours()]):
return False
elif isinstance(c, CourseAndVertex):
if not all([course_in_list(course, course_list) for course in c.get_neighbours()]):
return False
for credReq in self.creditReqs:
if not credReq.check_validity([str(course) for course in get_courses_from_course_list(course_list)]):
return False
return True
if __name__ == "__main__":
# cr = CreditRequirement(5.0)
# courses = "CSC3XX, CSC4XX, MAT3XX, MAT4XX, STA3XX, STA4XX, BCB410H1, BCB420H1, BCB330Y1/ BCB430Y1,
# MAT224H1/ MAT247H1, MAT235Y1/ MAT237Y1/ MAT257Y1".split(', ')
# for course in courses:
# cr.add_courses(course)
# cr.add_limit(["CSC4XX", "BCB4XX"], 1.5, 'min')
# cr.add_limit("MAT3XX, STA4XX, STA3XX, STA4XX".split(", "), 2.0, 'max')
# cr.add_limit("CSC490H1, CSC491H1, CSC494H1, CSC495H1, CSC494Y1, BCB330Y1/ BCB430Y1".split(', '), 1.0, 'max')
# cr.add_limit("CSC301H1, CSC302H1, CSC318H1, CSC404H1, CSC413H1, CSC417H1, CSC418H1, CSC419H1, CSC420H1,
# CSC428H1, CSC454H1, CSC485H1, CSC490H1, CSC491H1, CSC494H1, CSC495H1, CSC494Y1".split(', '), 0.5, 'min')
# v = cr.check_validity("CSC301H1, CSC302H1, CSC318H1, CSC404H1, CSC413H1, CSC417H1, CSC428H1, CSC419H1, CSC420H1,
# CSC490H1".split(', '))
# v2 = cr.check_validity("MAT354H1, MAT363H1, MAT367H1, MAT377H1, MAT382H1, CSC417H1, CSC428H1, CSC419H1, CSC420H1,
# CSC490H1".split(', '))
# print(v2)
g = generate_graph_from_file('courses.csv')
CSspec = Program("""CSC110Y1, CSC111H1, (MAT137Y1/ MAT157Y1)
CSC207H1, CSC209H1, (STA237H1/ STA247H1/ STA255H1/ STA257H1), CSC236H1, CSC258H1, CSC263H1/ CSC265H1, MAT223H1/
MAT240H1; STA247H1/ STA237H1/ STA255H1/ STA257H1
CSC369H1, CSC373H1
5.0: CSC3XX, CSC4XX, MAT3XX, MAT4XX, STA3XX, STA4XX, BCB410H1, BCB420H1, BCB330Y1/ BCB430Y1, MAT224H1/ MAT247H1,
MAT235Y1/ MAT237Y1/ MAT257Y1
exclusion: MAT329Y1, MAT390H1, MAT391H1, STA248H1, STA238H1, STA261H1
1.5(min): CSC4XX, BCB4XX
2.0(max): MAT3XX, STA4XX, STA3XX, STA4XX
1.0(max): CSC490H1, CSC491H1, CSC494H1, CSC495H1, CSC494Y1, BCB330Y1/ BCB430Y1
0.5(min): CSC301H1, CSC302H1, CSC318H1, CSC404H1, CSC413H1, CSC417H1, CSC418H1, CSC419H1, CSC420H1, CSC428H1,
CSC454H1, CSC485H1, CSC490H1, CSC491H1, CSC494H1, CSC495H1, CSC494Y1""")
print(CSspec.get_required_courses(g))
# import doctest
# PRINT = False
# doctest.testmod(verbose=True)
# PRINT = True