Created
June 14, 2014 09:39
-
-
Save yati-sagade/b1e14bcbf7a4bbb30db2 to your computer and use it in GitHub Desktop.
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
from __future__ import print_function | |
import time | |
import requests | |
BASE_URL = 'https://api.clever.com' | |
OAUTH_TOK = 'DEMO_TOKEN' | |
def get(resfile): | |
retry_delay = 0 | |
while True: | |
try: | |
return requests.get(BASE_URL + resfile, | |
headers={'Authorization': 'Bearer DEMO_TOKEN'}) | |
except Exception as e: | |
time.sleep(2 ** retry_delay) | |
retry_delay += 1 | |
def doit(): | |
sections_res = '/v1.1/sections/' | |
num_sections, num_students = 0., 0. | |
while True: | |
sections = get(sections_res).json() | |
print('Got data for {}'.format(sections_res)) | |
num_sections += len(sections['data']) | |
print('Num sections: {}'.format(len(sections['data']))) | |
num_students += sum(len(section['data']['students']) | |
for section in sections['data']) | |
print('Num students: {}'.format(sum(len(section['data']['students']) | |
for section in sections['data']))) | |
paging = sections['paging'] | |
if paging['current'] == paging['total']: | |
break | |
assert sections['links'][1]['rel'] == 'next' | |
sections_res = sections['links'][1]['uri'] | |
time.sleep(4) | |
return num_students / num_sections | |
if __name__ == '__main__': | |
ans = doit() | |
print('The average # of students per section is {:0.2f}'.format(ans)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment