From 7184e633c9d40b9b666dba5d26be3c7f009d46ed Mon Sep 17 00:00:00 2001 From: Tyson Clugg Date: Wed, 16 Dec 2015 17:02:01 +1100 Subject: [PATCH] pylint cleanups on dict_merge. --- dddp/views.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/dddp/views.py b/dddp/views.py index bdc7295..0df5cf5 100644 --- a/dddp/views.py +++ b/dddp/views.py @@ -16,18 +16,22 @@ import pybars # from https://www.xormedia.com/recursively-merge-dictionaries-in-python/ -def dict_merge(a, b): - '''recursively merges dict's. not just simple a['key'] = b['key'], if - both a and bhave a key who's value is a dict then dict_merge is called - on both values and the result stored in the returned dictionary.''' - if not isinstance(b, dict): - return b - result = deepcopy(a) - for k, v in b.iteritems(): - if k in result and isinstance(result[k], dict): - result[k] = dict_merge(result[k], v) +def dict_merge(lft, rgt): + """ + Recursive dict merge. + + Recursively merges dict's. not just simple lft['key'] = rgt['key'], if + both lft and rgt have a key who's value is a dict then dict_merge is + called on both values and the result stored in the returned dictionary. + """ + if not isinstance(rgt, dict): + return rgt + result = deepcopy(lft) + for key, val in rgt.iteritems(): + if key in result and isinstance(result[key], dict): + result[key] = dict_merge(result[key], val) else: - result[k] = deepcopy(v) + result[key] = deepcopy(val) return result