Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factory design pattern on Location
  • Loading branch information
codex-spec committed Aug 14, 2021
commit 960a896c2d2e96961ffb52eb6ea6954291b792a7
19 changes: 19 additions & 0 deletions app/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ class Point:
def __init__(self, x, y):
self.x = x
self.y = y



class Person:

def __init__(self, p, w):
self.p = p
self.w = w

personOne = Person("Abdullah", 190)

print(personOne.p)
print(personOne.w)

3
y
n
y
y
53 changes: 53 additions & 0 deletions app/location/DataLocation
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from ..coordinates import Coordinates
from ..utils import countries
from ..utils.populations import country_population
from __init__ import country_population, country_code

# pylint: disable=redefined-builtin,invalid-name
class Location: # pylint: disable=too-many-instance-attributes
"""
A location in the world affected by the coronavirus.
"""

def __init__(
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
): # pylint: disable=too-many-arguments
# General info.
self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates

# Last update.
self.last_updated = last_updated

# Statistics.
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered

def serialize(self):
"""
Serializes the location into a dict.

:returns: The serialized location.
:rtype: dict
"""
return {
# General info.
"id": self.id,
"country": self.country,
"country_code": self.country_code,
"country_population": self.country_population,
"province": self.province,
# Coordinates.
"coordinates": self.coordinates.serialize(),
# Last updated.
"last_updated": self.last_updated,
# Latest data (statistics).
"latest": {
"confirmed": self.confirmed,
"deaths": self.deaths,
"recovered": self.recovered,
},
}
44 changes: 0 additions & 44 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@ class Location: # pylint: disable=too-many-instance-attributes
A location in the world affected by the coronavirus.
"""

def __init__(
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
): # pylint: disable=too-many-arguments
# General info.
self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates

# Last update.
self.last_updated = last_updated

# Statistics.
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered

@property
def country_code(self):
"""
Expand All @@ -47,33 +30,6 @@ def country_population(self):
"""
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.id,
"country": self.country,
"country_code": self.country_code,
"country_population": self.country_population,
"province": self.province,
# Coordinates.
"coordinates": self.coordinates.serialize(),
# Last updated.
"last_updated": self.last_updated,
# Latest data (statistics).
"latest": {
"confirmed": self.confirmed,
"deaths": self.deaths,
"recovered": self.recovered,
},
}


class TimelinedLocation(Location):
"""
A location with timelines.
Expand Down
47 changes: 47 additions & 0 deletions app/location/creationalBP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from ..coordinates import Coordinates
from ..utils import countries
from ..utils.populations import country_population
from ..__init__.py import country_code, country_population

class DataLocation:

def __init__ (self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
):

self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates


self.last_updated = last_updated


self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered

def serialize(self):
"""
Serializes the location into a dict.
:returns: The serialized location.
:rtype: dict
"""
return {

"id": self.id,
"country": self.country,
"country_code": self.country_code,
"country_population": self.country_population,
"province": self.province,

"coordinates": self.coordinates.serialize(),

"last_updated": self.last_updated,
"latest": {
"confirmed": self.confirmed,
"deaths": self.deaths,
"recovered": self.recovered,
},
}
}