Skip to content

Commit a589091

Browse files
committed
refactor: apply singleton pattern in app/services
1 parent 8e957d2 commit a589091

File tree

5 files changed

+11
-22
lines changed

5 files changed

+11
-22
lines changed

app/location/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44
from ..utils.populations import country_population
55

66

7-
8-
class Locations:
9-
def __init__(self,locations: list[Location]):
10-
self.locations = locations
11-
12-
def add_service(self,location):
13-
self.locations.append(location)
14-
15-
def get_location(self):
16-
return locations
17-
18-
19-
207
# pylint: disable=redefined-builtin,invalid-name
218
class Location: # pylint: disable=too-many-instance-attributes
229
"""

app/services/location/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
"""app.services.location"""
22
from abc import ABC, abstractmethod
33

4+
def singleton(cls):
5+
instances = {}
46

5-
class LocationServices:
6-
def __init__(self,location_services: list[LocationService]):
7-
self.location_services = location_services
7+
def wrapper(*args, **kwargs):
8+
if cls not in instances:
9+
instances[cls] = cls(*args, **kwargs)
10+
return instances[cls]
811

9-
def add_service(self,location_service):
10-
self.location_services.append(location_service)
11-
12-
def get_service(self):
13-
return location_services
12+
return wrapper
1413

14+
@singleton
1515
class LocationService(ABC):
1616
"""
1717
Service for retrieving locations.

app/services/location/csbs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

17-
17+
@singleton
1818
class CSBSLocationService(LocationService):
1919
"""
2020
Service for retrieving locations from csbs

app/services/location/jhu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
PID = os.getpid()
2222

2323

24+
@singleton
2425
class JhuLocationService(LocationService):
2526
"""
2627
Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19).

app/services/location/nyt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
LOGGER = logging.getLogger("services.location.nyt")
1717

1818

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

0 commit comments

Comments
 (0)