A collection of utilities for working with Dictionaries in Python.
Project description
dict-tape
A collection of Python utilities for manipulating dictionaries
Installation
pip install dict-tape
Usage
from dict_tape import chain_get
Features
Traversal
chain_get
This is intended to help solve problems like this with deeply nested dictionaries:
some_dict.get('a', {})[0].get('b', {}).get('e', 'default')
The problem is that while .get()
can help with chains of dictionaries, if you have lists
intermixed in there, you can't sanely default things and have to just hope the list has items
(since list indexing will throw an exception if the list is empty).
But with chain_get()
you can accomplish this in a way that will sanely default:
from dict_tape import chain_get
some_dict = {'a': [{'b': {'e': 'value'}}]}
# Prints 'value'
print(chain_get(some_dict, 'a', 0, 'b', 'e', default='default'))
some_dict = {'a': []}
# Prints 'default'
print(chain_get(some_dict, 'a', 0, 'b', 'e', default='default'))
chain_get()
will also do some sanity checking on your types and arguments:
- Checks to make sure that every new level is a
dict
,list
, orstr
- If the current data being indexed is a
list
orstr
, it will check that the arg provided is anint
- If the current data being indexed is a
dict
, it will check that the arg provided is anint
orstr
- If the resulting data it finds with traversal doesn't match the type provided for
default
, it will throw an error so you know you got something you apparently didn't expect
Options
default
: The default value to return if the chain fails at any point.check_arg_types
: You can set this toFalse
to disable checking whether arg types are valid for data. If this is disabled, when it would normally throw an error for this problem it will instead return thedefault
value.check_data_types
: You can set this toFalse
to disable checking whether the data types are valid. If this is disabled, when it would normally throw an error for this problem it will instead return thedefault
value.
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
dict_tape-1.3.0.tar.gz
(17.7 kB
view hashes)
Built Distribution
dict_tape-1.3.0-py3-none-any.whl
(15.8 kB
view hashes)
Close
Hashes for dict_tape-1.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0167d52a7bf239d85273cd5ab046a526883b30fd1d55fed8189486ab8e474db |
|
MD5 | 0ded94c239a46a075ccfe501c4510ac1 |
|
BLAKE2b-256 | d7d1d451c54bf49f8240cef01255769742966825edc41ed680af3fe1eee38070 |