Hybrid Settings
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
Metadata
Release files for hsettings 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hsettings-0.1.5.tar.gz | 5.5 kB | Without provenance | Details for hsettings-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hsettings-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for hsettings-0.1.5-py3-none-any.whl |
Total release size: 11.2 kB
Release files / hsettings-0.1.5.tar.gz
| Download URL | hsettings-0.1.5.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
181d283e25c2e5cd97ae6ceedff92e1a2bf4f3007570a024dca40039b9e788c5
|
|
BLAKE2b-256 checksum How to use checksums |
8011b7af08d95b3b1943b0079289a19300d19d0ccbcc1158a0e0348bd3a3d6b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / hsettings-0.1.5-py3-none-any.whl
| Download URL | hsettings-0.1.5-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6ab0672554cfece4f10379e1936d4fbec27a5c19e892ab4a9846b2211ce86da8
|
|
BLAKE2b-256 checksum How to use checksums |
bc45e1524e7735ab37fd8ab078781d099a941d2c9a2850110c5599647fbf767f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for hsettings 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.