This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Code to deeply merge multiple python dictionaries

Project Description

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/

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
Release History

Release History

This version
History Node

1.5

History Node

1.4.4

History Node

1.4.3.2

History Node

1.4.3.1

History Node

1.4.3

History Node

1.4.2

History Node

1.4.1

History Node

1.4

History Node

1.3

History Node

1.2

History Node

1.1

History Node

1.0

History Node

0.9.9.8

History Node

0.9.9.7

History Node

0.9.9.6

History Node

0.9.9.5

History Node

0.9.9.4

History Node

0.9.9.3

History Node

0.9.9.2.2

History Node

0.9.9.2.1

History Node

0.9.9.2

History Node

0.9.9.1

History Node

0.9.9

History Node

0.9.8.2

History Node

0.9.8.1

History Node

0.9.8

History Node

0.9.7

History Node

0.9.6

History Node

0.9.5

History Node

0.9.4

History Node

0.9.3

History Node

0.9.2

History Node

0.9.1

History Node

0.9

History Node

0.8.3.1

History Node

0.8.3

History Node

0.8.2

History Node

0.8.1

History Node

0.8

History Node

0.7

History Node

0.6

History Node

0.5

History Node

0.4

History Node

0.3

History Node

0.2

History Node

0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
option_merge-1.5.tar.gz (17.9 kB) Copy SHA256 Checksum SHA256 Source Apr 19, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting