Skip to content

Commit 062b817

Browse files
authored
Creational design pattern
1 parent 1c7e4ae commit 062b817

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cre.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""app.config.py"""
2+
import functools
3+
import logging
4+
5+
from pydantic import AnyUrl, BaseSettings
6+
7+
CFG_LOGGER = logging.getLogger("app.config")
8+
9+
10+
class _Settings(BaseSettings):
11+
__instance = None
12+
13+
def __init__(self):
14+
if not _Settings.__instance:
15+
print("I have already got")
16+
else:
17+
port: int = 5000
18+
rediscloud_url: AnyUrl = None
19+
local_redis_url: AnyUrl = None
20+
# Scout APM
21+
scout_name: str = None
22+
# Sentry
23+
sentry_dsn: str = None
24+
25+
@functools.lru_cache()
26+
def get_settings(**kwargs) -> BaseSettings:
27+
"""
28+
Read settings from the environment or `.env` file.
29+
https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support
30+
31+
Usage:
32+
import app.config
33+
34+
settings = app.config.get_settings(_env_file="")
35+
port_number = settings.port
36+
"""
37+
CFG_LOGGER.info("Loading Config settings from Environment ...")
38+
if _Settings.__instance is None:
39+
_Settings.__instance = _Settings(**kwargs)
40+
else:
41+
return _Settings(**kwargs)

0 commit comments

Comments
 (0)