From 776e4f707e7e21dfabf7f2b39786c0d38fe70919 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 15 Aug 2021 21:13:29 -0400 Subject: [PATCH 1/4] test1Ignore --- app/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/config.py b/app/config.py index 377ebc13..14977932 100644 --- a/app/config.py +++ b/app/config.py @@ -7,7 +7,7 @@ CFG_LOGGER = logging.getLogger("app.config") -class _Settings(BaseSettings): +class _Settings(BaseSettings): #comment port: int = 5000 rediscloud_url: AnyUrl = None local_redis_url: AnyUrl = None From 44168f2ccaa0bc64544642451ff156797b066762 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 15 Aug 2021 21:16:15 -0400 Subject: [PATCH 2/4] test1 text removed --- app/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/config.py b/app/config.py index 14977932..377ebc13 100644 --- a/app/config.py +++ b/app/config.py @@ -7,7 +7,7 @@ CFG_LOGGER = logging.getLogger("app.config") -class _Settings(BaseSettings): #comment +class _Settings(BaseSettings): port: int = 5000 rediscloud_url: AnyUrl = None local_redis_url: AnyUrl = None From c3f03718921c4cf629ad46a5305f58bfadc82389 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 15 Aug 2021 23:25:55 -0400 Subject: [PATCH 3/4] adds singleton design patter to the settings class to ensure only one instance of settings can be instanciated --- app/caches.py | 4 ++-- app/config.py | 34 +++++++++++++++++++++------------- app/main.py | 5 ++--- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/caches.py b/app/caches.py index df95f508..c6cfa5e4 100644 --- a/app/caches.py +++ b/app/caches.py @@ -5,11 +5,11 @@ import aiocache -from .config import get_settings +from .config import _Settings LOGGER = logging.getLogger(name="app.caches") -SETTINGS = get_settings() +SETTINGS = _Settings.get_settings() if SETTINGS.rediscloud_url: REDIS_URL = SETTINGS.rediscloud_url diff --git a/app/config.py b/app/config.py index 377ebc13..c65123e1 100644 --- a/app/config.py +++ b/app/config.py @@ -8,6 +8,9 @@ class _Settings(BaseSettings): + + settingsInstance = None + port: int = 5000 rediscloud_url: AnyUrl = None local_redis_url: AnyUrl = None @@ -16,18 +19,23 @@ class _Settings(BaseSettings): # Sentry sentry_dsn: str = None + def __init__(self): + if _Settings.settingsInstance: + raise Exception("This is a singleton") + else: + _Settings.settingsInstance = self @functools.lru_cache() -def get_settings(**kwargs) -> BaseSettings: - """ - Read settings from the environment or `.env` file. - https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support - - Usage: - import app.config - - settings = app.config.get_settings(_env_file="") - port_number = settings.port - """ - CFG_LOGGER.info("Loading Config settings from Environment ...") - return _Settings(**kwargs) + def get_settings(**kwargs) -> BaseSettings: + """ + Read settings from the environment or `.env` file. + https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support + + Usage: + import app.config + + settings = app.config._Settings.get_settings(_env_file="") + port_number = settings.port + """ + CFG_LOGGER.info("Loading Config settings from Environment ...") + return _Settings(**kwargs) \ No newline at end of file diff --git a/app/main.py b/app/main.py index b9aff949..43ba2c3c 100644 --- a/app/main.py +++ b/app/main.py @@ -13,7 +13,7 @@ from scout_apm.async_.starlette import ScoutMiddleware from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from .config import get_settings +from .config import _Settings from .data import data_source from .routers import V1, V2 from .utils.httputils import setup_client_session, teardown_client_session @@ -23,7 +23,7 @@ # ############ LOGGER = logging.getLogger("api") -SETTINGS = get_settings() +SETTINGS = _Settings.get_settings() if SETTINGS.sentry_dsn: # pragma: no cover sentry_sdk.init(dsn=SETTINGS.sentry_dsn) @@ -113,7 +113,6 @@ async def handle_validation_error( APP.include_router(V1, prefix="", tags=["v1"]) APP.include_router(V2, prefix="/v2", tags=["v2"]) - # Running of app. if __name__ == "__main__": uvicorn.run( From 99a467507d1c58a64040a08800c17702fb90abd3 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 15 Aug 2021 23:55:14 -0400 Subject: [PATCH 4/4] structural design pattern to ensure all location services are contained inside the composite class --- app/services/location/Composite.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/services/location/Composite.py diff --git a/app/services/location/Composite.py b/app/services/location/Composite.py new file mode 100644 index 00000000..150bbff7 --- /dev/null +++ b/app/services/location/Composite.py @@ -0,0 +1,22 @@ + +class Composite(LocationService): + + + def init(self): + self._children: List[Component] = [] + + + def add(self, LocationService): + self._children.append(LocationService) + component.parent = self + + def remove(self, LocationService) -> None: + self._children.remove(LocationService) + component.parent = None + + def is_composite(self) -> bool: + return True + + def operation(self): + for child in self._children: + child.getLocations() \ No newline at end of file