Merging two dictionaries
Douglas Garstang
doug.garstang at gmail.com
Mon Aug 2 12:42:09 EDT 2010
On Mon, Aug 2, 2010 at 12:47 AM, Paul Rubin <no.email at nospam.invalid> wrote:
> Douglas Garstang <doug.garstang at gmail.com> writes:
>> default = {...
>> 'data_sources': { ...
>> cluster = {...
>> 'data_source': { ...
>
> Did you want both of those to say the same thing instead of one
> of them being 'data_source' and the other 'data_sources' ?
>
> If yes, then the following works for me:
>
> def merge(cluster, default):
> # destructively merge default into cluster
> for k,v in cluster.iteritems():
> if k in default and type(v)==dict:
> assert type(default(k))==dict
> merge(v,default[k])
> for k,v in default.iteritems():
> if k not in cluster:
> cluster[k] = v
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Hmmm, using that gives me:
Traceback (most recent call last):
File "./test4.py", line 48, in ?
merge(cluster, default)
File "./test4.py", line 42, in merge
assert type(default(k))==dict
TypeError: 'dict' object is not callable
where line 42 is 'assert type(default(k))==dict', and the inputs are:
default = {
'cluster': {
'platform': {
'elements': {
'data_sources': {
'elements': {
'db_min_pool_size': 10
},
},
},
},
}
}
cluster = {
'cluster': {
'name': 'Customer 1',
'description': 'Customer Production',
'environment': 'production',
'platform': {
'elements': {
'data_source': {
'elements': {
'username': 'username',
'password': 'password'
},
},
},
},
}
}
and it's called with:
merge(cluster, default)
Doug.
More information about the Python-list
mailing list