Skip to content

Commit 635f777

Browse files
authored
Merge branch 'master' into format
2 parents ef97e3f + 1be9af5 commit 635f777

File tree

8 files changed

+92
-33
lines changed

8 files changed

+92
-33
lines changed

.all-contributorsrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@
9090
"avatar_url": "https://avatars0.githubusercontent.com/u/13730460?v=4",
9191
"profile": "https://github.com/Abdirahiim",
9292
"contributions": [
93-
"doc"
93+
"doc",
94+
"tool",
95+
"platform"
9496
]
9597
},
9698
{

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,7 @@ coverage.xml
6363
docs/_build/
6464

6565
# PyBuilder
66-
target/
66+
target/
67+
68+
# OSX Stuff
69+
.DS_Store

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ __Sample response__
129129
"id": 39,
130130
"country": "Norway",
131131
"country_code": "NO",
132+
"country_population": 5009150,
132133
"province": "",
133134
"county": "",
134135
"last_updated": "2020-03-21T06:59:11.315422Z",
@@ -174,6 +175,7 @@ __Sample response__
174175
"id": 0,
175176
"country": "Thailand",
176177
"country_code": "TH",
178+
"country_population": 67089500,
177179
"province": "",
178180
"county": "",
179181
"last_updated": "2020-03-21T06:59:11.315422Z",
@@ -252,6 +254,7 @@ __Sample Response__
252254
"id": 16,
253255
"country": "Italy",
254256
"country_code": "IT",
257+
"country_population": 60340328,
255258
"province": "",
256259
"county": "",
257260
"last_updated": "2020-03-23T13:32:23.913872Z",
@@ -291,6 +294,7 @@ __Sample Response__
291294
"id": 0,
292295
"country": "US",
293296
"country_code": "US",
297+
"country_population": 310232863,
294298
"province": "New York",
295299
"state": "New York",
296300
"county": "New York",
@@ -309,6 +313,7 @@ __Sample Response__
309313
"id": 1,
310314
"country": "US",
311315
"country_code": "US",
316+
"country_population": 310232863,
312317
"province": "New York",
313318
"state": "New York",
314319
"county": "Westchester",
@@ -432,7 +437,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
432437
</tr>
433438
<tr>
434439
<td align="center"><a href="https://github.com/SeanCena"><img src="https://avatars1.githubusercontent.com/u/17202203?v=4" width="100px;" alt=""/><br /><sub><b>SeanCena</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=SeanCena" title="Code">💻</a> <a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=SeanCena" title="Documentation">📖</a> <a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=SeanCena" title="Tests">⚠️</a></td>
435-
<td align="center"><a href="https://github.com/Abdirahiim"><img src="https://avatars0.githubusercontent.com/u/13730460?v=4" width="100px;" alt=""/><br /><sub><b>Abdirahiim Yassin </b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=Abdirahiim" title="Documentation">📖</a></td>
440+
<td align="center"><a href="https://github.com/Abdirahiim"><img src="https://avatars0.githubusercontent.com/u/13730460?v=4" width="100px;" alt=""/><br /><sub><b>Abdirahiim Yassin </b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=Abdirahiim" title="Documentation">📖</a> <a href="#tool-Abdirahiim" title="Tools">🔧</a> <a href="#platform-Abdirahiim" title="Packaging/porting to new platform">📦</a></td>
436441
<td align="center"><a href="https://github.com/kant"><img src="https://avatars1.githubusercontent.com/u/32717?v=4" width="100px;" alt=""/><br /><sub><b>Darío Hereñú</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=kant" title="Documentation">📖</a></td>
437442
<td align="center"><a href="https://github.com/o-ba"><img src="https://avatars1.githubusercontent.com/u/8812114?v=4" width="100px;" alt=""/><br /><sub><b>Oliver</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=o-ba" title="Documentation">📖</a></td>
438443
<td align="center"><a href="http://www.carmelagreco.dev"><img src="https://avatars0.githubusercontent.com/u/5394906?v=4" width="100px;" alt=""/><br /><sub><b>carmelag</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=carmelag" title="Documentation">📖</a></td>

app/location/__init__.py

Lines changed: 19 additions & 4 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 ..utils.populations import country_population
34

45

56
class Location:
@@ -26,9 +27,22 @@ def __init__(self, id, country, province, coordinates, last_updated, confirmed,
2627
def country_code(self):
2728
"""
2829
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
30+
31+
:returns: The country code.
32+
:rtype: str
2933
"""
3034
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
3135

36+
@property
37+
def country_population(self):
38+
"""
39+
Gets the population of this location.
40+
41+
:returns: The population.
42+
:rtype: int
43+
"""
44+
return country_population(self.country_code)
45+
3246
def serialize(self):
3347
"""
3448
Serializes the location into a dict.
@@ -38,10 +52,11 @@ def serialize(self):
3852
"""
3953
return {
4054
# General info.
41-
"id": self.id,
42-
"country": self.country,
43-
"country_code": self.country_code,
44-
"province": self.province,
55+
'id' : self.id,
56+
'country' : self.country,
57+
'country_code' : self.country_code,
58+
'country_population': self.country_population,
59+
'province' : self.province,
4560
# Coordinates.
4661
"coordinates": self.coordinates.serialize(),
4762
# Last updated.

app/models/location.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class Location(BaseModel):
1414
id: int
1515
country: str
1616
country_code: str
17-
county: str = ""
18-
province: str = ""
19-
last_updated: str # TODO use datetime.datetime type.
17+
country_population: int = None
18+
province: str = ''
19+
county: str = ''
20+
last_updated: str # TODO use datetime.datetime type.
2021
coordinates: Dict
2122
latest: Latest
2223
timelines: Timelines = {}

app/utils/countrycodes.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from itertools import chain
2+
13
# Default country code.
24
default_code = "XX"
35

@@ -366,12 +368,14 @@ def country_code(country):
366368
Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1
367369
Defaults to "XX".
368370
"""
371+
# Look in synonyms if not found.
372+
if not country in is_3166_1 and country in synonyms:
373+
country = synonyms[country]
374+
375+
# Return code if country was found.
369376
if country in is_3166_1:
370377
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-
print("No country_code found for '" + country + "'. Using '" + default_code + "'")
377-
return default_code
378+
379+
# Default to default_code.
380+
print ("No country_code found for '" + country + "'. Using '" + default_code + "'")
381+
return default_code

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+

tests/test_location.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def __init__(self, latest):
1313

1414
return TestTimeline(args[0])
1515

16-
1716
@pytest.mark.parametrize(
1817
"test_id, country, country_code, province, latitude, longitude, confirmed_latest, deaths_latest, recovered_latest",
1918
[
@@ -35,13 +34,12 @@ def test_location_class(
3534
deaths_latest,
3635
recovered_latest,
3736
):
38-
3937
# id, country, province, coordinates, confirmed, deaths, recovered
4038
coords = coordinates.Coordinates(latitude=latitude, longitude=longitude)
4139

4240
# Timelines
4341
confirmed = timeline.Timeline(confirmed_latest)
44-
deaths = timeline.Timeline(deaths_latest)
42+
deaths = timeline.Timeline(deaths_latest)
4543
recovered = timeline.Timeline(recovered_latest)
4644

4745
# Date now.
@@ -53,16 +51,4 @@ def test_location_class(
5351
)
5452

5553
assert location_obj.country_code == country_code
56-
57-
# validate serialize
58-
check_dict = {
59-
"id": test_id,
60-
"country": country,
61-
"country_code": country_code,
62-
"province": province,
63-
"last_updated": now,
64-
"coordinates": {"latitude": latitude, "longitude": longitude},
65-
"latest": {"confirmed": confirmed_latest, "deaths": deaths_latest, "recovered": recovered_latest},
66-
}
67-
68-
assert location_obj.serialize() == check_dict
54+
assert not location_obj.serialize() == None

0 commit comments

Comments
 (0)