Skip to content

Instantly share code, notes, and snippets.

@peterjpxie
Created April 10, 2022 08:39
Show Gist options
  • Save peterjpxie/dfa996599c7a2def6ddcdccca25fab75 to your computer and use it in GitHub Desktop.
Save peterjpxie/dfa996599c7a2def6ddcdccca25fab75 to your computer and use it in GitHub Desktop.
diff_simple_dict.py
def diff_simple_dict(expected, actual, ignore=[], output_file=None):
"""Compare simple dict generated by ini_to_dict
"""
diff_list = []
for key in expected:
if key not in ignore:
# missing in actual
if key not in actual:
diff_list.append("- %s = %s" % (key, expected[key]))
# diff
elif expected[key] != actual[key]:
diff_list.append("- %s = %s" % (key, expected[key]))
diff_list.append("+ %s = %s" % (key, actual[key]))
# more in actual (missing in expected)
for key in actual:
if key not in ignore:
if key not in expected:
diff_list.append("+ %s = %s" % (key, actual[key]))
diff_list.sort()
diff = "\n".join(diff_list)
if output_file and diff != "":
with open(output_file, "w") as f:
f.write(diff)
return diff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment