Skip to content

Commit 80a2e08

Browse files
author
ExpDev07
committed
moved timelines logic to TimelinedLocation
1 parent d2b8d45 commit 80a2e08

File tree

5 files changed

+58
-27
lines changed

5 files changed

+58
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ We provide multiple data-sources you can pick from, simply add the query paramat
2121

2222
#### Available sources:
2323

24-
* **jhu** - https://github.com/CSSEGISandData/COVID-19 - Data repository operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE).
24+
* **jhu** - https://github.com/CSSEGISandData/COVID-19 - Data repository operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE).
2525

2626
* **... more to come later**.
2727

app/data/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'jhu': JhuLocationService(),
66
}
77

8-
def data_source(source: str):
8+
def data_source(source):
99
"""
1010
Retrieves the provided data-source service.
1111
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .coordinates import Coordinates
2-
from .utils import countrycodes
1+
from ..coordinates import Coordinates
2+
from ..utils import countrycodes
33

44
class Location:
55
"""
@@ -13,7 +13,7 @@ def __init__(self, id, country, province, coordinates, confirmed, deaths, recove
1313
self.province = province.strip()
1414
self.coordinates = coordinates
1515

16-
# Data.
16+
# Statistics.
1717
self.confirmed = confirmed
1818
self.deaths = deaths
1919
self.recovered = recovered
@@ -25,38 +25,65 @@ def country_code(self):
2525
"""
2626
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
2727

28-
def serialize(self, timelines = False):
28+
def serialize(self):
2929
"""
3030
Serializes the location into a dict.
3131
32-
:param timelines: Whether to include the timelines.
3332
:returns: The serialized location.
3433
:rtype: dict
3534
"""
36-
serialized = {
35+
return {
3736
# General info.
3837
'id' : self.id,
3938
'country' : self.country,
40-
'province' : self.province,
4139
'country_code': self.country_code,
40+
'province' : self.province,
4241

4342
# Coordinates.
4443
'coordinates': self.coordinates.serialize(),
4544

46-
# Latest data.
45+
# Latest data (statistics).
4746
'latest': {
48-
'confirmed': self.confirmed.latest,
49-
'deaths' : self.deaths.latest,
50-
'recovered': self.recovered.latest
47+
'confirmed': self.confirmed,
48+
'deaths' : self.deaths,
49+
'recovered': self.recovered
5150
},
5251
}
5352

53+
class TimelinedLocation(Location):
54+
"""
55+
A location with timelines.
56+
"""
57+
58+
def __init__(self, id, country, province, coordinates, timelines):
59+
super().__init__(
60+
# General info.
61+
id, country, province, coordinates,
62+
63+
# Statistics (retrieve latest from timelines).
64+
confirmed=timelines.get('confirmed').latest,
65+
deaths=timelines.get('deaths').latest,
66+
recovered=timelines.get('recovered').latest,
67+
)
68+
69+
# Set timelines.
70+
self.timelines = timelines
71+
72+
def serialize(self, timelines = False):
73+
"""
74+
Serializes the location into a dict.
75+
76+
:param timelines: Whether to include the timelines.
77+
:returns: The serialized location.
78+
:rtype: dict
79+
"""
80+
serialized = super().serialize()
81+
5482
# Whether to include the timelines or not.
5583
if timelines:
5684
serialized.update({ 'timelines': {
57-
'confirmed': self.confirmed.serialize(),
58-
'deaths' : self.deaths.serialize(),
59-
'recovered': self.recovered.serialize(),
85+
# Serialize all the timelines.
86+
key: value.serialize() for (key, value) in self.timelines.items()
6087
}})
6188

6289
# Return the serialized location.

app/routes/v2/latest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
@api.route('/latest')
55
def latest():
66
# Get the serialized version of all the locations.
7-
locations = [ location.serialize() for location in request.source.get_all() ]
7+
locations = request.source.get_all()
88

99
# All the latest information.
10-
latest = list(map(lambda location: location['latest'], locations))
10+
# latest = list(map(lambda location: location['latest'], locations))
1111

1212
return jsonify({
1313
'latest': {
14-
'confirmed': sum(map(lambda latest: latest['confirmed'], latest)),
15-
'deaths' : sum(map(lambda latest: latest['deaths'], latest)),
16-
'recovered': sum(map(lambda latest: latest['recovered'], latest)),
14+
'confirmed': sum(map(lambda location: location.confirmed, locations)),
15+
'deaths' : sum(map(lambda location: location.deaths, locations)),
16+
'recovered': sum(map(lambda location: location.recovered, locations)),
1717
}
1818
})

app/services/location/jhu.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from . import LocationService
2-
from ...location import Location
2+
from ...location import TimelinedLocation
33
from ...coordinates import Coordinates
44
from ...timeline import Timeline
55

@@ -16,6 +16,8 @@ def get(self, id):
1616
# Get location at the index equal to provided id.
1717
return self.get_all()[id]
1818

19+
# ---------------------------------------------------------------
20+
1921
import requests
2022
import csv
2123
from datetime import datetime
@@ -121,15 +123,17 @@ def get_locations():
121123
# Grab coordinates.
122124
coordinates = location['coordinates']
123125

124-
# Create location and append.
125-
locations.append(Location(
126+
# Create location (supporting timelines) and append.
127+
locations.append(TimelinedLocation(
126128
# General info.
127129
index, location['country'], location['province'], Coordinates(coordinates['lat'], coordinates['long']),
128130

129131
# Timelines (parse dates as ISO).
130-
Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['confirmed'].items() }),
131-
Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['deaths'].items() }),
132-
Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['recovered'].items() })
132+
{
133+
'confirmed': Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['confirmed'].items() }),
134+
'deaths' : Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['deaths'].items() }),
135+
'recovered': Timeline({ datetime.strptime(date, '%m/%d/%y').isoformat() + 'Z': amount for date, amount in timelines['recovered'].items() })
136+
}
133137
))
134138

135139
# Finally, return the locations.

0 commit comments

Comments
 (0)