Skip to content

Instantly share code, notes, and snippets.

@heyitsjames
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save heyitsjames/dcd8f6c40ad8ea10ea25 to your computer and use it in GitHub Desktop.

Select an option

Save heyitsjames/dcd8f6c40ad8ea10ea25 to your computer and use it in GitHub Desktop.
Average number of students per section (Clever API sandbox)
from __future__ import division
import requests
import json
def get_average_students_per_section():
'''
Computes average number of students per section across all disctricts
'''
total_students = 0
num_sections = 0
auth = {'Authorization':'Bearer DEMO_TOKEN'}
districts_r = requests.get('https://api.clever.com/v1.1/districts', headers=auth)
districts_r.raise_for_status() # raises error if failed
district_uris = district_uris = [district['uri'] for district in json.loads(districts_r.text)['data']]
for uri in district_uris:
sections_r = requests.get('https://api.clever.com/{0}/sections'.format(uri), headers=auth)
sections_r.raise_for_status() # raises error if failed
section_list = json.loads(sections_r.text)['data']
students = sum([len(section['data']['students']) for section in section_list])
total_students = total_students + students
num_sections = num_sections + len(section_list)
return total_students/num_sections #21.78 as of 09/11/2014
@heyitsjames

Copy link
Copy Markdown
Author

FYI: to run this code, the requests module is required, which is not part of the standard libary. Just a pip install away, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment