forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.py
More file actions
64 lines (53 loc) · 1.88 KB
/
location.py
File metadata and controls
64 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from .coordinates import Coordinates
from .utils import countrycodes
class Location:
"""
A location in the world affected by the coronavirus.
"""
def __init__(self, id, country, province, coordinates, confirmed, deaths, recovered):
# General info.
self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates
# Data.
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered
@property
def country_code(self):
"""
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
"""
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
def serialize(self, timelines = False):
"""
Serializes the location into a dict.
:param timelines: Whether to include the timelines.
:returns: The serialized location.
:rtype: dict
"""
serialized = {
# General info.
'id' : self.id,
'country' : self.country,
'province' : self.province,
'country_code': self.country_code,
# Coordinates.
'coordinates': self.coordinates.serialize(),
# Latest data.
'latest': {
'confirmed': self.confirmed.latest,
'deaths' : self.deaths.latest,
'recovered': self.recovered.latest
},
}
# Whether to include the timelines or not.
if timelines:
serialized.update({ 'timelines': {
'confirmed': self.confirmed.serialize(),
'deaths' : self.deaths.serialize(),
'recovered': self.recovered.serialize(),
}})
# Return the serialized location.
return serialized