Skip to content

Commit 3278a3b

Browse files
committed
Apply decorators to location
1 parent 96e5c9f commit 3278a3b

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

app/location/__init__.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ def serialize(self):
7474
}
7575

7676

77+
def decoratedSerialize(func, inc_timelines, *args, **kwargs):
78+
serialized = func()
79+
if kwargs.get("timelines") and inc_timelines:
80+
# Whether to include the timelines or not.
81+
timelines = kwargs["timelines"]
82+
if timelines:
83+
serialized.update(
84+
{
85+
"timelines": {
86+
# Serialize all the timelines.
87+
key: value.serialize()
88+
for (key, value) in self.timelines.items()
89+
}
90+
}
91+
)
92+
93+
if kwargs.get("state"):
94+
state = kwargs["state"]
95+
serialized.update({"state": state})
96+
97+
if kwargs.get("county"):
98+
county = kwargs["county"]
99+
serialized.update({"county": county})
100+
101+
return serialized
102+
77103
class TimelinedLocation(Location):
78104
"""
79105
A location with timelines.
@@ -96,29 +122,19 @@ def __init__(self, id, country, province, coordinates, last_updated, timelines):
96122

97123
# Set timelines.
98124
self.timelines = timelines
125+
self.inc_timelines = false
99126

100127
# pylint: disable=arguments-differ
101-
def serialize(self, timelines=False):
128+
@decoratedSerialize(timelines=self.timelines, inc_timelines=self.inc_timelines)
129+
def serialize(self, inc_timelines=false):
102130
"""
103131
Serializes the location into a dict.
104-
105-
:param timelines: Whether to include the timelines.
132+
:param inc_timelines: Whether to include the timelines.
106133
:returns: The serialized location.
107134
:rtype: dict
108135
"""
109136
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-
)
137+
self.inc_timelines = inc_timelines
122138

123139
# Return the serialized location.
124140
return serialized

app/location/csbs.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,16 @@ def __init__(self, id, state, county, coordinates, last_updated, confirmed, deat
2525
self.state = state
2626
self.county = county
2727

28-
def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
28+
@decoratedSerialize(state=self.state, county=self.county)
29+
def serialize(self): # pylint: disable=arguments-differ,unused-argument
2930
"""
3031
Serializes the location into a dict.
32+
3133
3234
:returns: The serialized location.
3335
:rtype: dict
3436
"""
3537
serialized = super().serialize()
3638

37-
# Update with new fields.
38-
serialized.update(
39-
{"state": self.state, "county": self.county,}
40-
)
41-
4239
# Return the serialized location.
4340
return serialized

app/location/nyt.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@ def __init__(self, id, state, county, coordinates, last_updated, timelines):
1414
self.state = state
1515
self.county = county
1616

17-
def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
17+
@decoratedSerialize(timelines=self.timelines, state=self.state, county=self.county)
18+
def serialize(self, inc_timelines=False): # pylint: disable=arguments-differ,unused-argument
1819
"""
1920
Serializes the location into a dict.
20-
2121
:returns: The serialized location.
2222
:rtype: dict
2323
"""
24-
serialized = super().serialize(timelines)
25-
26-
# Update with new fields.
27-
serialized.update(
28-
{"state": self.state, "county": self.county,}
29-
)
24+
serialized = super().serialize(inc_timelines)
3025

3126
# Return the serialized location.
3227
return serialized

0 commit comments

Comments
 (0)