schettino72 / mergedict

mergedict - A Python `dict` with a merge() method
https://pypi.python.org/pypi/mergedict
Other
14 stars 1 forks source link

does not merge nested arrays #2

Open saurabh-hirani opened 8 years ago

saurabh-hirani commented 8 years ago
import json
from mergedict import ConfigDict

ds1 = ConfigDict({
  'a': 1,
  'b': [1,2,3],
  'c': {
    'd': [3,4]
  }
})
ds1.merge({
  'a': 2,
  'b': [4,5,6],
  'c': {
    'd': [1,2]
  }
})
print(json.dumps(ds1, indent=2))

gives the output

{
  "a": 2,
  "c": {
    "d": [
      1,
      2
    ]
  },
  "b": [
    1,
    2,
    3,
    4,
    5,
    6
  ]
}

Outer array in merged. But the inner one (pointed by 'd') is overwritten - is this the expected behavior?

hartwork commented 4 years ago

@schettino72 is this a bug or by design?

schettino72 commented 4 years ago

It is by design. Like python module copy does not do deep-copy by default. But of course a "deep-merge" could be implemented, added...

I wrote mergedict expecting people to create their own Merge definitions. ConfigDict was more like an example. Anyway if someone already implemented a "deep merge", I guess not harm in also including it the package.