-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute_attendance.py
69 lines (63 loc) · 2.25 KB
/
compute_attendance.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
from datetime import datetime
def load_roster(filename):
name = dict()
line_no = 0
for line in open(filename):
line_no += 1
#try:
values = line.split()
uin = values[-1]
last = values[0]
first = ' '.join(values[1:-1])
#except (ValueError, IndexError) as err:
# print(err)
# print('at line {}'.format(line_no))
# break
if uin in name and (first != name[uin]['first'] or last != name[uin]['last']):
print('[WARNING] {} already exists, replacing with new value'.format(uin))
print(' old: {} {}'.format(name[uin]['first'], name[uin]['last']))
print(' new: {} {}'.format(first, last))
#c = input('replace? (y/n) ')
#if c == 'n':
# continue
else:
name[uin] = dict()
name[uin]['first'] = first;
name[uin]['last'] = last
return name
def split_on_comma_or_whitespace(line):
if ',' in line:
values = line.strip().split(',')
else:
values = line.strip().split()
return values
def load_swipe_log(filename):
attendance = dict()
for line in open(filename):
values = split_on_comma_or_whitespace(line)
time = float(values[0])
uin = values[-1].strip()
date = str(datetime.fromtimestamp(time)).split()[0]
if date not in attendance:
print('processing attendance on {}'.format(date))
attendance[date] = set()
#if uin not in attendance[date]:
# print('marked {} present on {}'.format(uin, date))
attendance[date].add(uin)
return attendance
def compute_attendance(name, attendance, filename='attendance.csv'):
print('writing attendance data to "{}"...'.format(filename))
with open(filename,'wt') as f:
f.write('last,first,uin,{},total\n'.format(','.join(date for date in sorted(attendance))))
f.write('\n'.join('{},{},{},{},{}'.format(name[uin]['last'], name[uin]['first'], uin, ','.join(str(uin in attendance[date]) for date in sorted(attendance)), sum(int(uin in attendance[date]) for date in sorted(attendance))) for uin in sorted(name)))
f.write('\n')
print('done.')
def main():
try:
name = load_roster('roster')
except (ValueError, IndexError) as err:
print("Error loading roster:",err)
attendance = load_swipe_log('swipe_log')
compute_attendance(name, attendance)
if __name__ == '__main__':
main()