def count_by_gender(): gendercount = {} for inmate in get_inmate_data(): gprop = inmate['gender'] if gendercount.get(gprop): gendercount[gprop] += 1 else: gendercount[gprop] = 1 return gendercount
def count_young_offenders_by_race(): racecount = {} for inmate in get_inmate_data(): if inmate['age_at_offense'] <= 21: rtype = inmate['race'] if racecount.get(rtype): racecount[rtype] += 1 else: racecount[rtype] = 1 return racecount
def count_by_race_within_age_at_offence(min_age=-1, max_age=999): racecount = {} for inmate in get_inmate_data(): _age = inmate['age_at_offense'] if _age >= min_age and _age <= max_age: rtype = inmate['race'] if racecount.get(rtype): racecount[rtype] += 1 else: racecount[rtype] = 1 return racecount
from inspect import getsourcefile import os.path import sys current_path = os.path.abspath(getsourcefile(lambda: 0)) current_dir = os.path.dirname(current_path) parent_dir = current_dir[:current_dir.rfind(os.path.sep)] sys.path.insert(0, parent_dir) ########### END BOILERPLATE from scraper import get_inmate_data ### This is more or less an integration test to ### spot-check the expected values given the specified DATA_SRC_URL ### (i.e. this is a very brittle test) INMATE_DATA = get_inmate_data() def test_inmate_count(): assert len(INMATE_DATA) == 232 def test_types(): """ make sure the objects are what they should be, i.e. return value of get_inmate_data() is a list of dicts """ assert type(INMATE_DATA) == list assert all(type(i) == dict for i in INMATE_DATA)
def count_women(): the_count = 0 for row in get_inmate_data(): if row['gender'] == 'F': the_count += 1 return the_count
def count_by_race(): racecount = defaultdict(int) for inmate in get_inmate_data(): racecount[inmate['race']] += 1 return racecount