-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.py
310 lines (239 loc) · 8.98 KB
/
helper.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
"""
Part by: Muhammad Hamza
"""
import csv
import os
import cv2 as cv
import calendar
from config import *
import sqlite3
from tkinter.filedialog import asksaveasfile
from tkinter.messagebox import showwarning
def printTable(table):
"""This function prints the table in a tkinter treeview widget in a nice way.
Args:
table (Treeview): This is the widget in which we're print the data.
"""
con = sqlite3.connect(databaseName)
cur = con.cursor()
cur.execute(f"SELECT * FROM {tableName}")
data = cur.fetchall()
data.sort(key=lambda d: d[1].upper())
for id, name, semester in data:
percentageSum = 0
for course in courses:
percentageSum += getAttendancePercentageFor(id, course)
table.insert(parent='', index='end', iid=id, text='',
values=(id, name, semester, f'{percentageSum/8:.2f}%'))
cur.close()
con.close()
def downloadReport(id):
"""
This function will download the attendance report of a student
"""
files = [('Excel files', '*.xlsx'), ('All files', '*.*')]
attendanceReport = getAttendanceTableFor(id)
filename = asksaveasfile(
filetypes=files, defaultextension=files, initialfile=f'{id}')
if filename:
attendanceReport.to_excel(filename.name, index=False)
def getColumnNames(table: str):
"""
This function get table name as argument.
Then it returns user the list of columns which
that table have.
"""
con = sqlite3.connect(databaseName)
cursor = con.cursor()
cursor.execute(
f"PRAGMA table_info({table});")
columnNames = [n for _, n, *_ in cursor.fetchall()]
cursor.close()
con.close()
return columnNames
def markAttendance(students: list, course: str, dayTime: str) -> None:
"""
This function accepts a list of student ids which are supposed to be marked as present.
Second argument is the course for which this attendance is to be marked.
Third is the unique Identifier for this day and this time of this class.
(Example: 15-12-2022-900)
"""
course = course.upper()
columns = getColumnNames(course)
db = sqlite3.connect(databaseName)
cursor = db.cursor()
columnsString = ["dayTime"]
valuesString = [f"'{dayTime}'"]
for column in columns[1:]:
if column in students:
columnsString.append(f"'{column}'")
valuesString.append("'P'")
else:
columnsString.append(f"'{column}'")
valuesString.append("'A'")
columnsString = ", ".join(columnsString)
valuesString = ",".join(valuesString)
query = f"INSERT INTO {course} ({columnsString}) VALUES ({valuesString});"
cursor.execute(query)
db.commit()
cursor.close()
db.close()
def getTotalNumberOfRecords(table):
"""This function will return total number of records in a table.
Args:
table (string): This is the table name for which we want to get the total number of records.
Returns:
number: number of records in the table
"""
db = sqlite3.connect(databaseName)
cursor = db.cursor()
cursor.execute(f"SELECT COUNT(*) FROM {table};")
totalNumberOfRecords = cursor.fetchone()[0]
cursor.close()
db.close()
return totalNumberOfRecords
def getAttendancePercentageFor(id: int, course: str) -> float:
"""This function will return percentage of presence of a student
whose id is passed in the current course.
Args:
id (int): CMS ID of student
course (str): Course code
Returns:
float: Percentage of attendance
"""
course = course.upper()
db = sqlite3.connect(databaseName)
cursor = db.cursor()
query = f"SELECT `{id}` FROM `{course}`;"
cursor.execute(query)
data = cursor.fetchall()
# getting first item from the data returned from sql
# That is either 'A' or 'P'
data = [d for d, in data]
cursor.close()
db.close()
totalRecordsCount = getTotalNumberOfRecords(course)
presentCount = data.count('P')
try:
percentage = presentCount / totalRecordsCount * 100
except ZeroDivisionError:
return 0
return percentage
def getWeekDay(date: str, sep: str = '-') -> str:
"""This function takes a date in form of string and an optional separator which the date is separated by. Then it return the weekday for that date.
Args:
date (str): The date string in form of day[sep]month[sep]year
sep (str): The [sep] that separates the entities of date
Returns:
str: Week day at that date
"""
day, month, year = date.split(sep)
return calendar.day_name[calendar.weekday(
int(year), int(month), int(day))]
def getAttendanceTableFor(id: str) -> pd.DataFrame:
"""This function takes a student cms Id, then create a table for his attendance in all courses and return it as a DataFrame object.
Args:
id (str): Id of student
Returns:
pd.DataFrame: DataFrame object for attendance of student.
"""
db = sqlite3.connect(databaseName)
cur = db.cursor()
tableOfAttendance = {}
timeTableWeekDays = timeTable.to_dict('tight')['index']
timeTableTimes = timeTable.to_dict('tight')['columns']
timeTableClasses = timeTable.to_dict('tight')['data']
for course in courses:
cur.execute(f"SELECT `dayTime`, `{id}` FROM `{course}`;")
records = cur.fetchall()
# dayTime is in form of date-month-year-time
# So first 3 are date and last one is time
dates = list(["-".join(date.split('-')[:3])
for date, _ in records])
times = list([date.split('-')[-1]
for date, _ in records])
attendances = list([attendance
for _, attendance in records])
for time in times:
if len(time) == 3:
times[times.index(time)] = '0' + time
for date in dates:
if tableOfAttendance.get(date) is None:
tableOfAttendance[date] = ['-'] * len(timeTableTimes)
for date in dates:
for time, attendance in zip(times, attendances):
weekDayIndex = timeTableWeekDays.index(getWeekDay(date))
timeIndex = timeTableTimes.index(time)
tableOfAttendance[date][timeIndex] = f"{timeTableClasses[weekDayIndex][timeIndex]}: {attendance}"
tableOfAttendance = [[key, *value]
for key, value in tableOfAttendance.items()]
cur.close()
db.close()
attendanceDataFrame = pd.DataFrame(tableOfAttendance,
columns=(['Date'] + timeTableTimes))
return attendanceDataFrame
def loadName(id):
"""This function will return name of student whose id is passed.
Args:
id (number): CMS ID of student
Returns:
string: Name of student
"""
db = sqlite3.connect(databaseName)
cursor = db.cursor()
query = f"SELECT * FROM {tableName} WHERE cmsId={id}"
cursor.execute(query)
_, name, _ = cursor.fetchall()[0]
cursor.close()
return name
def getFrameInRGB(capture):
"""This function will return a frame from the capture object in RGB format.
Args:
capture (frame): This is the capture object from which we want to get the frame.
Returns:
frame: This is the frame in RGB format.
"""
success, frame = capture.read()
return cv.cvtColor(frame, cv.COLOR_BGR2RGB) if success else None
def getDefaultAttendanceRecord(today):
"""This function will return a default attendance record for a day.
Args:
today (string): This is the date for which we want to create a default attendance record.
Returns:
dict: This is the default attendance record for a day.
"""
return {
"day": today,
"0900": 0, "1000": 0, "1100": 0, "1200": 0, "1300": 0, "1400": 0, "1500": 0, "1600": 0
}
def getCSV(filename):
"""This function will return a list of encodings from a csv file.
Args:
filename (string): This is the name of the csv file from which we want to get the encodings.
Returns:
list: This is the list of encodings.
"""
with open(f"./{directoryName}/{filename}") as f:
reader = csv.reader(f)
encodings = []
for code in reader:
encodings.extend(code)
encodings = [float(code) for code in encodings]
return encodings
def getKnownEncodings():
"""This function will return a dictionary of known encodings.
Returns:
Series: This is the dictionary of known encodings.
"""
knownEncodingFiles = os.listdir(f"./{directoryName}")
knownEncodings = pd.Series(dtype='float64')
for file in knownEncodingFiles:
knownEncodings[file[:-4]] = getCSV(file)
return knownEncodings
def showMessage(text):
"""This function will show a message box with the text passed."""
showwarning("Warning", text)
def truncateWidget(widget):
"""This function will remove all the children of a widget."""
for child in widget.winfo_children():
child.destroy()