Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. d1 = {"a1": {"b11": "0", "b12": "1"}, "a2": {"b21": "2", "b22": "3"}, "a3": {"b32": "3"}}
  2. d2 = {"a1": {"b11": "4", "b12": "1"}, "a2": {"b21": "2", "b22": "3"}, "a3": {"b32": "3"}, "a4": {"b41": "5"}}
  3.  
  4. def compare_dictionaries(dict_1, dict_2, dict_1_name, dict_2_name, path=""):
  5. err = ''
  6. key_err = ''
  7. value_err = ''
  8. old_path = path
  9. for k in dict_1.keys():
  10. path = old_path + "[%s]" % k
  11. if not dict_2.has_key(k):
  12. key_err += "Key %s%s not in %sn" % (dict_2_name, path, dict_2_name)
  13. else:
  14. if isinstance(dict_1[k], dict) and isinstance(dict_2[k], dict):
  15. err += compare_dictionaries(dict_1[k],dict_2[k],'d1','d2', path)
  16. else:
  17. if dict_1[k] != dict_2[k]:
  18. value_err += "Value of %s%s (%s) not same as %s%s (%s)n"
  19. % (dict_1_name, path, dict_1[k], dict_2_name, path, dict_2[k])
  20.  
  21. for k in dict_2.keys():
  22. path = old_path + "[%s]" % k
  23. if not dict_1.has_key(k):
  24. key_err += "Key %s%s not in %sn" % (dict_2_name, path, dict_1_name)
  25.  
  26. return key_err + value_err + err
  27.  
  28.  
  29. print compare_dictionaries(d1, d2, 'dict1', 'dict2')
  30.  
  31. dict2[a4] not in dict1
  32. Value of dict1[a1][b11] (0) not same as dict2[a1][b11] (4)
  33. Value of dict1[a2][b22] (3) not same as dict2[a2][b22] (6)
  34.  
  35. ................................
  36. | d1 | d2 |
  37. --------------------------------
  38. | Changed: |
  39. | [a1][b11]=0 | [a1][b11]=4 |
  40. | [a2][b22]=3 | [a2][b22]=6 |
  41. | Unique: |
  42. | | [a4] |
  43. ````````````````````````````````
Add Comment
Please, Sign In to add comment