NineTail NineTail -4 years ago 203
Python Question

Three way comparison using hash

I need a way to compare three hashes based on the hostname I've extracted. I have a function to compare two but looking to combine it to do all three in one function.

All variables have been captured and stored previously. This is just a comparison check.

I do not want to us isdiff or some other function if it can be avoided.

Need to compare three results.

results1 = {'uspn-hs-r3': ('uspn-hs-r3', 'archive\uspn-hs-r3.cfg', '23d516b3f2f438327b1a19fa63868621a87b8c8b')' etc.....

These values can very, hence the compare. The string is far to long to show it all. However the hostname in this case is uspn-hs-r3 followed by the :, then the directory it was in, followed by the hash.

results2
results3

Two Way Comparison:

for hostname, filename, filehash in results1.values():
if hostname in results2:
_, filename2, filehash2 = results2[hostname]
if filehash != filehash2:
print ("%s has a change (%s, %s)" % (
hostname, filehash, filehash2))
print(filename)
print(filename2)


Failed 3 way:

for hostname, filename, filehash in results1.values():
if hostname in results2 and hostname in results3:
_, filename2, filehash2, filename3, filehash3 = results3[hostname]
if filehash != filehash2 or filehash != filehash3:
print ("%s has a change (%s, %s)" % (
hostname, filehash, filehash2, filehash3))
print(filename)
print(filename2)
print(filename3)

Answer Source

You have error in line 3. result3 only has a tuple of size 3 and you're unpacking it to 5 values.

for hostname, filename, filehash in results1.values():
    if hostname in results2 and hostname in results3:
        _, filename2, filehash2 = results2[hostname]
        _, filename3, filehash3 = results3[hostname]
        if filehash != filehash2 or filehash != filehash3:
            print ("%s has a change (%s, %s, %s)" % (
                hostname, filehash, filehash2, filehash3))
            print(filename)
            print(filename2)
            print(filename3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download