python-benedict
python-benedict is the Python dictionary for humans dealing with evil/complex data.
Features
Full keypath support (using the dot syntax)
Many utility methods to retrieve data as needed (all methods listed below)
Give benediction to dict objects before they are returned (they receive benedict casting)
100% backward-compatible (you can replace existing dicts without pain)
Requirements
Python 2.7, 3.4, 3.5, 3.6, 3.7
Installation
Run pip install python-benedict
Testing
Run tox / python setup.py test
Usage
benedict is a dict subclass, so it is possible to use it as a normal dict (you can just cast an existing dict).
Basic get/set using keypath
from benedict import benedict
d = benedict()
d['profile.firstname'] = 'Fabio'
d['profile.lastname'] = 'Caccamo'
print(d) # -> { 'profile':{ 'firstname':'Fabio', 'lastname':'Caccamo' } }
print(d['profile']) # -> { 'firstname':'Fabio', 'lastname':'Caccamo' }
print('profile.lastname' in d) # -> True
Extra methods
# Return a deepcopy of the dict.
d.deepcopy()
# Return a list of all keypaths in the dict.
d.keypaths()
Utility methods
# Get value by key or keypath trying to return it as bool.
# Values like `1`, `true`, `yes`, `on`, `ok` will be returned as `True`.
d.get_bool(key, default=False)
# Get value by key or keypath trying to return it as list of bool values.
# If separator is specified and value is a string it will be splitted.
d.get_bool_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as datetime.
# If format is not specified it will be autodetected.
# If options and value is in options return value otherwise default.
d.get_datetime(key, default=None, format=None, options=[])
# Get value by key or keypath trying to return it as list of datetime values.
# If separator is specified and value is a string it will be splitted.
d.get_datetime_list(key, default=[], format=None, separator=',')
# Get value by key or keypath trying to return it as Decimal.
# If options and value is in options return value otherwise default.
d.get_decimal(key, default=Decimal('0.0'), options=[])
# Get value by key or keypath trying to return it as list of Decimal values.
# If separator is specified and value is a string it will be splitted.
d.get_decimal_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as dict.
# If value is a json string it will be automatically decoded.
d.get_dict(key, default={})
# Get value by key or keypath trying to return it as float.
# If options and value is in options return value otherwise default.
d.get_float(key, default=0.0, options=[])
# Get value by key or keypath trying to return it as list of float values.
# If separator is specified and value is a string it will be splitted.
d.get_float_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as int.
# If options and value is in options return value otherwise default.
d.get_int(key, default=0, options=[])
# Get value by key or keypath trying to return it as list of int values.
# If separator is specified and value is a string it will be splitted.
d.get_int_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as list.
# If separator is specified and value is a string it will be splitted.
d.get_list(key, default=[], separator=',')
# Get list by key or keypath and return value at the specified index.
# If separator is specified and list value is a string it will be splitted.
d.get_list_item(key, index=0, default=None, separator=',')
# Get value by key or keypath trying to return it as slug.
# If options and value is in options return value otherwise default.
d.get_slug(key, default='', options=[])
# Get value by key or keypath trying to return it as list of slug values.
# If separator is specified and value is a string it will be splitted.
d.get_slug_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as string.
# Encoding issues will be automatically fixed.
# If options and value is in options return value otherwise default.
d.get_str(key, default='', options=[])
# Get value by key or keypath trying to return it as list of str values.
# If separator is specified and value is a string it will be splitted.
d.get_str_list(key, default=[], separator=',')
License
Released under MIT License.
Metadata
Release files for python-benedict 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python-benedict-0.3.0.tar.gz | 8.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_benedict-0.3.0-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 18.3 kB
Release files / python-benedict-0.3.0.tar.gz
| Download URL | python-benedict-0.3.0.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d01f2da3278388fd4e328eb7a867a692f72e9e50e11fcee17e695b13a5b489e9
|
|
BLAKE2b-256 checksum How to use checksums |
42138ebf201bccb539874c0497cfd14a677adcada36e9bfe54dd2d44acae2d9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.3
|
Release files / python_benedict-0.3.0-py2-none-any.whl
| Download URL | python_benedict-0.3.0-py2-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
7b63b65a33e722be2860015a6f406da7a223f4d151c997fbef92ecac03c076d8
|
|
BLAKE2b-256 checksum How to use checksums |
24680fb3480ab1d872e45e8a411a40e5a819d522dbaac909b28f4cab4f877e24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.3
|