Possible Duplicate:
python: Dictionaries of dictionaries merge
I'm keeping a bunch of app settings stored in a dictionary that is saved to a text file as JSON. I'd like to to merge it with a default set of key/values, so that as the app changes in the future, the new ones can be merged in. For example:
defaults = {
'svn': "",
'notify': {
'email': "",
'notifo': { 'username': "", 'secret': ""},
'active': False,
'lastCheck': 0
}
}
local = {
'svn': "/path/to/trunk/",
'notify': {
'email': "me@mysite.com",
'notifo': { 'username': "me", 'secret': "1234"},
}
}
Notice that 'local' is missing ['notify']['active'] and ['notify']['lastCheck']. I want to be able to merge the two dictionaries into one that looks like this:
local = {
'svn': "/path/to/trunk/",
'notify': {
'email': "me@mysite.com",
'notifo': { 'username': "me", 'secret': "1234"},
'active': False,
'lastCheck': 0
}
}
I've been looking everywhere but only see examples of people flattening the dictionaries, merging the first level or doing funny things with defaultdict. Is it possible to recursively merge nested dictionaries.