option_merge

Code to deeply merge multiple python dictionaries


Keywords
deep, merge
License
WTFPL
Install
pip install option_merge==1.6

Documentation

Option Merge

This provides the option_merge.MergedOptions class, which allows you to treat multiple python dictionaries as one.

Documentation can be found at http://option-merge.readthedocs.org/

Deprecated

This project has been deprecated in favour of https://github.com/delfick/delfick_project

Usage

Either the classmethod shortcut:

options = MergedOptions.using(options1, options2)

Or with the update method:

options = MergedOptions()
options.update(options1)
options.update(options2)

And a separate override with __setitem__ syntax:

options['a'] = 1
options['b.c'] = 2
options['b'] = {"d": 3}
options[['b', 'e']] = 4

options.as_dict() == {"a": 1, "b.c": 2, "b": {"d": 3, "e": 4}}

When options are added, copies are made.

When you delete a key, it removes it from the first dictionary it can find. This means a key can change value when deleted rather than disappearing altogether

So:

options1 = {'a':{'b':1, 'c':3}, 'b':5}
options2 = {'a':{'b':4', 'c':9}, 'd':7}
options = MergedOptions.using(options1, options2)

# options['a'] == MergedOptions(prefix='a', <same_options>, <same_overrides>)
# options['a']['b'] == 4
# options['a']['c'] == 9
# options['d'] == 7

del options['a']['c']
# options['a']['c'] == 3

You may also get all values for a key with merged.values_for(path) where path is a dot-separated path. So options.values_for("a.b") == [4, 1]

You can also get all deeply nested keys with merged.all_keys(). So merged.all_keys() == ["d", "a.b", "b", "a.c"]

Installation

Use pip!:

pip install option_merge

Or if you're developing it:

pip install -e .
pip install -e ".[tests]"

Tests

Run the helpful script:

./test.sh