Table of Contents
Motivation
Sometimes a task which should be easy can be hard to accomplish in practice.
Manipulating nested dictionaries in Python seems to be such a task.
Without dotty
Accessing nested dictionaries looks like this
data = { 'a': { 'b': { 'c': 'd' } } } assert data['a']['b']['c'] == 'd'
You have to chain the keys to get down to the nested dict and get the value for that key
With dotty
Installation
You can install dotty with
pip install dotty-dict
Usage
from dotty_dict import dotty data = { 'a': { 'b': { 'c': 'd' } } } dot = dotty(data)
assert 'a.b' in dot assert dot['a.b.c'] == 'd'
After importing dotty you can wrap your existing vanilla Python dictionary in a dotty object.
Now you can access your nested values by chaining the key with the dot to a “key path”
Updating dictionaries
data = { 'a': { 'b': { 'c': 'd' } } } dot = dotty(data) dot['a.b.c'] = 'e' assert dot['a.b.c'] == {'c': 'e'}