Dynamic Config Class Creation using Environment Variables
Project description
LAZY_ENV_CONFIGURATOR
A utility library for Dynamic Config class generation. Now no more repeated boilerplate code...
Before lazy_env_configurator
, config classes used to be created as below.
class BaseConfig:
APP = os.environ.get("APP")
APP_ENV = os.environ.get("APP_ENV")
# authentication related configuration
# DB Credentials
DB_USERNAME = os.environ.get("DB_USERNAME")
DB_PASSWORD = os.environ.get("DB_PASSWORD")
DB_HOST = os.environ.get("DB_HOST")
DB_PORT = os.environ.get("DB_PORT")
DB_NAME = os.environ.get("DB_NAME")
DB_DRIVER = os.environ.get("DB_DRIVER")
This use to require a lot of boiler plate and redundant code With lazy_env_configurator
this can be reduced to below:
from lazy_env_configurator import BaseEnv, BaseConfig as Config_
class BaseConfig(BaseEnv):
class Config(Config_):
envs = ('APP',
'APP_ENV',
'DB_USERNAME',
'DB_PASSWORD',
'DB_PASSWORD',
'DB_HOST',
# defaults
('DB_PORT',3306),
'DB_NAME',
'DB_DRIVER'
)
Benefits of using lazy_env_configurator
over Normal classes
- Low memory footprint.
- Lazily evaluates environment variable and only loads them when used.
- Once loaded, env variables are cached.
- Get defaults populated, in case of missing
env
variables. - env attributes can be overridden easily.
- classes expose
instance
attribute preventing need to initialization and making it behave singleton. - Loads
.env
files by default so you only need to focus on essentials.
How to use
Let us refer below example:
from lazy_env_configurator import BaseEnv, BaseConfig as Config_
class BaseConfig(BaseEnv):
class Config(Config_):
envs = ('APP',
'APP_ENV',
'DB_USERNAME',
'DB_PASSWORD',
'DB_PASSWORD',
'DB_HOST',
# defaults
('DB_PORT',3306),
'DB_NAME',
'DB_DRIVER'
)
We can now use BaseConfig
class create as below.
>>>BaseConfig.instance.APP
Every class, subclassed from BaseEnv
would expose .instance
attribute which will be instance of the subclass. This instance can be used to access all the attributes on the class.
For simplicity,
metaclass
pre-initialises created class, so to make it behave as singleton.
How this works ?
lazy_env_configurator
uses descriptors
under the hood to dynamically populate env variables as attributes, thus making them available on demand, Lazily
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for lazy_env_configurator-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f19cf5ca01024a68ecb6c14324e8c2536fd30029c40f0b73c2066bfb389b68b4 |
|
MD5 | 3c7bcf12384c87ab7078dad9e18bfd56 |
|
BLAKE2b-256 | 2534202996072497d5c0235cbc4fcc674f659cf40580da36db8e4e001406ab00 |
Hashes for lazy_env_configurator-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abbc5c782f2930c1fe2ea6ddd738024d7140294acc3e8011070b4fd18fbefa7a |
|
MD5 | 0c2215ae97682456e28e508158c4dbd4 |
|
BLAKE2b-256 | bdb881dca5f9d2de9defdfcdd101ef41321067aa4c0f23524d124ea251389be8 |