fork download
  1. from collections import ChainMap
  2.  
  3. class DeepChainMap(ChainMap):
  4. def __getitem__(self, key):
  5. values = (mapping[key] for mapping in self.maps if key in mapping)
  6. try:
  7. first = next(values)
  8. except StopIteration:
  9. return self.__missing__(key)
  10. if isinstance(first, dict):
  11. return self.__class__(first, *values)
  12. return first
  13.  
  14. def __repr__(self):
  15. return repr(dict(self))
  16.  
  17. a = {'123': {'player': 1,
  18. 'opponent': 2},
  19. '18': {'player': 10,
  20. 'opponent': 12}
  21. }
  22.  
  23.  
  24. b = {'123': {'winner': 1},
  25. '180': {'winner': 2}
  26. }
  27.  
  28. print(DeepChainMap(a, b))
Success #stdin #stdout 0.04s 9648KB
stdin
Standard input is empty
stdout
{'123': {'winner': 1, 'player': 1, 'opponent': 2}, '180': {'winner': 2}, '18': {'player': 10, 'opponent': 12}}