Last active
August 29, 2015 14:06
-
-
Save heyitsjames/dcd8f6c40ad8ea10ea25 to your computer and use it in GitHub Desktop.
Average number of students per section (Clever API sandbox)
This file contains hidden or 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
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI: to run this code, the
requestsmodule is required, which is not part of the standard libary. Just apip installaway, though.