From 062b81728cecfce1d21e00fe116017f5faf2de9a Mon Sep 17 00:00:00 2001 From: Dohyun Kim <84333692+dohyun97@users.noreply.github.com> Date: Sat, 14 Aug 2021 23:48:46 -0400 Subject: [PATCH 1/2] Creational design pattern --- cre.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cre.py diff --git a/cre.py b/cre.py new file mode 100644 index 00000000..c43dee32 --- /dev/null +++ b/cre.py @@ -0,0 +1,41 @@ +"""app.config.py""" +import functools +import logging + +from pydantic import AnyUrl, BaseSettings + +CFG_LOGGER = logging.getLogger("app.config") + + +class _Settings(BaseSettings): + __instance = None + + def __init__(self): + if not _Settings.__instance: + print("I have already got") + else: + port: int = 5000 + rediscloud_url: AnyUrl = None + local_redis_url: AnyUrl = None + # Scout APM + scout_name: str = None + # Sentry + sentry_dsn: str = None + + @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 ...") + if _Settings.__instance is None: + _Settings.__instance = _Settings(**kwargs) + else: + return _Settings(**kwargs) From b61a071334ad472b2a7c6886d7eb3b1486d1a8b5 Mon Sep 17 00:00:00 2001 From: Dohyun Kim <84333692+dohyun97@users.noreply.github.com> Date: Sun, 15 Aug 2021 21:30:38 -0400 Subject: [PATCH 2/2] creational design pattern --- cre.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cre.py b/cre.py index c43dee32..9717f6fa 100644 --- a/cre.py +++ b/cre.py @@ -23,6 +23,7 @@ def __init__(self): sentry_dsn: str = None @functools.lru_cache() + @staticmethod def get_settings(**kwargs) -> BaseSettings: """ Read settings from the environment or `.env` file.