Created
May 29, 2013 22:28
-
-
Save mechinn/5674349 to your computer and use it in GitHub Desktop.
Playing with the Clever API, it gives the average number of students per section
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def get_error_code(x): | |
return { | |
400: 'Bad Request', | |
401: 'Unauthorized. Bad API key or not authorized to access a resource.', | |
404: 'Not found. The resource requested doesn\'t exist.', | |
500: 'Server errors. Oops!', | |
}.get(x, 'Error') | |
def avg_students_per_section(): | |
req = requests.get('https://api.getclever.com/v1.1/sections', auth=('DEMO_KEY', '')) | |
if 200 != req.status_code: | |
print get_error_code(req.status_code) | |
return | |
else: | |
print 'Successful Request' | |
allSections = req.json().get('data') | |
totalStudents = 0 | |
for section in allSections: | |
totalStudents += len(section.get('data').get('students')) | |
numSections = len(allSections) | |
avg = totalStudents / float(numSections) | |
print 'Total Students = ' + str(totalStudents) | |
print 'Number of Sections = ' + str(numSections) | |
print 'Average number of students per section = ' + str(avg) | |
if __name__=="__main__": | |
avg_students_per_section() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment