Skip to content

Commit 960a896

Browse files
committed
Factory design pattern on Location
1 parent 2133264 commit 960a896

File tree

4 files changed

+119
-44
lines changed

4 files changed

+119
-44
lines changed

app/coordinates.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ class Point:
1616
def __init__(self, x, y):
1717
self.x = x
1818
self.y = y
19+
20+
21+
22+
class Person:
23+
24+
def __init__(self, p, w):
25+
self.p = p
26+
self.w = w
27+
28+
personOne = Person("Abdullah", 190)
29+
30+
print(personOne.p)
31+
print(personOne.w)
32+
33+
3
34+
y
35+
n
36+
y
37+
y

app/location/DataLocation

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from ..coordinates import Coordinates
2+
from ..utils import countries
3+
from ..utils.populations import country_population
4+
from __init__ import country_population, country_code
5+
6+
# pylint: disable=redefined-builtin,invalid-name
7+
class Location: # pylint: disable=too-many-instance-attributes
8+
"""
9+
A location in the world affected by the coronavirus.
10+
"""
11+
12+
def __init__(
13+
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
14+
): # pylint: disable=too-many-arguments
15+
# General info.
16+
self.id = id
17+
self.country = country.strip()
18+
self.province = province.strip()
19+
self.coordinates = coordinates
20+
21+
# Last update.
22+
self.last_updated = last_updated
23+
24+
# Statistics.
25+
self.confirmed = confirmed
26+
self.deaths = deaths
27+
self.recovered = recovered
28+
29+
def serialize(self):
30+
"""
31+
Serializes the location into a dict.
32+
33+
:returns: The serialized location.
34+
:rtype: dict
35+
"""
36+
return {
37+
# General info.
38+
"id": self.id,
39+
"country": self.country,
40+
"country_code": self.country_code,
41+
"country_population": self.country_population,
42+
"province": self.province,
43+
# Coordinates.
44+
"coordinates": self.coordinates.serialize(),
45+
# Last updated.
46+
"last_updated": self.last_updated,
47+
# Latest data (statistics).
48+
"latest": {
49+
"confirmed": self.confirmed,
50+
"deaths": self.deaths,
51+
"recovered": self.recovered,
52+
},
53+
}

app/location/__init__.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,6 @@ class Location: # pylint: disable=too-many-instance-attributes
1010
A location in the world affected by the coronavirus.
1111
"""
1212

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-
3013
@property
3114
def country_code(self):
3215
"""
@@ -47,33 +30,6 @@ def country_population(self):
4730
"""
4831
return country_population(self.country_code)
4932

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-
7733
class TimelinedLocation(Location):
7834
"""
7935
A location with timelines.

app/location/creationalBP.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from ..coordinates import Coordinates
2+
from ..utils import countries
3+
from ..utils.populations import country_population
4+
from ..__init__.py import country_code, country_population
5+
6+
class DataLocation:
7+
8+
def __init__ (self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
9+
):
10+
11+
self.id = id
12+
self.country = country.strip()
13+
self.province = province.strip()
14+
self.coordinates = coordinates
15+
16+
17+
self.last_updated = last_updated
18+
19+
20+
self.confirmed = confirmed
21+
self.deaths = deaths
22+
self.recovered = recovered
23+
24+
def serialize(self):
25+
"""
26+
Serializes the location into a dict.
27+
:returns: The serialized location.
28+
:rtype: dict
29+
"""
30+
return {
31+
32+
"id": self.id,
33+
"country": self.country,
34+
"country_code": self.country_code,
35+
"country_population": self.country_population,
36+
"province": self.province,
37+
38+
"coordinates": self.coordinates.serialize(),
39+
40+
"last_updated": self.last_updated,
41+
"latest": {
42+
"confirmed": self.confirmed,
43+
"deaths": self.deaths,
44+
"recovered": self.recovered,
45+
},
46+
}
47+
}

0 commit comments

Comments
 (0)