-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddPlanDetails.py
executable file
·233 lines (203 loc) · 8.08 KB
/
AddPlanDetails.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
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
import xlwt
import xlrd
def AddPlanDetails(semesterNew, userID, planID, ignoreSeats=False):
Info_bookName = semesterNew + " Info"
allPlansInfoPath = "./Users/" + userID + "/" + planID + "/"
allPlansInfoDict = readPlanData(allPlansInfoPath, Info_bookName)
if (ignoreSeats):
seatDict = {}
else:
seatName = semesterNew + " Seats"
seatPath = "./Users/" + userID + "/" + planID + "/"
seatDict = readSeatData(seatPath, seatName)
firstRow = ("Section", "Open Seats", "Instructor", "Type", "Location", "Schedule",
"Dates", "Notes", "Semester", "Code", "RMP Score", "Average Score", "Earliest Time", "Latest Time")
preAllocate(allPlansInfoDict, firstRow)
addAverageScores(allPlansInfoDict)
addTimeExtrema(allPlansInfoDict)
addSeats(allPlansInfoDict, seatDict, ignoreSeats)
savePlanData(allPlansInfoDict, allPlansInfoPath, Info_bookName, firstRow)
# def addSeats(allPlansInfoDict):
def readPlanData(path, bookName):
filePath = path + bookName + ".xls"
dataList = {}
book = xlrd.open_workbook(filePath)
sheetNames = book.sheet_names()
for sheetName in sheetNames:
planData = []
sheet = book.sheet_by_name(sheetName)
rows = sheet.nrows
cols = sheet.ncols
for r in range(1, rows): # 从1开始,去掉标题
data = []
for c in range(cols):
data.append(sheet.cell_value(r, c))
planData.append(data)
dataList[sheetName] = planData
return dataList
def readSeatData(path, bookName):
filePath = path + bookName + ".xls"
dataList = {}
book = xlrd.open_workbook(filePath)
sheet = book.sheet_by_name(bookName)
rows = sheet.nrows
cols = sheet.ncols
for r in range(rows): # 从1开始,去掉标题
dataList[sheet.cell_value(r, 0)] = int(sheet.cell_value(r, 1))
return dataList
def preAllocate(allPlansInfoDict, firstRow):
num = len(firstRow)
keys = list(allPlansInfoDict.keys())
length = len(allPlansInfoDict[keys[0]])
for key in keys:
plan = allPlansInfoDict[key]
for i in range(0, length):
section = plan[i]
for j in range(0, num):
if (j >= len(section)):
section.append("")
def addAverageScores(allPlansInfoDict):
# Initialize dictionary for teacher scores
scores = {"Staff": -1}
keys = list(allPlansInfoDict.keys())
length = len(allPlansInfoDict[keys[0]])
# # Set pathway for teachers scores sheet
sheetName = "BU Teachers Scores"
path = "./Semesters/" + sheetName + ".xls"
book = xlrd.open_workbook(path)
sheet = book.sheet_by_name(sheetName)
# Fill dictionary with teacher's names initially pointing to -1
for key in keys:
plan = allPlansInfoDict[key]
for row in range(0, length):
if plan[row][2] not in scores:
professor = plan[row][2]
scores[plan[row][2]] = -1
# Pair the teacher's scores with their scores
for i in range(1, sheet.nrows):
if sheet.cell_value(i, 0) in scores and scores[sheet.cell_value(i, 0)] == -1 and sheet.cell_value(i, 3) != "NS":
scores[sheet.cell_value(i, 0)] = sheet.cell_value(i, 3)
# store teacher's scores in the dictionary
for key in keys:
plan = allPlansInfoDict[key]
for row in range(0, length):
section = plan[row]
professor = section[2]
score = scores[professor]
section[10] = score
# Use the dict to find the average of the teacher's scores
# Doesn't count repeat teacher's names for the same class
# Doesn't count scores of teachers with no scores
courses = {}
avgscores = []
for i in range(0, len(keys)):
for section in allPlansInfoDict[keys[i]]:
courses[section[9]] = {}
for section in allPlansInfoDict[keys[i]]:
courses[section[9]][section[3]] = section[10]
courseNames = list(courses.keys())
num = 0
sum = 0
# see https://www.bu.edu/reg/registration/abbreviations/
# rankedTypeList, from left to right, section types are ranked based on their importance in the course.
# we only consider the score that of the most important section type.
rankedTypeList = ["LEC", "IND", "EXP", "APP",
"DRS", "OTH", "LAB", "PLB", "DIS"]
for name in courseNames:
oneCourse = courses[name]
oneCourseTypes = list(oneCourse.keys())
for type in rankedTypeList:
if (type in oneCourseTypes):
if (oneCourse[type] != -1):
sum += oneCourse[type]
num += 1
if (num != 0):
avgscores.append(sum / num)
else:
avgscores.append(0)
for i in range(0, len(avgscores)):
allPlansInfoDict[keys[i]][0][11] = avgscores[i]
return
def addTimeExtrema(allPlansInfoDict):
# initialized var
keys = list(allPlansInfoDict.keys())
length = len(allPlansInfoDict[keys[0]])
for key in keys:
startMax = 24
endMin = 0
for i in range(0, length):
times = allPlansInfoDict[key][i][5].split(",")
for a in times:
days, start, mix, when2 = a.split(' ')
when1, end = mix.split('-')
hour1, minute1 = start.split(':')
hour2, minute2 = end.split(':')
if when1 == "pm" and int(hour1) != 12:
start_time = int(hour1)+12+int(minute1)/60
else:
start_time = int(hour1)+int(minute1)/60
if when2 == "pm" and int(hour2) != 12:
end_time = int(hour2)+12+int(minute2)/60
else:
end_time = int(hour2)+int(minute2)/60
if (start_time < startMax):
startMax = start_time
if (end_time > endMin):
endMin = end_time
if startMax > 12:
hour1 = int(startMax)-12
when1 = "pm"
else:
hour1 = int(startMax)
when1 = "am"
if endMin > 12:
hour2 = int(endMin)-12
when2 = "pm"
else:
hour2 = int(endMin)
when2 = "am"
minute1 = int((startMax - int(startMax))*60)
minute2 = int((endMin - int(endMin))*60)
# print("max is: ", hour1, ':', minute1,
# when1, " ", hour2, ':', minute2, when2)
timeExteme = [startMax, endMin]
allPlansInfoDict[key][0][12] = startMax
allPlansInfoDict[key][0][13] = endMin
return
def addSeats(allPlansInfoDict, seatDict, ignoreSeats):
planKeys = list(allPlansInfoDict.keys())
sectLen = len(allPlansInfoDict[planKeys[0]])
for planKey in planKeys:
plan = allPlansInfoDict[planKey]
for section in plan:
sectionName = section[9] + "-" + section[0]
if ignoreSeats:
section[1] = 1
else:
section[1] = seatDict[sectionName]
return
def savePlanData(allPlansInfoDict, allPlansInfoPath, Info_bookName, firstRow):
book = xlwt.Workbook(encoding="utf-8") # 创建workbook对象
plans = list(allPlansInfoDict.keys())
for plan in plans:
oneData = allPlansInfoDict[plan]
# 创建表单对象,cell_overwrite_ok允许单元格被覆盖
sheet = book.add_sheet(plan, cell_overwrite_ok=True)
if(firstRow):
for i in range(0, len(firstRow)):
sheet.write(0, i, firstRow[i])
n = 1
else:
n = 0
if(len(oneData) != 0):
for i in range(0, len(oneData)):
data = oneData[i]
for j in range(0, len(data)):
sheet.write(i+n, j, data[j]) # i+n是因为第一行已经有了标题
# print("已保存到第%d条" % (i+1))
savePath = allPlansInfoPath + Info_bookName + "Details.xls"
book.save(savePath)
print("Plan Details Updated!")
if __name__ == "__main__":
AddPlanDetails("2023-SPRG","1","1")
pass