Code Review Asked by moshevi on November 30, 2021
I am following a guide that specifies how to manage different configurations (DB_URI, LOGGER_NAME, etc…) per development environments.
I create a config
module that is structured like this
my_projectconfig__init__.py
import os
import sys
from . import settings
# create settings object corresponding to specified env
APP_ENV = os.environ.get('APP_ENV', 'Dev')
_current_config = vars(setting).get(f'{APP_ENV}Config')
# copy attributes to the module for convenience
for atr in [f for f in dir(_current_config) if '__' not in f]:
# environment can override anything
val = os.environ.get(atr, getattr(_current_config, atr))
setattr(sys.modules[__name__], atr, val)
my_projectconfigsettings.py
class BaseConfig:
DEBUG = True
LOGGER_NAME = 'my_app'
class DevConfig(BaseConfig):
# In actuality calling a function that fetches a secret
SQLALCHEMY_DATABASE_URI = 'sqlite://user.db'
class ProductionConfig(BaseConfig):
DEBUG = False
# In actuality calling a function that fetches a secret
SQLALCHEMY_DATABASE_URI = 'sqlite://user.db'
calling the config
application.py
import config
config.SQLALCHEMY_DATABASE_URI
This approach is kind of a hit and miss for me.
on one hand it allows me to easily specify several environments (dev, stage, prod, dev) without having a complex if else
logic in my settings.py
.
On the other hand I dont like using setattr
on sys.modules[__name__]
, seems a bit wonky to me. what do you think ?
Incase you are using Docker for deployment. The required environment variable can be configured as Kuberenetes configuration and it reads based on the deployment configuration environment.
Answered by Sudharsan S on November 30, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP