toejough / dictmerge

merge python dictionaries
MIT License
0 stars 1 forks source link

dictmerge

merge python dictionaries

which

latest version - 0.7.1

why

Because merging dictionaries in python seems like it should be straightforward, but often it's not.

what

Merge dictionaries according to the following rules:

The default resolver function will:

how

Example:

from dictmerge import merge

# define dict1, dict2...
dict1 = {
  'foo': 'foo-val',
  'bar': { 1: 'one', 2: 'two' },
  'baz': 'baz-val'
}
dict2 = {
  'foo': 'foo-val',
  'bar': { 3: 'three', 4: 'four' },
  'quux': 'quux-val'
}

d = merge(dict1, dict2)

assert d == {
  'foo': 'foo-val',
  'bar': {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
  },
  'baz': 'baz-val',
  'quux': 'quux-val'
}

Example with differing keys:

from dictmerge import merge

# define dict1, dict2...
dict1 = {
  'foo': 'foo-val',
}
dict2 = {
  'foo': 'other-val',
}

d = merge(dict1, dict2)  # throws dictmerge.KeyConflictError

testing

testing is done via py.test, with the tests in test_main.py.

installation

pip install --upgrade git+http://github.com/toejough/dictmerge