-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverifier.py
More file actions
703 lines (661 loc) · 28.3 KB
/
verifier.py
File metadata and controls
703 lines (661 loc) · 28.3 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#!/usr/bin/env python3
import yaml
from pprint import pprint
import os
import sys
import collections
import time
import copy
from . import university_info
MAJORS_LOCATION = "../majors/" # relative path to folder containing the major requirements JSONs
CERTIFICATES_LOCATION = "../certificates/" # relative path to folder containing the certificate requirements JSONs
DEGREES_LOCATION = "../degrees/" # relative path to the folder containing the AB/BSE requirements JSONs
REQ_PATH_SEPARATOR = '//'
# REQ_PATH_PREFIX := <type>//<year>//<dept_code or degree_code or certificate_name>
# Limit the type to 12 characters and the code/name to 100 characters
REQ_PATH_PREFIX = "%.12s" + REQ_PATH_SEPARATOR + "%d" + REQ_PATH_SEPARATOR + "%.100s"
DEFAULT_SCHEDULE = [[]]*8
def check_major(major_name, courses, year):
"""
Returns information about the major requirements satisfied by the courses
given in courses.
:param major_name: the name of the major
:param courses: a list of course-listings
:param year: the user's class year for which to read the requirements
:type major_name: string
:type courses: 2D array
:type year: int
:returns: Whether the major requirements are satisfied
:returns: The list of courses with info about the requirements they satisfy
:returns: A simplified json with info about how much of each requirement is satisfied
:rtype: (bool, dict, dict)
"""
year = int(year)
if year < 2000 or year > 3000:
raise ValueError("Year is invalid.")
if (major_name not in university_info.AB_CONCENTRATIONS
and major_name not in university_info.BSE_CONCENTRATIONS):
raise ValueError("Major code not recognized.")
major_filename = "%s.yaml" % major_name
major_filepath = os.path.join(_get_dir_path(), MAJORS_LOCATION, major_filename)
return check_requirements(major_filepath, courses, year)
def check_degree(degree_name, courses, year):
"""
Returns information about the degree requirements satisfied by the courses
given in courses.
:param degree_name: the name of the degree
:param courses: a list of course-listings
:param year: the user's class year for which to read the requirements
:type degree_name: string
:type courses: 2D array
:type year: int
:returns: Whether the degree requirements are satisfied
:returns: The list of courses with info about the requirements they satisfy
:returns: A simplified json with info about how much of each requirement is satisfied
:rtype: (bool, dict, dict)
"""
year = int(year)
degree_name = degree_name.upper()
if year < 2000 or year > 3000:
raise ValueError("Year is invalid.")
if degree_name not in ["AB", "BSE"]:
raise ValueError("Invalid degree name: %s" % degree_name)
degree_filename = "%s.yaml" % degree_name
degree_filepath = os.path.join(_get_dir_path(), DEGREES_LOCATION, degree_filename)
return check_requirements(degree_filepath, courses, year)
def check_certificate(certificate_name, courses, year):
"""
NOTE: Not yet fully supported. Some certificate specific functionality may not
be present, or may break.
Returns information about the certificate requirements satisfied by the courses
given in courses.
:param certificate_name: the name of the certificate
:param courses: a list of course-listings
:param year: the user's class year for which to read the requirements
:type certificate_name: string
:type courses: 2D array
:type year: int
:returns: Whether the certificate requirements are satisfied
:returns: The list of courses with info about the requirements they satisfy
:returns: A simplified json with info about how much of each requirement is satisfied
:rtype: (bool, dict, dict)
"""
year = int(year)
if year < 2000 or year > 3000:
raise ValueError("Year is invalid.")
if (certificate_name not in university_info.CERTIFICATES):
raise ValueError("Certificate not recognized.")
certificate_filename = "%s.yaml" % certificate_name
certificate_filepath = os.path.join(_get_dir_path(), CERTIFICATES_LOCATION, certificate_filename)
return check_requirements(certificate_filepath, courses, year)
def check_requirements(req_file, courses, year):
"""
Returns information about the requirements satisfied by the courses
given in courses.
:param req_file: the name of a file containing a requirements JSON
:param courses: a list of course-listings
:param year: the user's class year for which to read the requirements
:type req_file: string
:type courses: 2D array
:type year: int
:returns: Whether the requirements are satisfied
:returns: The list of courses with info about the requirements they satisfy
:returns: A simplified json with info about how much of each requirement is satisfied
:rtype: (bool, dict, dict)
"""
with open(req_file, 'r', encoding="utf8") as f:
req = yaml.safe_load(f)
courses = _init_courses(courses, req, year)
req = _init_req(req, year)
_mark_possible_reqs(req, courses)
_assign_settled_courses_to_reqs(req, courses)
_add_course_lists_to_req(req, courses)
formatted_courses = _format_courses_output(courses)
formatted_req = _format_req_output(req)
return formatted_req["satisfied"], formatted_courses, formatted_req
def get_courses_by_path(path):
"""
Returns the sets of all courses and all distribution requirements
in the subtree specified by path as a tuple:
(course_set, dist_req_set)
Note: Sets may contain duplicate courses if a course is listed in multiple
different ways
Note: the path parameter must be a requirement path string that was generated
through calling _init_path_to()
Implementation is sensitive to the path format, which must start with
<type>//<year>//<dept_code>
where the <>'s are replaced with the appropriate values.
:param path: the path identifier of a requirement as generated by _init_path_to()
:type path: string
:raises: ValueError - if the path was not generated by _init_path_to()
:returns: the tuple (course_set, dist_req_set)
:rtype: (set, set)
"""
req_type, year, req_name = path.split(REQ_PATH_SEPARATOR)[:3]
year = int(year)
if year < 2000 or year > 3000:
raise ValueError("Path malformatted.")
if not path.startswith(REQ_PATH_PREFIX % (req_type, year, req_name)):
raise ValueError("Path malformatted.")
if "/" in req_type or "/" in req_name:
raise ValueError("Path malformatted.")
filename = "%s.yaml" % req_name
if req_type == "Major":
if (req_name not in university_info.AB_CONCENTRATIONS and req_name not in university_info.BSE_CONCENTRATIONS):
raise ValueError("Path malformatted.")
req_filepath = os.path.join(_get_dir_path(), MAJORS_LOCATION, filename)
elif req_type == "Certificate":
if req_name not in university_info.CERTIFICATES:
raise ValueError("Path malformatted.")
req_filepath = os.path.join(_get_dir_path(), CERTIFICATES_LOCATION, filename)
elif req_type == "Degree":
if req_name not in ["AB", "BSE"]:
raise ValueError("Path malformatted.")
req_filepath = os.path.join(_get_dir_path(), DEGREES_LOCATION, filename)
else:
raise ValueError("Path malformatted.")
with open(req_filepath, 'r', encoding="utf8") as f:
req = yaml.safe_load(f)
_init_year_switch(req, year)
subreq = _get_req_by_path(req, path, year)
if not subreq:
raise ValueError("Path malformatted: " + path)
return _get_collapsed_course_and_dist_req_sets(subreq)
def _init_req(req, year):
req = copy.deepcopy(req)
_init_year_switch(req, year)
_init_req_fields(req)
_init_min_ALL(req)
_init_double_counting_allowed(req)
_init_path_to(req, year)
return req
def _format_req_output(req):
"""
Enforce the type and order of fields in the req output
"""
output = collections.OrderedDict()
if req["name"] == None: # hidden requirement. Do not show.
return None
for key in [
"name",
"code",
"degree",
"path_to",
"urls"
]:
if key in req:
output[key] = req[key]
if "code" in req and "description" in req:
output["description"] = req["description"]
if "contacts" in req:
output["contacts"] = []
for contact in req["contacts"]:
contact_copy = collections.OrderedDict()
for key in ["type", "name", "email"]:
contact_copy[key] = contact[key]
output["contacts"].append(contact_copy)
for key in [
"explanation",
"pdfs_allowed",
"completed_by_semester"
]:
if key in req:
output[key] = req[key]
output["satisfied"] = (req["min_needed"]-req["count"] <= 0)
for key in ["count", "min_needed", "max_counted"]:
output[key] = req[key]
if "req_list" in req: # internal node. recursively call on children
req_list = []
for subreq in req["req_list"]:
child = _format_req_output(subreq)
if (child != None):
req_list.append(_format_req_output(subreq))
if req_list:
output["req_list"] = req_list
if "settled" in req:
output["settled"] = req["settled"]
if "unsettled" in req:
output["unsettled"] = req["unsettled"]
return output
def _add_course_lists_to_req(req, courses):
"""
Add course lists for each requirement that either
(a) has no subrequirements, or
(b) has hidden subrequirements
"""
include_course_lists = False
if "req_list" in req:
for subreq in req["req_list"]:
if subreq["name"] == None:
include_course_lists = True
else: # recursively for all named requirements
_add_course_lists_to_req(subreq, courses)
else:
include_course_lists = True
if include_course_lists:
req["settled"] = []
req["unsettled"] = []
for sem in courses:
for course in sem:
if req["double_counting_allowed"]:
if len(course["reqs_double_counted"]) > 0:
for path in course["reqs_double_counted"]:
if path.startswith(req["path_to"]):
req["settled"].append(course["name"])
## add to reqs_satisfied because couldn't be added in _assign_settled_courses_to_reqs()
course["reqs_satisfied"].append(req["path_to"])
elif len(course["settled"]) > 0:
for path in course["settled"]:
if path.startswith(req["path_to"]):
req["settled"].append(course["name"])
else:
for path in course["possible_reqs"]:
if path.startswith(req["path_to"]):
req["unsettled"].append(course["name"])
break
def _init_courses(courses, req, year):
if not courses:
courses = DEFAULT_SCHEDULE
else:
courses = copy.deepcopy(courses)
for sem_num,semester in enumerate(courses):
for course in semester:
course["name"] = course["name"].split(':')[0]
course["possible_reqs"] = []
course["reqs_satisfied"] = []
course["reqs_double_counted"] = [] # reqs satisfied for which double counting allowed
course["semester_number"] = sem_num
course["num_settleable"] = 0 # number of reqs to which can be settled. autosettled if 1
if "external" not in course:
course["external"] = False
if "settled" not in course or course["settled"] == None:
course["settled"] = []
elif req["type"] in ["Major", "Degree"]: # filter out irrelevant requirements from list
for path in course["settled"]:
if not path.startswith(REQ_PATH_PREFIX % (req["type"], year, req["code"])):
course["settled"].remove(path)
else: # type must be "Certificate"
for path in course["settled"]:
if not path.startswith(REQ_PATH_PREFIX % (req["type"], year, req["name"])):
course["settled"].remove(path)
return courses
def _format_courses_output(courses):
"""
Enforce the type and order of fields in the courses output
"""
output = []
for i,sem in enumerate(courses):
output.append([])
for j,course in enumerate(sem):
output[i].append(collections.OrderedDict())
for key in ["name", "possible_reqs", "reqs_satisfied"]:
output[i][j][key] = course[key]
if len(course["settled"]) > 0: # only show if non-empty
output[i][j]["settled"] = course["settled"]
return output
def _year_matches_code(year, code):
"""
Returns whether `year` falls in the range specified by `code`
"""
if isinstance(code, int): # explicitly specified year as an int
return year == code
if not code or code.lower() == "default": # empty indicates default case
return True
code = code.replace(" ", "") # strip it of spaces for processing
if code.startswith("<="):
return year <= int(code[2:])
elif code.startswith(">="):
return year >= int(code[2:])
elif code.startswith("<"):
return year < int(code[1:])
elif code.startswith(">"):
return year > int(code[1:])
elif code.startswith("=="):
return year == int(code[2:])
elif code.startswith("!="):
return year != int(code[2:])
elif "-" in code: # range of years (inclusive), such as "2018-2020"
fr, to, *_ = code.split("-")
return year >= int(fr) and year <= int(to)
else: # just the year is the same as ==
return year == int(code)
def _init_year_switch(req, year):
"""
Checks for any year_switch primitives and selects the right subrequirements.
Any requirement that contains a year_switch is overridden by the first
subrequirement of that year_switch whose year code matches the user's
class year.
Fields in req are overridden by any explicitly listed fields of the
overriding subrequirement, but any fields not specified by the
subrequirement remain as is (except the year_switch, which is removed).
If no year code matches, the requirement is unchanged and the year_switch
is just removed (that is, the year_switch is ignored and has no effect).
"""
if "year_switch" in req:
newreq = {}
for subreq in req["year_switch"]:
code = subreq.get("year_code", None) # year_code set to default
if _year_matches_code(year, code):
newreq = subreq
break # stop at the first year code that matches
del req["year_switch"]
if "year_code" in newreq:
del newreq["year_code"]
req.update(newreq) # override with the values from subreq
# Note that the recursive case below can always still happen even if the
# year_switch above was triggered, since it may have just gained a req_list
# from the overriding subrequirement.
if "req_list" in req:
for subreq in req["req_list"]:
_init_year_switch(subreq, year)
def _init_req_fields(req):
"""
Initializes all the counts to zero and ensures that min_needed and
max_counted exist.
"""
req["count"] = 0
if ("name" not in req) or (req["name"] == '') or (req["name"] == None):
req["name"] = None
if "no_req" in req: # enforce that no_req cannot require a non-zero count
req["no_req"] = None # ignore the contents of a no_req
req["min_needed"] = 0
req["max_counted"] = 0
if "min_needed" not in req or req["min_needed"] == None:
if "type" in req: # check for root
req["min_needed"] = "ALL"
else:
req["min_needed"] = 0
if "max_counted" not in req:
req["max_counted"] = None
if "req_list" in req:
for subreq in req["req_list"]:
_init_req_fields(subreq)
elif "course_list" in req and "excluded_course_list" not in req:
req["excluded_course_list"] = []
elif "num_courses" in req:
req["min_needed"] = req["num_courses"]
if "dist_req" in req and isinstance(req["dist_req"], str):
req["dist_req"] = [req["dist_req"]] # backwards compatibility with non-list dist_req
return req
def _init_min_ALL(req):
"""
Replaces every instance of min_needed="ALL" with the actual number.
"""
num_counted_from_below = 0
if "req_list" in req:
for subreq in req["req_list"]:
num_counted_from_below += _init_min_ALL(subreq)
elif "course_list" in req: # written as loop in case other actions neeed later
for _ in req["course_list"]:
num_counted_from_below += 1
elif "dist_req" in req or "num_courses" in req:
if (req["max_counted"]):
num_counted_from_below += req["max_counted"]
if req["min_needed"] == "ALL":
req["min_needed"] = num_counted_from_below
if req["max_counted"] == None:
return num_counted_from_below
else:
return min(req["max_counted"], num_counted_from_below)
def _init_double_counting_allowed(req, from_parent=False):
"""
Initializes the double_counting_allowed field in all subtrees
"""
if "double_counting_allowed" not in req:
req["double_counting_allowed"] = from_parent
if "req_list" in req:
for subreq in req["req_list"]:
_init_double_counting_allowed(subreq, req["double_counting_allowed"])
def _init_path_to(req, year):
"""
Assign a path identifier to each node/subrequirement in the requirements
tree with the properties:
1. The path is unique (no two nodes in the tree have the same path) as long
as no two subrequirements in the same subtree have the same name.
2. The path gives the traversal of the tree needed to reach that node.
"""
if "path_to" not in req: # only for root of the tree
if req["type"] in ["Major", "Degree"]:
req["path_to"] = REQ_PATH_PREFIX % (req["type"], year, req["code"])
else: # type must be "Certificate"
req["path_to"] = REQ_PATH_PREFIX % (req["type"], year, req["name"])
if "req_list" in req:
for i,subreq in enumerate(req["req_list"]):
# the identifier is the req name if present, or otherwise, an identifying number
identifier = ''
if ("name" not in subreq) or (subreq["name"] == '') or (subreq["name"] == None):
identifier = "%03d" % i
else:
identifier = subreq["name"]
subreq["path_to"] = req["path_to"] + REQ_PATH_SEPARATOR + str(identifier)
_init_path_to(subreq, year)
def _json_format(obj):
import json
return json.dumps(obj, sort_keys=False, indent=2, separators=(',', ': ')) + "\n"
def _get_dir_path():
return os.path.dirname(os.path.realpath(__file__))
def _mark_possible_reqs(req, courses):
"""
Finds all the requirements that each course can satisfy.
"""
if "req_list" in req: # recursively check subrequirements
for subreq in req["req_list"]:
_mark_possible_reqs(subreq, courses)
else: # note: course_list and dist_req can both be specified for the same req
if "course_list" in req:
_mark_courses(req, courses)
if "dist_req" in req:
_mark_dist(req, courses)
def _assign_settled_courses_to_reqs(req, courses):
"""
Assigns only settled courses and those that can only satify one requirement,
and updates the appropriate counts.
"""
old_deficit = req["min_needed"] - req["count"]
if (req["max_counted"] != None):
old_available = req["max_counted"] - req["count"]
was_satisfied = (old_deficit <= 0)
newly_satisfied = 0
if "req_list" in req: # recursively check subrequirements
for subreq in req["req_list"]:
newly_satisfied += _assign_settled_courses_to_reqs(subreq, courses)
elif req["double_counting_allowed"]:
newly_satisfied = _mark_all(req, courses)
elif "course_list" in req:
newly_satisfied = _mark_settled(req, courses)
elif "dist_req" in req:
newly_satisfied = _mark_settled(req, courses)
elif "num_courses" in req:
newly_satisfied = _check_degree_progress(req, courses)
req["count"] += newly_satisfied
new_deficit = req["min_needed"] - req["count"]
if (not was_satisfied) and (new_deficit <= 0): # this req just became satisfied
if req["max_counted"] == None: # unlimited
return req["count"]
else:
return min(req["max_counted"],req["count"]) # cut off at max
elif (was_satisfied) and (new_deficit <= 0): # this requirement was already satisfied, but added more
if req["max_counted"] == None: # unlimited
return newly_satisfied
else:
return min(old_available, newly_satisfied) # cut off at old_available
else: # requirement still not satisfied
return 0
def _mark_courses(req, courses):
num_marked = 0
for sem in courses:
for c in sem:
if req["path_to"] in c["possible_reqs"]: # already used
continue
excluded = False
for pattern in req["excluded_course_list"]:
if _course_match(c["name"], pattern):
excluded = True
if excluded: # this course cannot count for this req, so skip it
continue
for pattern in req["course_list"]:
if _course_match(c["name"], pattern):
num_marked += 1
c["possible_reqs"].append(req["path_to"])
if not req["double_counting_allowed"]:
c["num_settleable"] += 1
break
return num_marked
def _mark_dist(req, courses):
num_marked = 0
for sem in courses:
for c in sem:
if req["path_to"] in c["possible_reqs"]: # already used
continue
if c["dist_area"] in req["dist_req"]:
num_marked += 1
c["possible_reqs"].append(req["path_to"])
if not req["double_counting_allowed"]:
c["num_settleable"] += 1
return num_marked
def _mark_settled(req, courses):
"""
Finds and marks all courses in 'courses' that have been settled to
this requirement.
"""
num_marked = 0
for sem in courses:
for c in sem:
if len(c["reqs_satisfied"]) > 0: # already used in some subreq
continue
if len(c["settled"])>0:
for p in c["settled"]: # go through the settled paths
if p in req["path_to"] and (c["external"] or req["path_to"] in c["possible_reqs"]): # c was settled into this requirement
num_marked += 1
c["reqs_satisfied"].append(req["path_to"])
break
elif c["num_settleable"] == 1 and req["path_to"] in c["possible_reqs"]:
num_marked += 1
c["reqs_satisfied"].append(req["path_to"])
c["settled"].append(req["path_to"])
return num_marked
def _mark_all(req, courses):
"""
Finds and marks all courses in 'courses' that satisfy a requirement where
double counting is allowed.
"""
num_marked = 0
for sem in courses:
for c in sem:
if req["path_to"] in c["possible_reqs"]:
num_marked += 1
c["reqs_double_counted"].append(req["path_to"])
elif c["external"]:
for p in c["settled"]:
if p in req["path_to"]:
num_marked += 1
c["reqs_satisfied"].append(req["path_to"])
return num_marked
def _check_degree_progress(req, courses):
"""
Checks whether the correct number of courses have been completed by the
end of semester number 'by_semester' (1-8)
"""
by_semester = req["completed_by_semester"]
num_courses = 0
if by_semester == None or by_semester > len(courses):
by_semester = len(courses)
for i in range(by_semester):
num_courses += len(courses[i])
return num_courses
def _course_match(course_name, pattern):
if type(pattern) is dict:
if len(pattern.items()) > 1:
raise ValueError("Each course must be a separate item in the course list."
" Please split the courses onto individual list items: \n" + str(pattern))
[(pattern, _)] = pattern.items()
pattern = pattern.split(':')[0] # remove course title
pattern = ["".join(p.split()).upper() for p in pattern.split('/')] # split by '/' and
course = ["".join(c.split()).upper() for c in course_name.split('/')] # remove spaces
for c in course:
for p in pattern:
if c == p: # exact name matched
return True
if p[:4] == 'LANG' and c[:3] in university_info.LANG_DEPTS: # language course
if c[3:] == p[4:]: # course numbers match
return True
if (len(p)>4 and p[4] == '*'): # 'LANG*' or 'LANG***'
return True
if (len(c)>=4 and len(p)>5 and
p[5] == '*' and c[3:4] == p[4:5]): # 'LANG1*' or 'LANG1**'
return True
if (len(c)>=5 and len(p)>6 and
p[6] == '*' and c[3:5] == p[4:6]): # 'LANG12*'
return True
if (len(c)>=6 and len(p)>7 and
p[7] == '*' and c[3:6] == p[4:7]): # 'LANG123*'
return True
# non-language course
if (len(c)>=3 and len(p)>3 and
p[3] == '*' and c[:3] == p[:3]): # 'AAA*' or 'AAA***'
return True
if (len(c)>=4 and len(p)>4 and
p[4] == '*' and c[:4] == p[:4]): # 'AAA1*' or 'AAA1**'
return True
if (len(c)>=5 and len(p)>5 and
p[5] == '*' and c[:5] == p[:5]): # 'AAA12*' Note: not currently in format spec
return True
if (len(c)>=6 and len(p)>6 and
p[6] == '*' and c[:6] == p[:6]): # 'AAA123*' matches to 'AAA123C'
return True
return False
def _get_req_by_path(req, path_to, year):
"""
Returns the subrequirement of req that is pointed to by path_to
"""
if "path_to" not in req:
_init_path_to(req, year)
if req["path_to"] == path_to:
return req
if "req_list" in req:
for subreq in req["req_list"]:
result = _get_req_by_path(subreq, path_to, year)
if result:
return result
return None
def _get_collapsed_course_and_dist_req_sets(req):
"""
Returns the sets of all courses and all distribution requirements
in req's subtree as a tuple:
(course_set, dist_req_set)
Note: Sets may contain duplicate courses if a course is listed in multiple
different ways
"""
if "course_list" in req or "dist_req" in req:
course_set = set()
dist_req_set = set()
if "course_list" in req:
for course in req["course_list"]:
course = course.split(':')[0] # strip course name
course_set.add(course)
if "dist_req" in req:
dist = req["dist_req"]
if isinstance(dist, str): # backwards compatibility with non-list dist_req
dist = [dist]
dist_req_set.update(dist)
return (course_set, dist_req_set)
total_course_set = set()
total_dist_req_set = set()
if "req_list" in req:
for subreq in req["req_list"]:
course_set, dist_req_set = _get_collapsed_course_and_dist_req_sets(subreq)
if course_set:
total_course_set |= course_set # union of both sets
if dist_req_set:
total_dist_req_set |= dist_req_set # union of both sets
return (total_course_set, total_dist_req_set)
def main():
with open ("verifier_tests/1.test", "r", encoding="utf8") as f:
major_name = f.readline()[:-1]
year = int(f.readline())
courses = yaml.safe_load(f)
satisfied,courses,major = check_major(major_name,courses,year)
print(_json_format(courses))
print(_json_format(major))
if __name__ == "__main__":
main()