-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttendanceTallier.py
105 lines (86 loc) · 2.89 KB
/
AttendanceTallier.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
import os
def createSectionDict():
"""
for each row of enrollment sheet, add student name as dict key to value
of enrolled section
"""
sectionDict = {}
sectionCodes = []
# open membership sheet and skip header line
f = open(os.path.join('roster', os.listdir('roster')[0]), "r")
for i in range(len(os.listdir('attendance'))):
line = next(f)
if line.startswith('Sect'):
line = next(f)
sectionCodes.append(line.split(',')[0])
# loop through lines with names to assign
for line in f:
name = ''
parts = line.split(",")
for part in parts:
if '@' in part: # extract first part of email
name = part.split("@")[0]
section = parts[0]
if name == '':
continue
#print(name + " " + section)
sectionDict[name] = section
f.close()
return sectionDict, sectionCodes
# get sectionDict
sectionDict, sectionCodes = createSectionDict()
# uncomment this to create a txt of section membership
'''
#check that dict is correct
roster = open("Section Roster.txt", "w")
for key, value in sectionDict.items():
roster.write("{key}:{value}\n".format(key = key, value = value))
roster.close()
'''
# from attendance sheets, assign each student to a points tally
pointsDict = {}
for (f, numCode) in zip(os.listdir('attendance'), sectionCodes):
attendance = open(os.path.join('attendance', f), 'r')
for line in attendance:
if line.startswith("Student") or line.startswith("Points"):
continue
parts = line.split(",")
name = parts[4]
try:
points = int(float(parts[-1][:-1]))
except:
continue
if name not in pointsDict:
pointsDict[name] = 0
if name in sectionDict and sectionDict[name] == str(numCode):
pointsDict[name] += points
elif name not in sectionDict:
pointsDict[name] = max(pointsDict[name], points)
attendance.close()
# uncomment this to create a txt of point tallies
'''
#check that dict is correct
points = open("Points.txt", "w")
for key, value in pointsDict.items():
points.write("{key}:{value}\n".format(key = key, value = value))
points.close()
'''
# open file to tally points
if not os.path.exists('assignment_results'):
os.makedir('assignment_results')
newAssignment = open(os.path.join('assignment_results', 'assignment.csv'), "w")
canvasData = open(os.path.join('gradebook', os.listdir('gradebook')[0]), "r")
newAssignment.write(next(canvasData))
# add each student's info + score to new assignment
for line in canvasData:
parts = line.split(",")[:-1]
name = parts[4]
if name in pointsDict:
points = pointsDict[name]
else:
points = 0
for part in parts:
newAssignment.write(str(part) + ",")
newAssignment.write(str(points) + "\n")
canvasData.close()
newAssignment.close()