Very simple deep_set and deep_get functions to access nested dicts using 'dotted strings' as key.
Project description
Description
Simple functions to set or get values from a nested dict structure
This module DOES NOT implement dotted notation as an alternative access method for dicts. I generally do not like changing python dicts to enable dot notation, hence no available package fitted my needs for a simple deep accessor.
Arguments
deep_get accepts:
- d: the dictionary
- key: a string OR anything accepted by the list() constructor
- access_method: one of METHOD_ATTR (assumed if default is None), METHOD_GET, METHOD_SETDEFAULT (assumed if default is not None). This determines the method to be used to access the nested dict structure.
- default: a callable to be used as default for the dict .get or .setdefault functions
- sep: by default it is a dot '.', you can use anything the string function split will accept
deep_set accepts:
- d: same as above
- key: same as above
- value
- default: if set (not None which is the default), will use setdefault to traverse the nested dict structure
- sep: same as above
How to use
from dict_deep import __version__, deep_get, deep_set
# Alternative 1
d = {'a': {'b': {}}}
deep_set(d, "a.b.c", "Hello World")
print(deep_get(d, "a.b.c"))
# Alternative 2
d = {}
deep_set(d, ['a', 'b', 'c'], "Hello World", default=lambda: dict())
print(deep_get(d, "a.b.c"))
# Alternative 3
d = {}
deep_set(d, "a->b->c", "Hello World", default=lambda: dict(), sep="->")
print(deep_get(d, "a->b->c", sep="->"))
# Alternative 4
d = {}
_ = deep_get(d=d, key=['a', 'b'], default=lambda: dict(), sep=".")
_['c'] = "Hello World"
print(deep_get(d, "a.b.c"))
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-deep-1.2.1.tar.gz
(2.5 kB
view hashes)
Built Distribution
Close
Hashes for dict_deep-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3714b2000608bb84392178680722108ae8c2032592733a3fc4f9a8055229043 |
|
MD5 | 52d027ffdb173ffb69bd71289aa993ab |
|
BLAKE2b-256 | 76660e28d25edf9273e885d523518489538b64124c15454d5d03d1202168051e |