Python hybrid settings from multiple sources.
Project description
Description
This package can load settings from multiple sources and hybrid them into one dict-like object.
Installation
For Python 3.5+
pip install hsettings
Usage
Load settings
Load settings from dict.
from hsettings.loaders import DictLoader
data1 = {
'k1': 'v1',
'k2': 'v2',
'k3': 1,
'k4': '1'
}
settings = DictLoader.load(data1)
// type casts
settings = DictLoader.load(data1, casts={
'k3': str, 'k4': int
})
assert settings.get('k3') == '1'
assert settings.get('k4') == 1
// keys includes
settings = DictLoader.load(data1, includes=['k1', 'k2'])
assert settings.as_dict() == {'k1': 'v1', 'k2': 'v2'}
// keys excludes
settings = DictLoader.load(data1, excludes=['k3', 'k4'])
assert settings.as_dict() == {'k1': 'v1', 'k2': 'v2'}
// keys includes and excludes
settings = DictLoader.load(
data1,
includes=['k1', 'k2'],
excludes=['k2', 'k3']
)
assert settings.as_dict() == {'k1': 'v1'}
// map keys to inner keys
settings = DictLoader.load(data1, key_mappings={
'k2': 'k2.k2-1',
'k3': 'k2.k2-2'
})
assert settings.as_dict() == {
'k1': 'v1',
'k2': {
'k2-1': 'v2',
'k2-2': 1
},
'k4': '1'
}
// map keys to inner keys and only contains these keys
// this is useful to load use defined envs
settings = DictLoader.load(data1, key_mappings={
'k2': 'k2.k2-1',
'k3': 'k2.k2-2'
}, only_key_mappings_includes=True)
assert settings.as_dict() == {
'k2': {
'k2-1': 'v2',
'k2-2': 1
}
}
Load settings from json file.
from hsettings.loaders import JsonLoader
settings = JsonLoader.load(json_file)
Load settings from yaml file.
from hsettings.loaders import YamlLoader
settings = YamlLoader.load(yaml_file)
Load settings from environment and/or env file. Support casts, env_to_key_mapping, includes, excludes and only_key_mappings_includes as DictLoader. Use env_to_key_mapping and only_key_mappings_includes parameters to only get specific env settings.
// load from environment
from hsettings.loaders import EnvLoader
settings = EnvLoader.load()
// load from environment and env file
from hsettings.loaders import EnvLoader
settings = EnvLoader.load(env_file)
Use settings
data1 = {
'k1': 'v1',
'k2': {
'k2-1': 'v2-1',
'k2-2': 'v2-2'
},
'k3': ['v3-1', 'v3-2'],
'k4': 1,
'k5': [0, 1, 2],
'k6': {
'k6-1': '',
'k6-2': None,
'k6-3': 1.2
},
'k7': {
'k7-1': {
'k7-1-1': 7
}
}
}
settings = Settings(data1)
// get settings by get method or []
print(settings.get('k1'))
// output v1
print(settings['k1'])
// output v1
// use dot(.) to get inner value
print(settings.get('k2.k2-1'))
// output v2-1
print(settings['k2.k2-1'])
// output v2-1
// set default value if not set
// note None is not equal to not set
print(settings.get('not_set', 'yes'))
// output yes
// get whole settings as dict
print(settings.as_dict())
// clone a new settings
settings2 = settings.clone()
print(settings2 == settings)
// output True
print(settings2 is settings)
// output False
// set settings by set method or []
settings.set('k3', 'v3')
print(settings['k3])
// output v3
settings['k3'] = 'vv3'
print(settings['k3'])
// output vv3
// merge settings, keys conflict will be overrided
data2 = {
'k1': 'kk1',
'k2': {
'k2-1': 'kk2-1'
},
'k5': [3, 4],
'k6': {
'k6-2': 'kk6-2',
'k6-4': 2.4
}
}
settings3 = settings.clone()
settings3.merge(data2)
print(settings3['k2.k2-1'])
print(settings3['k2.k2-1'])
// output kk2-1
// output v2-1
More examples are in tests.
Test
Run unit test
pytest
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
hsettings-0.1.5.tar.gz
(5.5 kB
view details)
Built Distribution
File details
Details for the file hsettings-0.1.5.tar.gz
.
File metadata
- Download URL: hsettings-0.1.5.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 181d283e25c2e5cd97ae6ceedff92e1a2bf4f3007570a024dca40039b9e788c5 |
|
MD5 | e953ccdccacf1e7da3520cdcd78f446f |
|
BLAKE2b-256 | 8011b7af08d95b3b1943b0079289a19300d19d0ccbcc1158a0e0348bd3a3d6b7 |
File details
Details for the file hsettings-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: hsettings-0.1.5-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ab0672554cfece4f10379e1936d4fbec27a5c19e892ab4a9846b2211ce86da8 |
|
MD5 | cc61a844bb20040b47a7b0c421959654 |
|
BLAKE2b-256 | bc45e1524e7735ab37fd8ab078781d099a941d2c9a2850110c5599647fbf767f |