Created
March 23, 2022 09:55
-
-
Save dtantsur/730039a964379f3c107f080045c3f76d to your computer and use it in GitHub Desktop.
Compare two YAMLs recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import yaml | |
def compare(r1, r2, prefix=''): | |
if isinstance(r1, dict): | |
for (k1, v1), (k2, v2) in zip(r1.items(), r2.items()): | |
if k1 != k2: | |
print(prefix, 'key mismatch:', k1, '!=', k2) | |
else: | |
compare(v1, v2, prefix=f'{prefix}.{k1}') | |
elif isinstance(r1, list): | |
for idx, (v1, v2) in enumerate(zip(r1, r2)): | |
compare(v1, v2, prefix=f'{prefix}.{idx}') | |
elif r1 != r2: | |
print(prefix, 'value mismatch:', r1, '!=', r2) | |
with open(sys.argv[1]) as f1: | |
f1 = yaml.safe_load(f1) | |
with open(sys.argv[2]) as f2: | |
f2 = yaml.safe_load(f2) | |
compare(f1, f2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment