Skip to main content

A Python `dict` with a merge() method.

Project description

mergedict - A Python dict with a merge() method

https://travis-ci.org/schettino72/mergedict.png?branch=master https://coveralls.io/repos/schettino72/mergedict/badge.png

A MergeDict is a dict with a merge() method. merge() is like dict.update()

from mergedict import MergeDict

d1 = MergeDict({'a': 1, 'b': 'one'})
d1.merge({'a':2, 'c': [2]})

assert d1 == {'a': 2, 'c': [2], 'b': 'one'}

A MergeDict can be subclassed to create custom “merge” operations based on the type of an item value.

from mergedict import MergeDict

class SumDict(MergeDict):
      @MergeDict.dispatch(int)
      def merge_int(this, other):
          return this + other

d2 = SumDict({'a': 1, 'b': 'one'})
d2.merge({'a':2, 'b': 'two'})

assert d2 == {'a': 3, 'b': 'two'}

mergedict module comes with a ConfigDict that will extend/update lists/sets/dicts.

from mergedict import ConfigDict

d3 = ConfigDict({'a': 1, 'my_list': [1, 2]})
d3.merge({'a':2, 'my_list': [3, 4]})

assert d3 == {'a': 2, 'my_list': [1, 2, 3, 4]}

Project Details

license

The MIT License Copyright (c) 2013 Eduardo Naufel Schettino

see LICENSE file

developers / contributors

  • Eduardo Naufel Schettino

install

$ pip install mergedict

or download and:

$ python setup.py install

tests

To run the tests:

$ py.test

Project details


Download files

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

Source Distribution

mergedict-0.2.0.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file mergedict-0.2.0.tar.gz.

File metadata

  • Download URL: mergedict-0.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mergedict-0.2.0.tar.gz
Algorithm Hash digest
SHA256 67c972c30087072f02cbf5052ee6e6cd87eba26c193bc698b71b58b32b141369
MD5 0a52ba99f7e56f5c88670ecfe422c5f7
BLAKE2b-256 0e19ccc2d886d9cbdc0757fd57167bed592c72763e779756e25eef2c1d7a2b0f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page