This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
grading.py
280 lines (239 loc) · 9.16 KB
/
grading.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
#!/usr/bin/env python3
import sys
from openpyxl import load_workbook
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QComboBox, QPushButton,
QLineEdit, QPlainTextEdit, QGridLayout)
from PyQt5.QtWidgets import (QListWidget)
FILENAME = './COMP8301_1A_2021_Grades.xlsx'
wb = load_workbook(filename=FILENAME)
wb_sheet = wb['Grades']
GET_FIRST_NAME = lambda x: wb_sheet[f'A{x}'].value
GET_LAST_NAME = lambda x: wb_sheet[f'B{x}'].value
GET_ID_NUMBER = lambda x: wb_sheet[f'C{x}'].value
GET_GROUP_NUM = lambda x: int(wb_sheet[f'N{x}'].value)
IDX_RANGE = list( range(2,66) )
IDX_NAMES = ['(%02d) %s %s'%(GET_GROUP_NUM(x), GET_FIRST_NAME(x),GET_LAST_NAME(x)) for x in IDX_RANGE]
class AssignmentItem:
def __init__(self, idx, g_sym, f_sym) -> None:
self.idx = str(idx)
self.g_sym = str(g_sym) + self.idx
self.f_sym = str(f_sym) + self.idx
pass
@property
def grade(self):
return str(wb_sheet[ self.g_sym ].value)
@grade.setter
def grade(self, grade):
try:
wb_sheet[ self.g_sym ] = float(grade)
except:
wb_sheet[ self.g_sym ] = '-'
@property
def feedback(self):
return wb_sheet[ self.f_sym ].value
@feedback.setter
def feedback(self, feedback):
wb_sheet[ self.f_sym ] = str(feedback)
pass
def Assignment(idx):
if idx==1:
g_sym, f_sym = 'G', 'H'
elif idx==2:
g_sym, f_sym = 'I', 'J'
elif idx==3:
g_sym, f_sym = 'K', 'L'
else:
raise Exception('wrong assignment index.')
_map = dict()
for idx in IDX_RANGE:
_map[idx] = AssignmentItem(idx, g_sym, f_sym)
return _map
EX_LIST = [ Assignment(1), Assignment(2), Assignment(3) ]
class KeysReactor():
keySpecs = {
Qt.Key_Control: 0x01,
Qt.Key_Alt: 0x02,
Qt.Key_Shift: 0x04
}
keySpecsKeys = keySpecs.keys()
def __init__(self, parent, name='Default'):
self.name = name
self.key_list = [0x00]
self.reactor = dict()
self.press_hook_pre = None
self.press_hook_post = None
self.release_hook_pre = None
self.release_hook_post = None
if parent:
self.parent = parent
self.super = super(type(parent), parent)
parent.keyPressEvent = lambda e: self.pressed(e.key(), e)
parent.keyReleaseEvent = lambda e: self.released(e.key(), e)
pass
def __str__(self):
ret = []
ret.append('Ctrl') if self.key_list[0]&0x01 else None
ret.append('Alt') if self.key_list[0]&0x02 else None
ret.append('Shift') if self.key_list[0]&0x04 else None
ret += [str(x) for x in self.key_list[1:]]
return '[%s] %s'%(self.name, '+'.join(ret))
def register(self, keys, hookfn):
key_hash = [0x00]
for tmp_key in keys:
if tmp_key in self.keySpecsKeys: # for specific keys
key_hash[0] = key_hash[0] | self.keySpecs[tmp_key]
else: # for general keys
key_hash.append(tmp_key)
pass
key_hash = '_'.join([str(x) for x in key_hash])
self.reactor[key_hash] = hookfn
pass
def pressed(self, key, e=None):
#NOTE: pre hook
if self.press_hook_pre:
self.press_hook_pre(e)
#NOTE: press keys
if key in self.keySpecsKeys:
self.key_list[0] = self.key_list[0] | self.keySpecs[key] #append specific keys
else:
self.key_list.append(key)
key_hash = '_'.join([str(x) for x in self.key_list])
if key_hash in self.reactor:
ret = self.reactor[key_hash]() #unused ret code
else:
self.super.keyPressEvent(e)
#NOTE: post hook
if self.press_hook_post:
self.press_hook_post(e)
pass
def released(self, key, e=None):
#NOTE: pre hook
if self.release_hook_pre:
self.release_hook_pre(e)
#NOTE: remove keys
if key in self.keySpecsKeys: # remove a special key
self.key_list[0] = self.key_list[0] & (~self.keySpecs[key]) #remove specific keys
elif key in self.key_list: # remove a common key
self.key_list.remove(key)
elif key in [Qt.Key_Return, Qt.Key_Enter]: # reset the list
self.key_list = [0x00]
self.super.keyReleaseEvent(e)
if self.key_list[0]==0x00: #reset, if no specific keys presented
self.key_list = [0x00]
#NOTE: post hook
if self.release_hook_post:
self.release_hook_post(e)
pass
def hasSpecsKeys(self):
return self.key_list[0] != 0x00
def clear(self):
self.key_list = [0x00]
pass
def setKeyPressHook(self, press_hook, mode='post'):
assert( mode in ['pre','post','block'] )
setattr(self, f'press_hook_{mode}', press_hook)
pass
def setKeyReleaseHook(self, release_hook, mode='post'):
assert( mode in ['pre','post','block'] )
setattr(self, f'release_hook_{mode}', release_hook)
pass
pass
class GradingWindow(QWidget):
def __init__(self):
super().__init__()
self.grid = QGridLayout()
# Row 0
## uid label
self.uid_label = QLineEdit(parent=self)
self.uid_label.setDisabled(True)
self.grid.addWidget(self.uid_label, 0, 0, 1, 2)
## ex combobox
self.ex_comb = QComboBox()
self.ex_comb.addItems(['ex01', 'ex02', 'ex03'])
self.ex_comb.currentIndexChanged.connect( self.update )
self.grid.addWidget(self.ex_comb, 0, 2, 1, 1)
## name box
self.name_box = QLineEdit(parent=self)
# self.name_box.focusInEvent = lambda _: self.name_box.selectAll()
self.name_box.textChanged.connect( self.update )
self.grid.addWidget(self.name_box, 0, 3, 1, 2)
## Row 1 - 4
## feedback box
self.feedback_box = QPlainTextEdit(self)
self.grid.addWidget(self.feedback_box, 1, 0, 4, 3)
## name list
self.name_list = QListWidget(self)
self.name_list.addItems(IDX_NAMES)
self.name_list.currentRowChanged.connect( self.update_by_selection )
self.grid.addWidget(self.name_list, 1, 3, 4, 2)
## Row 5
self.grade_box = QLineEdit(self)
self.grade_box.mousePressEvent = lambda _:self.grade_box.selectAll()
self.grid.addWidget(self.grade_box, 5, 0, 1, 3)
#
self.save_btn = QPushButton('Save', self)
self.save_btn.clicked.connect( self.save )
self.grid.addWidget(self.save_btn, 5, 3, 1, 2)
# init process
self.init()
#
self.keysFn = KeysReactor(self, 'GradingWindow')
self.keysFn.register([Qt.Key_Control, Qt.Key_L], lambda:self.name_box.setFocus())
self.keysFn.register([Qt.Key_Control, Qt.Key_G], lambda:self.grade_box.setFocus())
self.keysFn.register([Qt.Key_Control, Qt.Key_S], lambda:self.save_btn.click())
#
self.setLayout(self.grid)
self.resize(self.sizeHint())
self.setWindowTitle('Grading Window')
self.setAttribute(Qt.WA_InputMethodEnabled)
self.show()
pass
def init(self):
self.idx = IDX_RANGE[0]
self.update_by_idx(self.idx)
pass
def search(self, name:str):
_name = name.lower()
for i, x in enumerate(IDX_NAMES):
if _name in x.lower():
return IDX_RANGE[i]
pass
return -1
def update_by_idx(self, idx):
_name = '%s %s'%( GET_FIRST_NAME(idx), GET_LAST_NAME(idx) )
_uid = GET_ID_NUMBER(idx)
_ex_idx = self.ex_comb.currentIndex()
_ex_ref = EX_LIST[ _ex_idx ]
#
self.uid_label.setText(f'{_name}, {_uid}')
self.grade_box.setText( _ex_ref[idx].grade )
self.feedback_box.setPlainText( _ex_ref[idx].feedback )
pass
def update(self, *args, **kwargs):
_name = self.name_box.text()
_index = self.search(_name)
if _index>=0:
self.idx = _index
self.name_list.setCurrentRow( IDX_RANGE.index(_index) )
self.update_by_idx(self.idx)
pass
def update_by_selection(self, *args, **kwargs):
_index = IDX_RANGE[ self.name_list.currentRow() ]
self.idx = _index
self.update_by_idx(self.idx)
def save(self):
_ex_idx = self.ex_comb.currentIndex()
_ex_ref = EX_LIST[ _ex_idx ]
#
_ex_ref[ self.idx ].grade = self.grade_box.text()
_ex_ref[ self.idx ].feedback = self.feedback_box.toPlainText()
wb.save(filename=FILENAME)
#
self.update_by_idx( self.idx )
pass
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = GradingWindow()
sys.exit(app.exec_())