Skip to content

Commit c9ffb55

Browse files
authored
Creational Design pattern
The location class mentioned in the init file of location folder had too many arguments passed. Whenever the programmer has to add a new serialize structure or add new arguments, they would have to change at many files to implement this. To enable this extensibility builder pattern has been implemented, which allows the programmer to create new serial structures, add more variables to location and pass very few arguments
1 parent 133a4ef commit c9ffb55

File tree

1 file changed

+53
-71
lines changed

1 file changed

+53
-71
lines changed

app/location/__init__.py

Lines changed: 53 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,61 @@
22
from ..coordinates import Coordinates
33
from ..utils.countries import CountryCodeUtil
44
from ..utils.populations import country_population
5+
from abc import ABC, abstractmethod
56

7+
class LocationBuilder(ABC):
8+
"""
9+
An Abstract class to inherit to build coordinates of a location.
10+
"""
11+
@abstractmethod
12+
def country_code(self):
13+
pass
14+
15+
@abstractmethod
16+
def country_population(self):
17+
pass
18+
19+
@abstractmethod
20+
def serialize(self):
21+
pass
622

723
# pylint: disable=redefined-builtin,invalid-name
8-
class Location: # pylint: disable=too-many-instance-attributes
24+
class Location(LocationBuilder): # pylint: disable=too-many-instance-attributes
925
"""
1026
A location in the world affected by the coronavirus.
1127
"""
12-
country_code_util = CountryCodeUtil()
13-
1428
def __init__(
15-
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
29+
self, locationDict
1630
): # pylint: disable=too-many-arguments
1731
# General info.
18-
self.id = id
19-
self.country = country.strip()
20-
self.province = province.strip()
21-
self.coordinates = coordinates
32+
#if all True, then location would be constructed of all the params passed.
33+
pass
2234

23-
# Last update.
24-
self.last_updated = last_updated
35+
def build_location_dict(self,locationDict):
36+
self.country_code_util = CountryCodeUtil()
2537

26-
# Statistics.
27-
self.confirmed = confirmed
28-
self.deaths = deaths
29-
self.recovered = recovered
38+
self.location_dict = locationDict.fromkeys([list(locationDict.keys())])
39+
for key, value in locationDict.items():
40+
if value:
41+
if key == "country":
42+
self.location_dict[key] = self.format_country(value)
43+
elif key == "province":
44+
self.location_dict[key] = self.format_province(value)
45+
elif key == "coordinates":
46+
self.location_dict[key] = self.format_coordinates(value)
47+
else:
48+
self.location_dict[key] = value
3049

50+
self.location_dict["country_population"] = self.country_population
51+
3152
@property
3253
def country_code(self):
3354
"""
3455
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
3556
:returns: The country code.
3657
:rtype: str
3758
"""
38-
return (country_code_util.get_country_code(self.country) or country_code_util.DEFAULT_COUNTRY_CODE).upper()
59+
return (self.country_code_util.get_country_code(self.location_dict["country"]) or self.country_code_util.DEFAULT_COUNTRY_CODE).upper()
3960

4061
@property
4162
def country_population(self):
@@ -54,68 +75,29 @@ def serialize(self):
5475
"""
5576
return {
5677
# General info.
57-
"id": self.id,
58-
"country": self.country,
59-
"country_code": self.country_code,
60-
"country_population": self.country_population,
61-
"province": self.province,
78+
"id": self.location_dict["id"],
79+
"country": self.location_dict["country"],
80+
"country_code": self.location_dict["country_code"],
81+
"country_population": self.location_dict["country_population"],
82+
"province": self.location_dict["province"],
6283
# Coordinates.
63-
"coordinates": self.coordinates.serialize(),
84+
"coordinates": self.location_dict["coordinates"],
6485
# Last updated.
65-
"last_updated": self.last_updated,
86+
"last_updated": self.location_dict["last_updated"],
6687
# Latest data (statistics).
6788
"latest": {
68-
"confirmed": self.confirmed,
69-
"deaths": self.deaths,
70-
"recovered": self.recovered,
89+
"confirmed": self.location_dict["confirmed"],
90+
"deaths": self.location_dict["deaths"],
91+
"recovered": self.location_dict["recovered"],
7192
},
7293
}
94+
7395

96+
def format_country(self,country):
97+
return country.strip()
7498

75-
class TimelinedLocation(Location):
76-
"""
77-
A location with timelines.
78-
"""
79-
80-
# pylint: disable=too-many-arguments
81-
def __init__(self, id, country, province, coordinates, last_updated, timelines):
82-
super().__init__(
83-
# General info.
84-
id,
85-
country,
86-
province,
87-
coordinates,
88-
last_updated,
89-
# Statistics (retrieve latest from timelines).
90-
confirmed=timelines.get("confirmed").latest or 0,
91-
deaths=timelines.get("deaths").latest or 0,
92-
recovered=timelines.get("recovered").latest or 0,
93-
)
94-
95-
# Set timelines.
96-
self.timelines = timelines
97-
98-
# pylint: disable=arguments-differ
99-
def serialize(self, timelines=False):
100-
"""
101-
Serializes the location into a dict.
102-
:param timelines: Whether to include the timelines.
103-
:returns: The serialized location.
104-
:rtype: dict
105-
"""
106-
serialized = super().serialize()
107-
108-
# Whether to include the timelines or not.
109-
if timelines:
110-
serialized.update(
111-
{
112-
"timelines": {
113-
# Serialize all the timelines.
114-
key: value.serialize()
115-
for (key, value) in self.timelines.items()
116-
}
117-
}
118-
)
99+
def format_province(self,province):
100+
return province.strip()
119101

120-
# Return the serialized location.
121-
return serialized
102+
def format_coordinates(self, coordinates):
103+
return coordinates.serialize()

0 commit comments

Comments
 (0)