|
5 | 5 |
|
6 | 6 |
|
7 | 7 | # pylint: disable=redefined-builtin,invalid-name |
8 | | -class Location: # pylint: disable=too-many-instance-attributes |
9 | | - """ |
10 | | - A location in the world affected by the coronavirus. |
11 | | - """ |
12 | | - |
13 | | - def __init__( |
14 | | - self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered, |
15 | | - ): # pylint: disable=too-many-arguments |
16 | | - # General info. |
17 | | - self.id = id |
18 | | - self.country = country.strip() |
19 | | - self.province = province.strip() |
20 | | - self.coordinates = coordinates |
21 | | - |
22 | | - # Last update. |
23 | | - self.last_updated = last_updated |
24 | | - |
25 | | - # Statistics. |
26 | | - self.confirmed = confirmed |
27 | | - self.deaths = deaths |
28 | | - self.recovered = recovered |
29 | | - |
30 | | - @property |
31 | | - def country_code(self): |
32 | | - """ |
33 | | - Gets the alpha-2 code represention of the country. Returns 'XX' if none is found. |
34 | | -
|
35 | | - :returns: The country code. |
36 | | - :rtype: str |
37 | | - """ |
38 | | - return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper() |
39 | | - |
40 | | - @property |
41 | | - def country_population(self): |
42 | | - """ |
43 | | - Gets the population of this location. |
44 | | -
|
45 | | - :returns: The population. |
46 | | - :rtype: int |
47 | | - """ |
48 | | - return country_population(self.country_code) |
49 | | - |
50 | | - def serialize(self): |
51 | | - """ |
52 | | - Serializes the location into a dict. |
53 | | -
|
54 | | - :returns: The serialized location. |
55 | | - :rtype: dict |
56 | | - """ |
57 | | - return { |
58 | | - # General info. |
59 | | - "id": self.id, |
60 | | - "country": self.country, |
61 | | - "country_code": self.country_code, |
62 | | - "country_population": self.country_population, |
63 | | - "province": self.province, |
64 | | - # Coordinates. |
65 | | - "coordinates": self.coordinates.serialize(), |
66 | | - # Last updated. |
67 | | - "last_updated": self.last_updated, |
68 | | - # Latest data (statistics). |
69 | | - "latest": { |
70 | | - "confirmed": self.confirmed, |
71 | | - "deaths": self.deaths, |
72 | | - "recovered": self.recovered, |
73 | | - }, |
74 | | - } |
75 | | - |
76 | | - |
77 | | -class TimelinedLocation(Location): |
78 | | - """ |
79 | | - A location with timelines. |
80 | | - """ |
81 | | - |
82 | | - # pylint: disable=too-many-arguments |
83 | | - def __init__(self, id, country, province, coordinates, last_updated, timelines): |
84 | | - super().__init__( |
85 | | - # General info. |
86 | | - id, |
87 | | - country, |
88 | | - province, |
89 | | - coordinates, |
90 | | - last_updated, |
91 | | - # Statistics (retrieve latest from timelines). |
92 | | - confirmed=timelines.get("confirmed").latest or 0, |
93 | | - deaths=timelines.get("deaths").latest or 0, |
94 | | - recovered=timelines.get("recovered").latest or 0, |
95 | | - ) |
96 | | - |
97 | | - # Set timelines. |
98 | | - self.timelines = timelines |
99 | | - |
100 | | - # pylint: disable=arguments-differ |
101 | | - def serialize(self, timelines=False): |
102 | | - """ |
103 | | - Serializes the location into a dict. |
104 | | -
|
105 | | - :param timelines: Whether to include the timelines. |
106 | | - :returns: The serialized location. |
107 | | - :rtype: dict |
108 | | - """ |
109 | | - serialized = super().serialize() |
110 | | - |
111 | | - # Whether to include the timelines or not. |
112 | | - if timelines: |
113 | | - serialized.update( |
114 | | - { |
115 | | - "timelines": { |
116 | | - # Serialize all the timelines. |
117 | | - key: value.serialize() |
118 | | - for (key, value) in self.timelines.items() |
119 | | - } |
120 | | - } |
121 | | - ) |
122 | | - |
123 | | - # Return the serialized location. |
124 | | - return serialized |
| 8 | +class Locations: |
| 9 | + class Location: # pylint: disable=too-many-instance-attributes |
| 10 | + """ |
| 11 | + A location in the world affected by the coronavirus. |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__( |
| 15 | + self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered, |
| 16 | + ): # pylint: disable=too-many-arguments |
| 17 | + # General info. |
| 18 | + self.id = id |
| 19 | + self.country = country.strip() |
| 20 | + self.province = province.strip() |
| 21 | + self.coordinates = coordinates |
| 22 | + |
| 23 | + # Last update. |
| 24 | + self.last_updated = last_updated |
| 25 | + |
| 26 | + # Statistics. |
| 27 | + self.confirmed = confirmed |
| 28 | + self.deaths = deaths |
| 29 | + self.recovered = recovered |
| 30 | + |
| 31 | + @property |
| 32 | + def country_code(self): |
| 33 | + """ |
| 34 | + Gets the alpha-2 code represention of the country. Returns 'XX' if none is found. |
| 35 | +
|
| 36 | + :returns: The country code. |
| 37 | + :rtype: str |
| 38 | + """ |
| 39 | + return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper() |
| 40 | + |
| 41 | + @property |
| 42 | + def country_population(self): |
| 43 | + """ |
| 44 | + Gets the population of this location. |
| 45 | +
|
| 46 | + :returns: The population. |
| 47 | + :rtype: int |
| 48 | + """ |
| 49 | + return country_population(self.country_code) |
| 50 | + |
| 51 | + def serialize(self): |
| 52 | + """ |
| 53 | + Serializes the location into a dict. |
| 54 | +
|
| 55 | + :returns: The serialized location. |
| 56 | + :rtype: dict |
| 57 | + """ |
| 58 | + return { |
| 59 | + # General info. |
| 60 | + "id": self.id, |
| 61 | + "country": self.country, |
| 62 | + "country_code": self.country_code, |
| 63 | + "country_population": self.country_population, |
| 64 | + "province": self.province, |
| 65 | + # Coordinates. |
| 66 | + "coordinates": self.coordinates.serialize(), |
| 67 | + # Last updated. |
| 68 | + "last_updated": self.last_updated, |
| 69 | + # Latest data (statistics). |
| 70 | + "latest": { |
| 71 | + "confirmed": self.confirmed, |
| 72 | + "deaths": self.deaths, |
| 73 | + "recovered": self.recovered, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + class TimelinedLocation(Location): |
| 79 | + """ |
| 80 | + A location with timelines. |
| 81 | + """ |
| 82 | + |
| 83 | + # pylint: disable=too-many-arguments |
| 84 | + def __init__(self, id, country, province, coordinates, last_updated, timelines): |
| 85 | + super().__init__( |
| 86 | + # General info. |
| 87 | + id, |
| 88 | + country, |
| 89 | + province, |
| 90 | + coordinates, |
| 91 | + last_updated, |
| 92 | + # Statistics (retrieve latest from timelines). |
| 93 | + confirmed=timelines.get("confirmed").latest or 0, |
| 94 | + deaths=timelines.get("deaths").latest or 0, |
| 95 | + recovered=timelines.get("recovered").latest or 0, |
| 96 | + ) |
| 97 | + |
| 98 | + # Set timelines. |
| 99 | + self.timelines = timelines |
| 100 | + |
| 101 | + # pylint: disable=arguments-differ |
| 102 | + def serialize(self, timelines=False): |
| 103 | + """ |
| 104 | + Serializes the location into a dict. |
| 105 | +
|
| 106 | + :param timelines: Whether to include the timelines. |
| 107 | + :returns: The serialized location. |
| 108 | + :rtype: dict |
| 109 | + """ |
| 110 | + serialized = super().serialize() |
| 111 | + |
| 112 | + # Whether to include the timelines or not. |
| 113 | + if timelines: |
| 114 | + serialized.update( |
| 115 | + { |
| 116 | + "timelines": { |
| 117 | + # Serialize all the timelines. |
| 118 | + key: value.serialize() |
| 119 | + for (key, value) in self.timelines.items() |
| 120 | + } |
| 121 | + } |
| 122 | + ) |
| 123 | + |
| 124 | + # Return the serialized location. |
| 125 | + return serialized |
0 commit comments