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
refactor: apply singleton pattern in app/services
  • Loading branch information
FrankX-xzh committed Aug 15, 2021
commit a589091f3f152ef7755c1b31c5fa560a45acb2ac
13 changes: 0 additions & 13 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
from ..utils.populations import country_population



class Locations:
def __init__(self,locations: list[Location]):
self.locations = locations

def add_service(self,location):
self.locations.append(location)

def get_location(self):
return locations



# pylint: disable=redefined-builtin,invalid-name
class Location: # pylint: disable=too-many-instance-attributes
"""
Expand Down
16 changes: 8 additions & 8 deletions app/services/location/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""app.services.location"""
from abc import ABC, abstractmethod

def singleton(cls):
instances = {}

class LocationServices:
def __init__(self,location_services: list[LocationService]):
self.location_services = location_services
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]

def add_service(self,location_service):
self.location_services.append(location_service)

def get_service(self):
return location_services
return wrapper

@singleton
class LocationService(ABC):
"""
Service for retrieving locations.
Expand Down
2 changes: 1 addition & 1 deletion app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

LOGGER = logging.getLogger("services.location.csbs")


@singleton
class CSBSLocationService(LocationService):
"""
Service for retrieving locations from csbs
Expand Down
1 change: 1 addition & 0 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
PID = os.getpid()


@singleton
class JhuLocationService(LocationService):
"""
Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19).
Expand Down
1 change: 1 addition & 0 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
LOGGER = logging.getLogger("services.location.nyt")


@singleton
class NYTLocationService(LocationService):
"""
Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data).
Expand Down