-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACB.py
519 lines (446 loc) · 19.5 KB
/
ACB.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
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
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem
import pandas as pd
import copy
from os import path
import sqlalchemy
#replace xxxx with password in the following line
engine = sqlalchemy.create_engine('mysql+pymysql://root:xxxx@localhost/acbtask1')
#MySQL database used for the task is named acbtask1
from ui import Ui_MainWindow, Ui_SecondPage, Ui_PreReq
ACB_BACKLOG_LIST = 'ACBBACKLOG.xls'
PENDING_COURSE_LIST = 'Pending Courses.xls'
PREREQ_LIST = 'Pre-requisite.xlsx'
TIME_TABLE = 'TIMETABLE.xls'
# PENDING_BACKLOG = "Pending Backlog.xls" #delete this after changing Pending Courses.xls or ACBBACKLOG.xls
#delete PENDING_BACKLOG_DF and DB after changing Pending Courses.xls or ACBBACKLOG.xls as per comment present above from shared version
REGISTRATION_DATA = "Registration Data Sem II 2019-20(13.02.2020).xls"
global PENDING_BACKLOG_DF
main_win = None
prereqWindow = None
student_id = None
student_name = None
courseDict = {}
studentIDS = []
electives = []
class MainWindow:
def __init__(self, studentIDS):
global prereq
global isPrereqFor
isPrereqFor = {}
prereq = {}
self.main_win = QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.main_win)
self.ui.listWidget.addItems(studentIDS)
self.ui.pushButton.clicked.connect(self.pushButton_click)
prereq_file = pd.read_excel(PREREQ_LIST, skiprows=1)
self.formPrereq()
def show(self):
self.main_win.show()
def pushButton_click(self):
global student_id
student_id = self.ui.listWidget.currentItem().text().split('-')[1]
global student_name
student_name = self.ui.listWidget.currentItem().text().split('-')[0]
global main_win
main_win = SecondPage()
main_win.show()
def formPrereq(self):
prereq_file = pd.read_excel(PREREQ_LIST, skiprows=1)
or_and = ""
for i in range(len(prereq_file['Subject'])): #all 671 rows
course1 = prereq_file['Subject'][i].strip(
) + prereq_file['Catalog'][i].strip() #eg. BIOF244
if str(prereq_file['preq1 subject'][i]) == 'nan':
continue
l = prereq_file['preq1 subject'][i].strip(
) + prereq_file['preq1 catalog'][i].strip()
prereq[course1] = [l]
if l not in isPrereqFor:
isPrereqFor[l] = [course1]
else:
isPrereqFor[l].append(course1)
#######################################################################
if str(prereq_file['preq2 sub'][i]) == 'nan':
continue
l = prereq_file['preq2 sub'][i].strip(
) + prereq_file['preq2 cat'][i].strip()
prereq[course1].append(l)
if l not in isPrereqFor:
isPrereqFor[l] = [course1]
else:
isPrereqFor[l].append(course1)
#######################################################################
if str(prereq_file['preq3 no'][i]) == 'nan':
continue
l = prereq_file['preq3 no'][i].strip(
) + prereq_file['preq3 cat'][i].strip()
prereq[course1].append(l)
if l not in isPrereqFor:
isPrereqFor[l] = [course1]
else:
isPrereqFor[l].append(course1)
#######################################################################
if str(prereq_file['preq4 no'][i]) == 'nan':
continue
l = prereq_file['preq4 no'][i].strip(
) + prereq_file['preq4 cat'][i].strip()
prereq[course1].append(l)
if l not in isPrereqFor:
isPrereqFor[l] = [course1]
else:
isPrereqFor[l].append(course1)
class SecondPage:
def __init__(self):
self.main_win = QMainWindow()
self.ui = Ui_SecondPage()
self.ui.setupUi(self.main_win)
self.tableSlotClash = 0
if student_id not in courseDict:
courseDict[student_id] = ["No pending courses"]
timetable = pd.read_excel(TIME_TABLE)
global courseDictWithoutName
courseDictWithoutName = [] # {CSF241,EEEF241}
global courseToSections # {CSF241: ["M-2","W-2"]}
global courseToSectionsNotSelected
global exam
global count
global courseNameForPreq
global electives
electives = []
courseNameForPreq = ""
count = {}
exam = {}
courseToSections = {}
for i in courseDict[student_id]:
courseDictWithoutName.append(i.split('-')[0]) # csf221
self.ui.listWidget_1.addItems(courseDict[student_id])
electives += courseDictWithoutName
for i in range(len(timetable['Course ID'])): #all entries
subject = timetable['Subject'][i]
catalog = timetable['Catalog'][i].strip()
title = timetable['Course Title'][i].strip()
if (subject + catalog) not in electives:
electives.append(subject + catalog)
self.ui.listWidget_1.addItems([subject + catalog + "-" + title])
self.ui.label_2.setText(student_name + " (" + student_id +")")
for i in electives:
count[i] = 0
# timetable = pd.read_excel('sem 2 15-16tt-24 FEB 16.xlsx', skiprows=2)
# for i in range(len(timetable['DAYS'])):
# c = timetable['COURSENO'][i].split()[0]+timetable['COURSENO'][i].split()[1]
# if c in courseDictWithoutName:
# key = c+'-'+str(timetable['STAT'][i])+str(timetable['SEC'][i])
# courseToSections[key] = timetable['DAYS'][i].split()
for i in range(len(timetable['Exam Tm Cd'])):
c = timetable['Subject'][i] + timetable['Catalog'][i].strip()
if c in exam or str(timetable['Exam Tm Cd'][i]) == 'nan':
continue
exam[c] = timetable['Exam Tm Cd'][i]
self.sectionToClassnbr = {}
for i in range(len(timetable['Course ID'])):
c = timetable['Subject'][i] + timetable['Catalog'][i].strip() + '-' + timetable['Section'][i].strip()
self.sectionToClassnbr[c] = timetable['Class Nbr'][i]
for i in range(len(timetable['Course ID'])):
c = timetable['Subject'][i] + timetable['Catalog'][i].strip()
if c in electives:
key = c + '-' + str(timetable['Section'][i])
if (key not in electives) and (key not in courseToSections):
courseToSections[key] = set()
if str(timetable['Mtg Start'][i]) == 'nan':
continue
l = self.giveTime(
timetable['Mtg Start'][i],
timetable['End time'][i],
timetable['Class Pattern'][i])
# print(l)
for el in l:
courseToSections[key].add(el)
# print(courseToSections)
courseToSectionsNotSelected = copy.copy(courseToSections)
self.ui.pushButton_back.clicked.connect(self.pushButton_click)
self.ui.listWidget_1.itemClicked.connect(self.courseClick)
self.ui.listWidget.itemClicked.connect(self.sectionClickAdd)
self.ui.listWidget_2.itemClicked.connect(self.sectionClickRemove)
self.ui.pushButton1.clicked.connect(self.showPrereq)
self.ui.pushButton.clicked.connect(self.validate)
global days, tableWidgetMap
tableWidgetMap = {}
days = {'M': 0, 'T': 1, 'W': 2, 'TH': 3, 'F': 4, 'S': 5}
###################################################
#to map student count:
registered = pd.read_excel(REGISTRATION_DATA)
global studentCountDict
studentCountDict = {}
for j in range(len(registered['ID'])): #all rows of registration data
iter_course = registered['Subject'][j].strip(
) + registered['Catalog'][j].strip() #eg. BIOF244
sectionNumber = '' #eg. L12
if str(registered['Lecture Section No'][j]) != 'nan':
sectionNumber = registered['Lecture Section No'][j].strip()
elif str(registered['Practical Section No'][j]) != 'nan':
sectionNumber = registered['Practical Section No'][j].strip()
elif str(registered['Tutorial Section No'][j]) != 'nan':
sectionNumber = registered['Tutorial Section No'][j].strip()
elif str(registered['Project Section No'][j]) != 'nan':
sectionNumber = registered['Project Section No'][j].strip()
elif str(registered['Thesis section'][j]) != 'nan':
sectionNumber = registered['Thesis section'][j].strip()
courseString = iter_course + '-' + sectionNumber
if courseString not in studentCountDict:
studentCountDict[courseString] = 1
else:
studentCountDict[courseString] += 1
##end for loop in registration data
listOfTables = list(pd.read_sql_query('show tables;',engine))
currentTableName = "stu_op" + student_id
if currentTableName in listOfTables: #if table already exists
self.getXLS()
def getXLS(self):
self.setToValidate()
self.ui.listWidgetErrors.clear()
currentTableName = "stu_op" + student_id
oldXLS = pd.read_sql_table(currentTableName,engine)
#oldXLS was the variable name in the version shared for the task
for i in range(len(oldXLS)):
section = oldXLS["Section"][i]
c = section.split('-')[0]
count[c] = count[c] + 1
courseToSectionsNotSelected.pop(section)
self.ui.listWidget_2.addItems([section])
schedule = courseToSections[section]
for s in schedule:
day = int(days[s.split('-')[0]])
slot = int(s.split('-')[1])
if (day, slot - 1) not in tableWidgetMap:
tableWidgetMap[day, slot - 1] = [section]
else:
tableWidgetMap[day, slot - 1].append(section)
self.formTable()
def setToValidate(self):
self.ui.pushButton.setText("Validate")
def validate(self):
if self.ui.pushButton.text() == "Validate":
l1 = self.checkExamClash()
l2 = self.checkPrereqClash()
l = l1 + l2
if self.tableSlotClash == 1:
self.ui.listWidgetErrors.addItems(["Clash in time table slots. Check for Yellow Cells"])
elif len(l) != 0:
self.ui.listWidgetErrors.addItems(l)
else:
self.ui.listWidgetErrors.addItems(
["No errors found. Click Save to generate output"])
self.ui.pushButton.setText("Save")
else:
self.ui.listWidgetErrors.clear()
op = pd.DataFrame(columns=['StudentID', 'StudentName', 'Section', 'ClassNbr'])
for i in range(self.ui.listWidget_2.count()):
courseSection = self.ui.listWidget_2.item(i).text()
op.loc[i] = [student_id, student_name, courseSection, self.sectionToClassnbr[courseSection]]
currentTableName = "stu_op" + student_id
#previously: op.to_excel("stu_op#" + student_id + ".xls", index = False)
op.to_sql(
name=currentTableName, #DB table name for this student
con=engine,
if_exists='replace', #old table will be replaced with new one
index=False
)
self.ui.listWidgetErrors.addItems(["Saved Successfully !!"])
# def generateOutput(self):
def getDays(self, days):
res = []
for i in days:
res += [str(i)]
if i == 'H':
res.pop()
res[-1] += 'H'
return res
def giveTime(self, a, b, c):
l = []
s = int(str(a)[0:2]) - 7
e = int(str(b)[0:2]) - 7
if str(b)[3:5] != '00':
e = e + 1
d = self.getDays(c)
for i in range(s, e):
for j in d:
l.append(j + '-' + str(i))
return l
def show(self):
self.main_win.show()
def pushButton_click(self):
global main_win
main_win = MainWindow(studentIDS)
main_win.show()
def courseClick(self, item):
course = self.ui.listWidget_1.currentItem().text().split('-')[0]
self.ui.listWidget.clear()
f = False
for i in courseToSectionsNotSelected:
if i.split('-')[0] == course:
section_number = i.split('-')[1] #eg. L12
courseString = course + '-' + section_number
if courseString not in studentCountDict:
break
f = True
currentCount = studentCountDict[courseString] #student count in section
outputString = courseString + ':' + str(currentCount)
self.ui.listWidget.addItems([outputString])
if f == False and count[course] == 0:
self.ui.listWidget.addItems(["Course not offered this sem"])
#will execute for courses with zero registrations.
#Needs to be dealt with appropriately
def formTable(self):
self.tableSlotClash = 0
for i in range(6):
for j in range(11):
if (i, j) not in tableWidgetMap:
continue
else:
s = '//'.join(tableWidgetMap[(i, j)])
self.ui.tableWidget.setItem(i, j, QTableWidgetItem(s))
if len(tableWidgetMap[(i, j)]) > 1:
self.tableSlotClash = 1
self.ui.tableWidget.item(
i, j).setBackground(
QtGui.QColor(
255, 255, 0))
def sectionClickAdd(self, item):
self.setToValidate()
self.ui.listWidgetErrors.clear()
#changed the following line
section = self.ui.listWidget.currentItem().text().split(':')[0]
if section == "Course not offered this sem":
return
c = section.split('-')[0]
count[c] = count[c] + 1
courseToSectionsNotSelected.pop(section)
self.ui.listWidget_2.addItems([section])
self.ui.listWidget.takeItem(self.ui.listWidget.currentRow())
schedule = courseToSections[section]
for s in schedule:
day = int(days[s.split('-')[0]])
slot = int(s.split('-')[1])
if (day, slot - 1) not in tableWidgetMap:
tableWidgetMap[day, slot - 1] = [section]
else:
tableWidgetMap[day, slot - 1].append(section)
self.formTable()
def sectionClickRemove(self, item):
self.setToValidate()
self.ui.listWidgetErrors.clear()
section = self.ui.listWidget_2.currentItem().text()
c = section.split('-')[0]
count[c] = count[c] - 1
courseToSectionsNotSelected[section] = courseToSections[section]
self.ui.listWidget_2.takeItem(self.ui.listWidget_2.currentRow())
schedule = courseToSections[section]
for s in schedule:
day = int(days[s.split('-')[0]])
slot = int(s.split('-')[1])
tableWidgetMap[day, slot - 1].remove(section)
if self.ui.listWidget_1.currentItem().text().split(
'-')[0] == section.split('-')[0]:
self.ui.listWidget.addItems([section])
self.formTable()
# def pushButton_prereq(self):
# course = self.ui.listWidget_1.currentItem().text().split('-')[0]
# # print(course)
# prereq_file = pd.read_excel(
# 'Pre-requisite_15-07-2019.xlsx', skiprows=1)
# # print(prereq_file.columns)
# l = ""
# for i in range(len(prereq_file['Subject'])):
# course1 = prereq_file['Subject'][i].strip(
# ) + prereq_file['Catalog'][i].strip()
# if course == course1:
# l = prereq_file['preq1 subject'][i] + \
# prereq_file['preq1 catalog'][i] + prereq_file['pereq1 title '][i]
def checkExamClash(self):
l = []
for i in electives:
for j in electives:
if i == j:
continue
if count[i] >= 1 and count[j] >= 1:
if i in exam and j in exam and exam[i] == exam[j]:
l.append("Exam clash between " + i + " " + j)
elif i not in exam:
self.ui.listWidgetErrors.addItems(["Warning: exam dates for {} not in Timetable. Skipping exam clash for this course".format(i)])
break
return l
def checkPrereqClash(self):
l = []
for i in courseDictWithoutName:
for j in courseDictWithoutName:
if i == j:
continue
if count[i] == 0 and count[j] >= 1 and j in prereq and i in prereq[j]:
l.append(j + " selected but it's prereq " + i + " is not")
return l
def showPrereq(self):
courseNameForPreq = self.ui.listWidget_1.currentItem(
).text().split('-')[0]
global prereqWindow
prereqWindow = PreReq(courseNameForPreq)
prereqWindow.show()
class PreReq:
def __init__(self, courseNameForPreq):
self.main_win = QMainWindow()
self.ui = Ui_PreReq()
self.ui.setupUi(self.main_win)
if courseNameForPreq in prereq:
self.ui.listWidgetLeft.addItems(prereq[courseNameForPreq])
else:
self.ui.listWidgetLeft.addItems([""])
if courseNameForPreq in isPrereqFor:
self.ui.listWidgetRight.addItems(isPrereqFor[courseNameForPreq])
else:
self.ui.listWidgetRight.addItems([""])
def show(self):
self.main_win.show()
def getPendingBacklog():
back_list = pd.read_excel(ACB_BACKLOG_LIST)
pendingDF = pd.read_excel(PENDING_COURSE_LIST, skiprows = 1)
for i in range(len(pendingDF)):
if pendingDF['Campus ID'][i] not in list(back_list['Campus ID']):
pendingDF.drop(i,inplace=True)
pendingDF.to_sql(
name='PENDING_BACKLOG_DB', # database table name
con=engine,
if_exists='replace',
index=False
)
if __name__ == '__main__':
pd.options.mode.chained_assignment = None
app = QApplication(sys.argv)
back_list = pd.read_excel(ACB_BACKLOG_LIST)
listOfTables = list(pd.read_sql_query('show tables;',engine))
if 'PENDING_BACKLOG_DB' not in listOfTables:
getPendingBacklog() #will have to execute always?
merged = pd.read_sql('PENDING_BACKLOG_DB',engine)
for i in range(len(back_list['NAME'])):
if '.' in back_list['NAME'][i]:
back_list['NAME'][i] = back_list['NAME'][i][:len(
back_list['NAME'][i]) - 1]
studentIDS = [
x + "-" + y for x,
y in zip(
back_list['NAME'],
back_list['Campus ID'])]
for i in range(len(merged['Campus ID'])):
if merged['Campus ID'][i] in courseDict:
courseDict[merged['Campus ID'][i]].append(merged['Subject'][i].strip(
) + merged['Catalog'][i].strip() + '-' + merged['Course Name'][i])
else:
courseDict[merged['Campus ID'][i]] = [merged['Subject'][i].strip(
) + merged['Catalog'][i].strip() + '-' + merged['Course Name'][i]]
# print(courseDict)
main_win = MainWindow(studentIDS)
main_win.show()
sys.exit(app.exec_())