-
Notifications
You must be signed in to change notification settings - Fork 0
/
agility_parse.py
418 lines (332 loc) · 10.1 KB
/
agility_parse.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from collections import Counter
from urllib2 import Request, urlopen, URLError
import csv
import numpy
import pylab as p
import time
import os
dog_name = raw_input("Good Day!, please enter your dog's name?" + " ")
nadacnum = raw_input("Please enter " + dog_name + "'s" + " NADAC number:" + " ")
while True:
request= Request("http://www.nadac.com/afrm/ph-to-csv.asp?regnum=" + nadacnum)
try:
print "Retrieving " + dog_name + "'s" + " records..."
response = urlopen(request)
points = response.read()
f = open('points.csv', 'w+')
f.write(points)
f.close()
except URLError, e:
print "Got and error code:", e
b = os.path.getsize('points.csv')
if b >0:
break
else:
nadacnum = raw_input("That was not a valid NADAC number, please try again")
filename = 'points.csv'
def parse(filename, delimiter):
""" Parses the csv points file to a JSON-like object"""
opened_file = open(filename)
csv_data = csv.reader(opened_file, delimiter=delimiter)
parsed_data = []
# Skip over the first line of the file for the headers
fields = csv_data.next()
for row in csv_data:
parsed_data.append(dict(zip(fields, row)))
opened_file.close()
return parsed_data
#for item in parsed_data:
# nadac_class = item["Class"]
# print nadac_class
# counter will work best for "Class", "Title" and "Host Club"
# other keys, like "Date Earned" and "Platinum" will need the nadac_class
# associated with the values
# dict keys:
# Points
# Class
# Total Points
# Title
# Date Earned
# DRI
# Silver/Purple
# Platinum
# Host Club
# plot returned in all functions
def barPlot(y, group_labels):
""" Will plot a bar plot given
y: list of numbers to be plotted
group_labels: """
fig = p.figure()
ax = fig.add_subplot(1,1,1)
N = len(y)
ind = range(N)
barChart = ax.bar(ind, y, facecolor='blue', align='center')
ax.set_ylabel("# of Qs")
ax.set_xticks(ind)
ax.set_xticklabels(group_labels)
p.title(dog_name + "'s" + " Qs")
fig.autofmt_xdate()
def autolabel(barChart):
for bar in barChart:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2, 1.01*height,'%d'%int(height), ha = 'center', va = 'bottom')
autolabel(barChart)
p.show()
# helper functions for nadac_classSort()
def regular_classSort():
"""will sort parsed data by the
regular class, helper function for
nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "AC" in key or "AS" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def jumpers_classSort():
"""will sort parsed data by the
jumpers class, helper function for
nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "JC" in key or "JS" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def chances_classSort():
"""will sort parsed data by the
chances class, helper function for
nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "CC" in key or "CS" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def tunnelers_classSort():
"""will sort parsed data by the
tunnelers class, helper function for
nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "TN-" in key or "TNS-" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def touchNgo_classSort():
"""will sort parsed data by the
touch 'n go class, helper function
for nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "TG-" in key or "TGS-" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def weavers_classSort():
"""will sort parsed data by the
weavers class, helper function
for nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "WV-" in key or "WVS-" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def hoopers_classSort():
"""will sort parsed data by the
hoopers class, helper function
for nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" not in key:
if "HP-" in key or "HPS-" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def extremeGames_classSort():
"""will sort parsed data by
all extreme games classes, helper
function for nadac_classSort()"""
group_labels = []
y = []
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
for key in count_by_class:
if "X" in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def nadac_classSort(nadac_class):
""" will return a plot displaying the
number of Qs earned in that class by level
(elite, open and novice)
nadac_class: "regular", "jumpers", "chances", "tunnelers",
"touch 'n go", "weavers", "hoopers", "extreme games" """
if nadac_class == "regular":
return regular_classSort()
elif nadac_class == "jumpers":
return jumpers_classSort()
elif nadac_class == "chances":
return chances_classSort()
elif nadac_class == "tunnelers":
return tunnelers_classSort()
elif nadac_class == "touch 'n go":
return touchNgo_classSort()
elif nadac_class == "weavers":
return weavers_classSort()
elif nadac_class == "hoopers":
return hoopers_classSort()
elif nadac_class == "extreme games":
return extremeGames_classSort()
def levelSort(level):
"""will sort parsed data by level
and return a plot displaying the number of
Qs earned by nadac class
level: "E", "O" or "N" """
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
group_labels = []
y = []
for key in count_by_class:
if level in key:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
# levelSort doesn't work properly for novice because "N" is in TN-E, etc
def clubSort():
""" will sort parsed data by Host Club, and
return a plot displaying the number of Qs
earned at that club's trials over the dogs'
lifetime"""
parsed_data = parse(filename, ",")
count_host_club = Counter(item["Host Club"][:-6] for item in parsed_data)
group_labels = []
y = []
for key in count_host_club:
group_labels.append(key)
y.append(count_host_club[key])
return barPlot(y, group_labels)
def lifetimeSort():
"""will sort Qs by nadac class only
(include all levels) for lifetime of dog"""
parsed_data = parse(filename, ",")
count_by_class = Counter(item["Class"] for item in parsed_data)
group_labels = []
y = []
for key in count_by_class:
group_labels.append(key)
y.append(count_by_class[key])
return barPlot(y, group_labels)
def nadacYear(start_year, end_year):
""" Returns a plot showing the number
of Qs per class and per level for the
specified NADAC year, NADAC year is
from August 1 - July 31"""
# convert start and end year to strings in YY format
start_year = str(start_year)
end_year = str(end_year)
# make list_of_tuples = (nadac_class, Date Earned)
list_of_tuples = []
parsed_data = parse(filename, ",")
for item in parsed_data:
tuple = (item["Class"], item["Date Earned"])
list_of_tuples.append(tuple)
# have list_of_tuples which contains all Qs
# need to pull tuples that contain start_year and end_year
# make a list of item[0] in tuple (class name)
class_name = []
for item in list_of_tuples:
if start_year in item[1]:
class_name.append(item[0])
elif end_year in item[1]:
class_name.append(item[0])
# set up counter for class_name list
q_count = [(x, class_name.count(x)) for x in set(class_name)]
# set up bar plot
y = []
group_labels = []
for item in q_count:
group_labels.append(item[0])
y.append(item[1])
return barPlot(y, group_labels)
def platinumSort():
""" Returns a plot showing the number
of platinum Qs per class for the lifetime
of the dog"""
# make list_of_tuples = (nadac_class, Platinum)
list_of_tuples = []
parsed_data = parse(filename, ",")
for item in parsed_data:
tuple = (item["Class"], item["Platinum"])
list_of_tuples.append(tuple)
# need to remove Qs that were not platinum runs
empty = []
platinum_list = []
for tuple in list_of_tuples:
if tuple[1] == '':
empty.append(tuple)
else:
platinum_list.append(tuple)
# count the types of platinum runs and put in dict
count_platinum = Counter(item[0] for item in platinum_list)
# generate plot
group_labels = []
y = []
for key in count_platinum:
group_labels.append(key)
y.append(count_platinum[key])
return barPlot(y, group_labels)
def titleSorthelper():
"""will sort parsed data by title
and return a list of titles earned,
helper function for title sorting """
parsed_data = parse(filename, ",")
count_by_title = Counter(item["Title"] for item in parsed_data)
titles = []
for key in count_by_title:
titles.append(key)
sorted_titles = sorted(titles)
return sorted_titles
def natchSort():
"""will print off a list of
NATCH points achieved"""
all_titles = titleSorthelper()
for title in all_titles:
if "NATCH" in title:
print title
def titleSort():
"""will sort parsed data by title
and return a list of all titles earned
except for NATCH titles"""
all_titles = titleSorthelper()
for title in all_titles:
if "NATCH" not in title:
print title