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
Noneif 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.pywith 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' # '{}' optional
API_LIMIT = '$API_LIMIT:20<int>' # environment variable with int value converter
COLLECTION_NAME = 'collections' # plain python string value
_config_files_=['./config.ini']
MONGO_DB_NAME=None # environment dependent plain text data we can save in config.ini(reduce environment variable count)
PRICE = None
patch_settings()
# patch_settings(__name__) specify module explicitly
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
# plain text value in configure file with float converter
PRICE = 25.5<float>
[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}')
Release files for ms-settings 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ms-settings-0.1.2.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ms_settings-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.6 kB
Release files / ms-settings-0.1.2.tar.gz
| Download URL | ms-settings-0.1.2.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26696fa36c328cdf903436ba05e54ce1e03b0a78a09f57a331d8ed49fdf3209a
|
|
BLAKE2b-256 checksum How to use checksums |
591480876740de704c2feebd8d02777023983d68de6b78652ee18ac8584a16b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.12
|
Release files / ms_settings-0.1.2-py3-none-any.whl
| Download URL | ms_settings-0.1.2-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
45cd8c3ba2be920ec18b5b7346271f17686dc01d38ba7ba565bf03e97128d1d2
|
|
BLAKE2b-256 checksum How to use checksums |
e2b46987b4fbc7dd2c330000ad4a2e6bc5ce92bc152acc68c1f128109072f3df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.6.12
|