A dictionary with nested access, interpolation and fallback features. It is primarily meant to be used for storing configurations of programs.
Project description
ConfDict
Configuration dictionary that extends built-in python dict with recursive access, self references and fallback functionality. There is no extensive documentation yet, you can check out tests to figure out all features.
Example usage.
>>> from confdict import ConfDict
>>> cd = ConfDict(
# Configuration of ConfDict, these are default values
__separator = '/',
__self_key = '.',
__parent_key = '..',
__root_key = '...',
__key_key = '<key>',
__interpolation_regex = r'({{([^{}]*)}})',
__fallback_key = 'fallback',
# Remaining arguments will directly be stored in underlying dict
users = {
# fallback dict is used when a key is not found at that level
'fallback': {
# <key> is evaluated to key of the current level
# it is useful when used with fallback
'username': '{{<key>}}',
# you can use self references
'ssh_private_key': '/home/{{username}}/.ssh/id_rsa',
},
}
)
>>> cd
ConfDict
{ 'users': { 'fallback': { 'ssh_private_key': '/home/{{username}}/.ssh/id_rsa',
'username': '{{<key>}}'}}}
>>> cd['users/john/username']
'john'
>>> cd['users/john/ssh_private_key']
'/home/john/.ssh/id_rsa'
>>> cd['users/john']
ConfDict
{'ssh_private_key': '/home/john/.ssh/id_rsa', 'username': 'john'}
>>> cd['users/jean']
ConfDict
{'ssh_private_key': '/home/jean/.ssh/id_rsa', 'username': 'jean'}
>>> cd['users/jean'] = { 'username': 'jeans_custom_username' }
>>> cd['users/jean/ssh_private_key']
'/home/jeans_custom_username/.ssh/id_rsa'
>>> # 'jean' exists now under 'users'
>>> # there is no partial fallback so there is no 'ssh_private_key'
>>> cd['users/jean']
ConfDict
{'username': 'jeans_custom_username'}
>>> # we can realize fallback as jean to make concrete values
>>> cd['users/fallback'].realize('jean')
>>> cd['users/jean']
ConfDict
{'ssh_private_key': '/home/jeans_custom_username/.ssh/id_rsa', 'username': 'jeans_custom_username'}
>>> # interpolation still works
>>> cd['users/jean/username'] = 'jeans_custom_username2'
>>> cd['users/jean/ssh_private_key']
'/home/jeans_custom_username2/.ssh/id_rsa'
Installation
$ pip install confdict
Development
There is a Makefile to automate commonly used development tasks.
Environment Setup
### create a virtualenv for development
$ sudo pip install virtualenv # or your preferred way to install virtualenv
$ make virtualenv # will also install dependencies
$ source env/bin/activate
### run pytest / coverage
$ make test
### before commit
$ make format
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file confdict-0.0.1.tar.gz.
File metadata
- Download URL: confdict-0.0.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/5.0.4-arch1-1-ARCH
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a979530e6176cd54ba5d54295b916a282c0f640dc931b9628f11aadaaaa08a58
|
|
| MD5 |
201c5d366e0a343aad76e90a187ca35f
|
|
| BLAKE2b-256 |
b0f871cbab47cd592aa3337f37c94e539ab03c3fb2122e0af34b644c6efcd19a
|
File details
Details for the file confdict-0.0.1-py3-none-any.whl.
File metadata
- Download URL: confdict-0.0.1-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.11 CPython/3.7.2 Linux/5.0.4-arch1-1-ARCH
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb13bae393d73c7dec20d43a8cda42e97c2eb9e901b902ced186d4b93ba6894b
|
|
| MD5 |
2da0dd82d4c09b26120516eddd13377f
|
|
| BLAKE2b-256 |
e263d6855a5badbc95c0fb2966e1c553e21d7c4d811cb023fd49883eb2ff1ffd
|