-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
231 lines (182 loc) · 5.89 KB
/
main.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
import sqlite3
import datetime
conn = sqlite3.connect('attendance.db')
cur = conn.cursor()
def add_student():
id = int(input('Enter id: '))
name = input('Enter name: ')
semester = int(input('Enter semester: '))
degree = input('Enter degree: ')
try:
cur.execute('INSERT INTO STUDENTS VALUES (?, ?, ?, ?)', (id, name, semester, degree))
conn.commit()
except Exception as e:
print(e)
else:
print('\nStudent Added')
def remove_student():
id = int(input('Enter id: '))
try:
cur.execute('DELETE FROM STUDENTS WHERE ID = ?', (id,))
conn.commit()
except Exception as e:
print(e)
else:
print('\nStudent Removed')
def view_students():
for row in cur.execute('SELECT * FROM STUDENTS ORDER BY ID ASC'):
print(row)
def add_subject():
degree = input('Enter degree: ')
semester = int(input('Enter semester: '))
subject = input('Enter subject name: ')
try:
cur.execute('INSERT INTO CLASSES VALUES (?, ?, ?)', (degree, semester, subject))
cur.execute(f'''CREATE TABLE IF NOT EXISTS {subject}(
ID INT NOT NULL,
DATE TEXT NOT NULL,
DEGREE TEXT NOT NULL,
PRESENCE INT NOT NULL,
PRIMARY KEY(ID, DATE)
);
''',)
conn.commit()
except Exception as e:
print(e)
else:
print('\nSubject Added')
def remove_subject():
subject = input('Enter subject name: ')
try:
cur.execute(f'DROP TABLE {subject}')
cur.execute('DELETE FROM CLASSES WHERE SUBJECT = ?', (subject,))
conn.commit()
except Exception as e:
print(e)
else:
print('\nSubject Deleted')
def view_subjects():
for row in cur.execute('SELECT * FROM CLASSES'):
print(row)
def view_full_record():
degree = input('Enter degree: ')
subject = input('Enter subject name: ')
try:
for row in cur.execute(f'SELECT * FROM {subject} WHERE DEGREE = ?', (degree,)):
print(row)
except Exception as e:
print(e)
def mark_attendance():
degree = input('Enter degree: ')
semester = int(input('Enter semester: '))
subject = input('Enter subject name: ')
try:
print('\nEnter 0 for absent, 1 for present')
attendance = []
for row in cur.execute('SELECT * FROM STUDENTS WHERE DEGREE = ? AND SEMESTER = ?', (degree, semester)):
presence = int(input(f'{row[0]}: '))
attendance.append((row[0], str(datetime.date.today()), degree, presence))
cur.executemany(f'INSERT INTO {subject} VALUES (?, ?, ?, ?)', attendance)
conn.commit()
except Exception as e:
print(e)
else:
print('\nAttendance Marked')
def monthly_report():
degree = input('Enter degree: ')
subject = input('Enter subject name: ')
year = int(input('Enter year: '))
month = int(input('Enter month: '))
records = []
total_days = []
presents = {}
try:
for row in cur.execute(f'SELECT * FROM {subject} WHERE DEGREE = ? ORDER BY ID ASC', (degree, )):
date = datetime.datetime.strptime(row[1], '%Y-%m-%d').date()
if date.year == year and date.month == month:
records.append(row)
total_days.append(date.day)
presents[row[0]] = 0
distinct_days = set(total_days)
for record in records:
if record[3] == 1:
presents[record[0]] += 1
for student, attendance in presents.items():
percentage = (attendance * 100) / len(distinct_days)
print(f'{student}: {percentage}%')
print(f'Total days: {len(distinct_days)}')
except Exception as e:
print(e)
def yearly_report():
degree = input('Enter degree: ')
subject = input('Enter subject name: ')
year = int(input('Enter year: '))
records = []
total_days = []
presents = {}
try:
for row in cur.execute(f'SELECT * FROM {subject} WHERE DEGREE = ? ORDER BY ID ASC', (degree, )):
date = datetime.datetime.strptime(row[1], '%Y-%m-%d').date()
if date.year == year:
records.append(row)
total_days.append(date.day)
presents[row[0]] = 0
distinct_days = set(total_days)
for record in records:
if record[3] == 1:
presents[record[0]] += 1
for student, attendance in presents.items():
percentage = (attendance * 100) / len(distinct_days)
print(f'{student}: {percentage}%')
print(f'Total days: {len(distinct_days)}')
except Exception as e:
print(e)
def menu():
print('\n===============================')
print('1. Add a student')
print('2. Remove a student')
print('3. View all students')
print('4. Add a subject')
print('5. Remove a subject')
print('6. View all subjects')
print('7. Mark attendance')
print('8. View monthly report')
print('9. View yearly report')
print('10. Exit')
print('\n===============================')
option = int(input("Enter choice: "))
if option == 1:
add_student()
return True
elif option == 2:
remove_student()
return True
elif option == 3:
view_students()
return True
elif option == 4:
add_subject()
return True
elif option == 5:
remove_subject()
return True
elif option == 6:
view_subjects()
return True
elif option == 7:
mark_attendance()
return True
elif option == 8:
monthly_report()
return True
elif option == 9:
yearly_report()
return True
elif option == 10:
return False
else:
print('Invalid Choice')
return True
while menu():
pass
conn.close()