Commit ab3a158eb33ad9e2992e6b86eaa2074facf852d0

Authored by selurvedu
1 parent 6c4772c1

Make merge_dicts() recursive

1 1 # -*- coding: utf-8 -
  2 +from copy import deepcopy
2 3 from datetime import timedelta
3 4 from dateutil.parser import parse
4 5 from dpath.util import new as xpathnew
... ... @@ -246,11 +247,20 @@ def wait_to_date(date_stamp):
246 247 return wait_seconds
247 248
248 249
249   -def merge_dicts(left, right):
250   - new = {}
251   - new.update(left)
252   - new.update(right)
253   - return new
  250 +def merge_dicts(a, b):
  251 + """Merge dicts recursively.
  252 +
  253 + Origin: https://www.xormedia.com/recursively-merge-dictionaries-in-python/
  254 + """
  255 + if not isinstance(b, dict):
  256 + return b
  257 + result = deepcopy(a)
  258 + for k, v in b.iteritems():
  259 + if k in result and isinstance(result[k], dict):
  260 + result[k] = merge_dicts(result[k], v)
  261 + else:
  262 + result[k] = deepcopy(v)
  263 + return munchify(result)
254 264
255 265
256 266 def create_data_dict(path_to_value=None, value=None):
... ...
Please register or login to post a comment