Skip to content

Commit 73f02fb

Browse files
author
ExpDev07
committed
change datasource + refactoring + add to documentation
1 parent 8add335 commit 73f02fb

File tree

9 files changed

+79
-129
lines changed

9 files changed

+79
-129
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ __Sample response__
128128
"id": 39,
129129
"country": "Norway",
130130
"country_code": "NO",
131+
"country_population": 5009150,
131132
"province": "",
132133
"county": "",
133134
"last_updated": "2020-03-21T06:59:11.315422Z",
@@ -173,6 +174,7 @@ __Sample response__
173174
"id": 0,
174175
"country": "Thailand",
175176
"country_code": "TH",
177+
"country_population": 67089500,
176178
"province": "",
177179
"county": "",
178180
"last_updated": "2020-03-21T06:59:11.315422Z",
@@ -251,6 +253,7 @@ __Sample Response__
251253
"id": 16,
252254
"country": "Italy",
253255
"country_code": "IT",
256+
"country_population": 60340328,
254257
"province": "",
255258
"county": "",
256259
"last_updated": "2020-03-23T13:32:23.913872Z",
@@ -290,6 +293,7 @@ __Sample Response__
290293
"id": 0,
291294
"country": "US",
292295
"country_code": "US",
296+
"country_population": 310232863,
293297
"province": "New York",
294298
"state": "New York",
295299
"county": "New York",
@@ -308,6 +312,7 @@ __Sample Response__
308312
"id": 1,
309313
"country": "US",
310314
"country_code": "US",
315+
"country_population": 310232863,
311316
"province": "New York",
312317
"state": "New York",
313318
"county": "Westchester",

app/location/__init__.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ..coordinates import Coordinates
22
from ..utils import countrycodes
3-
from ..utils.population import countrypopulation
3+
from ..utils.populations import country_population
44

55
class Location:
66
"""
@@ -21,27 +21,29 @@ def __init__(self, id, country, province, coordinates, last_updated, confirmed,
2121
self.confirmed = confirmed
2222
self.deaths = deaths
2323
self.recovered = recovered
24-
24+
25+
@property
26+
def country_code(self):
27+
"""
28+
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
29+
30+
:returns: The country code.
31+
:rtype: str
32+
"""
33+
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
34+
2535
@property
2636
def country_population(self):
2737
"""
2838
Gets the population of this location.
39+
40+
:returns: The population.
41+
:rtype: int
2942
"""
3043
country_code = self.country_code
31-
32-
# Population data is only available for countries.
33-
if not self.country_code:
34-
return None
3544

3645
# Return population.
37-
return countrypopulation.get_population_dict()[country_code]
38-
39-
@property
40-
def country_code(self):
41-
"""
42-
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
43-
"""
44-
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
46+
return country_population(self.country_code)
4547

4648
def serialize(self):
4749
"""
@@ -52,10 +54,11 @@ def serialize(self):
5254
"""
5355
return {
5456
# General info.
55-
'id' : self.id,
56-
'country' : self.country,
57-
'country_code': self.country_code,
58-
'province' : self.province,
57+
'id' : self.id,
58+
'country' : self.country,
59+
'country_code' : self.country_code,
60+
'country_population': self.country_population,
61+
'province' : self.province,
5962

6063
# Coordinates.
6164
'coordinates': self.coordinates.serialize(),

app/models/location.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Location(BaseModel):
1010
id: int
1111
country: str
1212
country_code: str
13+
country_population: int = None
1314
county: str = ''
1415
province: str = ''
1516
last_updated: str # TODO use datetime.datetime type.

app/services/population/__init__.py

Whitespace-only changes.

app/services/population/countrypopulation.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

app/utils/countrycodes.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -361,27 +361,19 @@
361361
# "Cruise Ship" has no mapping, i.e. the default val is used
362362
}
363363

364-
def country_code(country, verbose=True):
364+
def country_code(country):
365365
"""
366366
Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1
367367
Defaults to "XX".
368368
"""
369+
# Look in synonyms if not found.
370+
if not country in is_3166_1 and country in synonyms:
371+
country = synonyms[country]
372+
373+
# Return code if country was found.
369374
if country in is_3166_1:
370375
return is_3166_1[country]
371-
else:
372-
if country in synonyms:
373-
synonym = synonyms[country]
374-
return is_3166_1[synonym]
375-
else:
376-
if verbose:
377-
print ("No country_code found for '" + country + "'. Using '" + default_code + "'")
378-
return default_code
379376

380-
def country_in_database(country):
381-
"""
382-
Checks if a given country is in the database.
383-
"""
384-
if country in chain(is_3166_1, synonyms):
385-
return True
386-
else:
387-
return False
377+
# Default to default_code.
378+
print ("No country_code found for '" + country + "'. Using '" + default_code + "'")
379+
return default_code

app/utils/population/__init__.py

Whitespace-only changes.

app/utils/population/countrypopulation.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

app/utils/populations.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import requests
2+
from io import StringIO, BytesIO
3+
from cachetools import cached, TTLCache
4+
from zipfile import ZipFile, ZipInfo
5+
from .countrycodes import country_code
6+
7+
# Fetching of the populations.
8+
def fetch_populations():
9+
"""
10+
Returns a dictionary containing the population of each country fetched from the GeoNames (https://www.geonames.org/).
11+
12+
:returns: The mapping of populations.
13+
:rtype: dict
14+
"""
15+
print ("Fetching populations...")
16+
17+
# Mapping of populations
18+
mappings = {}
19+
20+
# Fetch the countries.
21+
countries = requests.get("http://api.geonames.org/countryInfoJSON?username=dperic").json()['geonames']
22+
23+
# Go through all the countries and perform the mapping.
24+
for country in countries:
25+
mappings.update({ country["countryCode"]: int(country["population"]) or None })
26+
27+
# Finally, return the mappings.
28+
return mappings
29+
30+
# Mapping of alpha-2 codes country codes to population.
31+
populations = fetch_populations()
32+
33+
# Retrieving.
34+
def country_population(country_code, default=None):
35+
"""
36+
Fetches the population of the country with the provided country code.
37+
38+
:returns: The population.
39+
:rtype: int
40+
"""
41+
return populations.get(country_code, default)
42+
43+

0 commit comments

Comments
 (0)