Skip to content

Commit 4325aaa

Browse files
committed
adds population as property of the location object
1 parent 14101de commit 4325aaa

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

app/location/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ..coordinates import Coordinates
22
from ..utils import countrycodes
3+
from ..services.population import countrypopulation
34

45
class Location:
56
"""
@@ -20,6 +21,19 @@ def __init__(self, id, country, province, coordinates, last_updated, confirmed,
2021
self.confirmed = confirmed
2122
self.deaths = deaths
2223
self.recovered = recovered
24+
25+
@property
26+
def population(self):
27+
"""
28+
Gets the population of this location.
29+
"""
30+
31+
# Population data is unavailable for provinces
32+
if self.province:
33+
return None
34+
else:
35+
print(countrypopulation.get_population_dict()[self.country_code])
36+
return countrypopulation.get_population_dict()[self.country_code]
2337

2438
@property
2539
def country_code(self):

app/services/population/__init__.py

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
import csv
3+
from io import StringIO, BytesIO
4+
from cachetools import cached, TTLCache
5+
from zipfile import ZipFile, ZipInfo
6+
from ...utils.countrycodes import *
7+
8+
9+
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
10+
def get_population_dict():
11+
"""
12+
Creates a dictionary containing the population of each country.
13+
The data is cached for 1 hour.
14+
15+
:returns: a dictionary of format country code: population.
16+
:rtype: dict
17+
"""
18+
19+
# download file and save it to a buffer
20+
URL = 'http://api.worldbank.org/v2/en/indicator/SP.POP.TOTL?downloadformat=csv'
21+
zip_data = BytesIO(requests.get(URL).content)
22+
23+
#load buffer as ZipFile object
24+
zipfile = ZipFile(zip_data, mode='r')
25+
26+
# isolate the and open csv file as StringIO object
27+
target_file = [x for x in zipfile.namelist() if not x.startswith('Metadata')][0]
28+
csv_file = StringIO(zipfile.read(target_file).decode('utf-8'))
29+
30+
# parse csv file as csv.reader
31+
csv_reader = csv.reader(csv_file, delimiter=',')
32+
popluation_dict = {}
33+
34+
for row in csv_reader:
35+
36+
# verify that row exists
37+
if len(row) > 0:
38+
39+
# verify that country is in database
40+
if country_in_database(row[0]):
41+
42+
# Populate dict with last non-None value
43+
key = country_code(row[0], verbose=False)
44+
popluation_dict.update({key: int(next((el for el in row[::-1] if el), None))})
45+
46+
return popluation_dict
47+

0 commit comments

Comments
 (0)