-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
104 lines (83 loc) · 3.03 KB
/
helpers.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
90
91
92
93
94
95
96
97
98
99
100
101
import re
import requests
import urllib
from sys import argv
from bs4 import BeautifulSoup
def getSoup(target):
# opening file
with open(target, "r") as f:
data = f.read()
# processing data
soup = BeautifulSoup(data, "lxml")
return soup
# takes regex pattern and Soup object
# returns list of relavent tags
def getTags(pattern, soup):
pattern = re.compile(pattern);
tags = soup.find_all("a", attrs={"name": pattern})
return tags
def getCoursesOffWith(contents):
# checking to see if there are any relevent courses
infoInd = str(contents).find("Prerequisite: ")
if infoInd == -1:
return []
infoInd += len("Prerequisite: ")
connectionInfo = 0
def hasPrereqs(content):
return not (str(content).find("Prerequisite: ")==-1)
def hasOffW(content):
return not (str(content).find("Offered")==-1)
def getRelevantEndInd(strContent, relevantStartInd):
if strContent.find("Offered: jointly") != -1:
return strContent.find("Offered: jointly")
return strContent.find("<br/>", relevantStartInd)
def getRawPrereqList(content):
if not hasPrereqs(content):
return []
strContent = str(content)
relevantStartInd = strContent.find("Prerequisite: ")+len("Prerequisite: ")
relevantEndInd = getRelevantEndInd(strContent, relevantStartInd)
relevantContent = str(content)[relevantStartInd:relevantEndInd]
# splitting into each seperate prereq
return relevantContent.split(";")
def getRegPrereqs(rawPrereqList, coursePatt):
if not rawPrereqList:
return tuple()
regPrereqs = []
for section in rawPrereqList:
# checking for optional prereqs
if "or" not in section:
# append EACH ITEM from the list of options
for item in coursePatt.findall(section):
regPrereqs.append(item.replace(" ", "").lower())
return tuple(regPrereqs)
def getNonDeptPrereqs(rawPrereqList, coursePatt, deptPatt):
nonDeptPrereqs = [];
for section in rawPrereqList:
if "or" not in section:
for item in re.compile(coursePatt).findall(section):
nonDeptPrereqs.append(item);
return nonDeptPrereqs;
def getChoicePrereqs(rawPrereqList, coursePatt):
if not rawPrereqList:
return tuple()
choicePrereqs = []
for section in rawPrereqList:
# checking for optional prereqs
if "or" in section:
# append the ENTIRE tuple of options to the prereq list
options = coursePatt.findall(section)
if options != []:
choicePrereqs.append(tuple(options))
return tuple(choicePrereqs)
def getCourseId(content, dept):
# if content.b:
# defining string with course info
course_str = content.b.string
# finding index of end of course string
course_str_end = course_str.find("(")-1
# finding length of course_id
id_end = len(dept.lower())+4 # length of space and number
# defining course id, ex: CSE 143
course_id = course_str[0:id_end].replace(" ", "").lower()
return course_id