Skip to content

Instantly share code, notes, and snippets.

@mechinn
Created May 29, 2013 22:28
Show Gist options
  • Save mechinn/5674349 to your computer and use it in GitHub Desktop.
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
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