Refactor to make testing easier

This commit is contained in:
Chris Hill-Scott
2020-09-16 11:33:57 +01:00
parent b9f75218d1
commit 3047af2c13
2 changed files with 25 additions and 19 deletions

View File

@@ -38,3 +38,25 @@ class CITY_OF_LONDON:
class BRYHER:
WD20_CODE = 'E05011090'
POPULATION = 84
def estimate_number_of_smartphones_for_population(population):
smartphone_ownership_for_area_by_age_range = {}
for range, ownership in SMARTPHONE_OWNERSHIP_BY_AGE_RANGE.items():
min, max = range
smartphone_ownership_for_area_by_age_range[range] = sum(
people
for age, people in population
if min <= age <= max
) * ownership
total_population = sum(dict(population).values())
total_phones = sum(smartphone_ownership_for_area_by_age_range.values())
print( # noqa: T001
f' Population:{total_population: 11,.0f}'
f' Phones:{total_phones: 11,.0f}'
)
return total_phones

View File

@@ -13,6 +13,7 @@ from constants import (
MEDIAN_AGE_RANGE_UK,
MEDIAN_AGE_UK,
SMARTPHONE_OWNERSHIP_BY_AGE_RANGE,
estimate_number_of_smartphones_for_population,
)
from polygons import Polygons
from repo import BroadcastAreasRepository
@@ -79,27 +80,10 @@ def estimate_number_of_smartphones_in_area(country_or_ward_code):
if country_or_ward_code not in area_to_population_mapping:
raise ValueError(f'No population data for {country_or_ward_code}')
population = area_to_population_mapping[country_or_ward_code]
smartphone_ownership_for_area_by_age_range = {}
for range, ownership in SMARTPHONE_OWNERSHIP_BY_AGE_RANGE.items():
min, max = range
smartphone_ownership_for_area_by_age_range[range] = sum(
people
for age, people in population
if min <= age <= max
) * ownership
total_population = sum(dict(population).values())
total_phones = sum(smartphone_ownership_for_area_by_age_range.values())
print( # noqa: T001
f' Population:{total_population: 11,.0f}'
f' Phones:{total_phones: 11,.0f}'
return estimate_number_of_smartphones_for_population(
area_to_population_mapping[country_or_ward_code]
)
return total_phones
ctry19_filepath = source_files_path / "Countries.geojson"