-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeverettGetFit.py
executable file
·167 lines (130 loc) · 6.8 KB
/
LeverettGetFit.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
#!/usr/bin/python
#
# written by Bethany Seeger <[email protected]>
# June 6th, 2015
#
# python script that will a CSV file and tally up certain fields.
# Produces a new CSV file with the prefix "LGF_Tally_"
#
#
import sys, getopt
import os.path
import glob
from subprocess import call
from collections import namedtuple
MINUTES_PER_MILE = 15.0
Category = namedtuple('Category', ['mileval', 'minutes', 'index'])
# what index in the incoming array?
WALKING = Category(6, 7, 'WALK')
BIKING = Category(8, 9, 'BIKE')
TEAM = Category(10, 11, 'TEAM')
PLAYGROUND = Category(12, 13, 'PLAY')
INDOOR = Category(14, 15, 'INDOOR')
OUTDOOR = Category(16, 17, 'OUTDOOR')
WATER = Category(18, 19, 'WATER')
WINTER = Category(20, 21, 'WINTER')
CHORE = Category(22, 23, 'CHORE')
PE = Category(24, 25, 'PE')
OTHER = Category(27, 28, 'OTHER') #note that we skipped 26 here!
def usage():
print "python LeverettGetFit.py -f <CSV file>"
print " -f : file to do calculations on"
print " -h : usage message"
def main(argv):
try:
opts, args = getopt.getopt(argv, "f:h", ["file="])
except getopt.GetoptError:
usage()
sys.exit(2);
newfile = "";
for opt,arg in opts:
print ("opt is '" + opt + "' and arg is '" + arg + "'")
if opt in ('-f', '--file'):
csvfile = arg;
if opt in ('-h') :
usage()
sys.exit(2)
if (csvfile == '') :
usage()
sys.exit(2)
newfile = "Calcs-" + csvfile;
print ("Running calculations on file '" + csvfile + "'")
origfile = open(csvfile, 'r')
newfilename = "./LGF_Tally_" + csvfile
newfile = open(newfilename, 'w')
datadict = {}
origHeader = origfile.readline().split(',')
print("File pos is: " + str(origfile.tell()))
line = origfile.readline().strip("\n");
while line != '':
#for line in origfile:
data = map(lambda x: x.strip(' ').strip('"'), line.split(','));
# think tally per student here. They may be in the csv file multiple times
# note that this might be a staff person's name - the script could get fancy
# here and look at the column name.
studentName = data[4]
splitname = map(lambda x: x.strip(' '), studentName.split(';'))
print "studentName: " + studentName
print splitname
if studentName not in datadict:
print("creating entry for " + studentName)
datadict[studentName] = {
'first':splitname[1],
'last':splitname[0],
WALKING.index:0.0,
BIKING.index:0.0,
TEAM.index:0.0,
PLAYGROUND.index:0.0,
INDOOR.index:0.0,
OUTDOOR.index:0.0,
WATER.index:0.0,
WINTER.index:0.0,
CHORE.index:0.0,
PE.index:0.0,
OTHER.index:0.0,
'total': 0.0
}
# can really trim this down to where datadict[studentName]['Total'] is all we need and add on all the mile info to just one number
# for now leave this as it will help with debugging.
#datadict[studentName][WALKING.index] += float(data[WALKING.mileval]) * (float(data[WALKING.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][BIKING.index] += float(data[BIKING.mileval]) * (float(data[BIKING.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][TEAM.index] += float(data[TEAM.mileval]) * (float(data[TEAM.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][PLAYGROUND.index] += float(data[PLAYGROUND.mileval]) * (float(data[PLAYGROUND.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][INDOOR.index] += float(data[INDOOR.mileval]) * (float(data[INDOOR.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][OUTDOOR.index] += float(data[OUTDOOR.mileval]) * (float(data[OUTDOOR.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][WATER.index] += float(data[WATER.mileval]) * (float(data[WATER.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][WINTER.index] += float(data[WINTER.mileval]) * (float(data[WINTER.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][CHORE.index] += float(data[CHORE.mileval]) * (float(data[CHORE.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][PE.index] += float(data[PE.mileval]) * (float(data[PE.minutes]) / MINUTES_PER_MILE)
#datadict[studentName][OTHER.index] += float(data[OTHER.mileval]) * (float(data[OTHER.minutes]) / MINUTES_PER_MILE)
#duplicate here -- keep this one ultimately once things check out okay.
datadict[studentName]['total'] += float(data[WALKING.mileval]) * (float(data[WALKING.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[BIKING.mileval]) * (float(data[BIKING.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[TEAM.mileval]) * (float(data[TEAM.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[PLAYGROUND.mileval])* (float(data[PLAYGROUND.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[INDOOR.mileval]) * (float(data[INDOOR.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[OUTDOOR.mileval]) * (float(data[OUTDOOR.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[WATER.mileval]) * (float(data[WATER.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[WINTER.mileval]) * (float(data[WINTER.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[CHORE.mileval]) * (float(data[CHORE.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[PE.mileval]) * (float(data[PE.minutes]) / MINUTES_PER_MILE)
datadict[studentName]['total'] += float(data[OTHER.mileval]) * (float(data[OTHER.minutes]) / MINUTES_PER_MILE)
line = origfile.readline().strip('\n');
# phew, done with that. ;)
# round it out, now... should this be ceil?
for studentName in datadict:
datadict[studentName]['total'] = round(datadict[studentName]['total'])
# Now write the new file ------------------------
newfile.write('"student","total miles"\n')
# sort by the funky key (lastname; firstname)
students = sorted(datadict.keys())
for student in students:
# print using the split out first name and last name.
newfile.write('"' + datadict[student]['first'] + " " + datadict[student]['last'] + '":"' + str(datadict[student]['total']) + ' miles"\n');
# done writing file ------------------------------
# Now close the new file
newfile.close()
origfile.close()
print ("Finished running calculations. Created file: " + newfilename + "'");
if __name__ == "__main__":
main(sys.argv[1:])