forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
103 lines (89 loc) · 3.36 KB
/
__init__.py
File metadata and controls
103 lines (89 loc) · 3.36 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""app.location"""
from ..coordinates import Coordinates
from ..utils.countries import CountryCodeUtil
from ..utils.populations import country_population
from abc import ABC, abstractmethod
class LocationBuilder(ABC):
"""
An Abstract class to inherit to build coordinates of a location.
"""
@abstractmethod
def country_code(self):
pass
@abstractmethod
def country_population(self):
pass
@abstractmethod
def serialize(self):
pass
# pylint: disable=redefined-builtin,invalid-name
class Location(LocationBuilder): # pylint: disable=too-many-instance-attributes
"""
A location in the world affected by the coronavirus.
"""
def __init__(
self, locationDict
): # pylint: disable=too-many-arguments
# General info.
#if all True, then location would be constructed of all the params passed.
pass
def build_location_dict(self,locationDict):
self.country_code_util = CountryCodeUtil()
self.location_dict = locationDict.fromkeys([list(locationDict.keys())])
for key, value in locationDict.items():
if value:
if key == "country":
self.location_dict[key] = self.format_country(value)
elif key == "province":
self.location_dict[key] = self.format_province(value)
elif key == "coordinates":
self.location_dict[key] = self.format_coordinates(value)
else:
self.location_dict[key] = value
self.location_dict["country_population"] = self.country_population
@property
def country_code(self):
"""
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
:returns: The country code.
:rtype: str
"""
return (self.country_code_util.get_country_code(self.location_dict["country"]) or self.country_code_util.DEFAULT_COUNTRY_CODE).upper()
@property
def country_population(self):
"""
Gets the population of this location.
:returns: The population.
:rtype: int
"""
return country_population(self.country_code)
def serialize(self):
"""
Serializes the location into a dict.
:returns: The serialized location.
:rtype: dict
"""
return {
# General info.
"id": self.location_dict["id"],
"country": self.location_dict["country"],
"country_code": self.location_dict["country_code"],
"country_population": self.location_dict["country_population"],
"province": self.location_dict["province"],
# Coordinates.
"coordinates": self.location_dict["coordinates"],
# Last updated.
"last_updated": self.location_dict["last_updated"],
# Latest data (statistics).
"latest": {
"confirmed": self.location_dict["confirmed"],
"deaths": self.location_dict["deaths"],
"recovered": self.location_dict["recovered"],
},
}
def format_country(self,country):
return country.strip()
def format_province(self,province):
return province.strip()
def format_coordinates(self, coordinates):
return coordinates.serialize()