|
| 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