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' # '{}' 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}')
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
File details
Details for the file ms-settings-0.1.2.tar.gz
.
File metadata
- Download URL: ms-settings-0.1.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26696fa36c328cdf903436ba05e54ce1e03b0a78a09f57a331d8ed49fdf3209a |
|
MD5 | 766aeffcb81d0904f6fa0a3e3114b0a5 |
|
BLAKE2b-256 | 591480876740de704c2feebd8d02777023983d68de6b78652ee18ac8584a16b8 |
File details
Details for the file ms_settings-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: ms_settings-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45cd8c3ba2be920ec18b5b7346271f17686dc01d38ba7ba565bf03e97128d1d2 |
|
MD5 | 39131f52b54377cb83adf107152b1fea |
|
BLAKE2b-256 | e2b46987b4fbc7dd2c330000ad4a2e6bc5ce92bc152acc68c1f128109072f3df |