#NEWLINE# def mergeNestedDictionaries( dict1 , dict2 , copy = False , deepcopy = False ) : #NEWLINE# #INDENT# 'Recursively merge the values of dict2 into dict1.\n\n    This little function is very handy for selectively overriding settings in a\n    settings dictionary that has a nested structure.\n    ' #NEWLINE# if copy : #NEWLINE# #INDENT# dict1 = copyModule . copy ( dict1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if deepcopy : #NEWLINE# #INDENT# dict1 = copyModule . deepcopy ( dict1 ) #NEWLINE# #UNINDENT# #UNINDENT# for ( key , val ) in dict2 . iteritems ( ) : #NEWLINE# #INDENT# if ( ( key in dict1 ) and isinstance ( val , dict ) and isinstance ( dict1 [ key ] , dict ) ) : #NEWLINE# #INDENT# dict1 [ key ] = mergeNestedDictionaries ( dict1 [ key ] , deepcopy ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dict1 [ key ] = val #NEWLINE# #UNINDENT# #UNINDENT# return dict1