A simple setting tool for project
Project description
ms_settings
A simple explicit module level settings with env and file configuration
Description
Nowadays, we have a lot of libs to help us configure our project, like dotenv, Django, dynaconf, etc. But I prefer to follow the ZEN of python explicit is better than implicit.
I created this project to make project setting more explicit which will help us know better about all our projection configuration options during configuration period, and also make the IDE more friendly during the programming period.
Quick tour
If we have a project like the following structure:
project/ ├── config.ini │ ├── settings.py │ └── main.py
In our settings.py
we can specify the following configuration options:
- Raw python value/object option value, which will keep their origin value:
PY_INT = 20
- Environment values:
PY_ENV_VALUE=${PY_ENV_VALUE:default_env_value<converter>}
- Environment value without default value, which will be
None
if environment variable not exist - Environment value with default, but no converter. which will have the default value with string type
- Environment value with default and convert: the value will be converted to the right datatype, will raise Exception if can not be converted to the right data type
- Environment value without default value, which will be
- Configuration file support(
.ini
), the following parts should be noticed:- All configuration options in the configure file should be declared in
settings.py
with uppercase format - It's better only keep the environment related plain text configuration, no environment(even this is supported)
- The sections should follow: ['default', 'test', 'development', 'homolog', 'staging', 'production']. Of course you can cusomize these section during the initiate process
- All configuration options in the configure file should be declared in
For example, we want to configure a mongo database connection for our project, in our settings.py
, should be like:
from ms_settings.patch import patch_settings
API_ENV='${API_ENV:dev}'
MONGO_DB_URI='${MONGO_DB_URI:mongodb://localhost:27017}' # sensible environment dependent data
COLLECTION_NAME = 'collections' #none environment dependent plain text data
_config_files_=['./config.ini']
MONGO_DB_NAME=None # environment dependent plain text data we can save in config.ini(reduce environment variable count)
patch_settings(__name__) # **NOTE**, this call must be called before import the real setting module
in our config.ini
, we can save some non sensible environment dependent data, to avoid too much environment variable
[default]
MONGO_DB_NAME = dev_demoDB
[test]
MONGO_DB_NAME = test_demoDB
[staging]
MONGO_DB_NAME = staging_demoDB
[production]
MONGO_DB_NAME = demoDB
Then, in our main.py
we can use these configurations as following:
from example import settings
def main():
print(f'MONGO_DB_URI:{settings.MONGO_DB_URI}')
print(f'MONGO_DB_NAME: {settings.MONGO_DB_NAME}')
print(f'COLLECTION_NAME: {settings.COLLECTION_NAME}')
Project details
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 ms_settings-0.0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e8cc6e15b431d44cbbc9227972ede7290f6db7fe015e538dac6b7a72f33c173 |
|
MD5 | 4d2c2fd55c4995b8f77ec3c8d87b9006 |
|
BLAKE2b-256 | d5dfb90d666f08878af3b07988d2cb3101d34eceb6d822425ff466d1b498edbb |