Skip to main content

comparision

Example Input

file1.txt

shard1 host1:6379 host2:6379 host3:6379 shard2 host4:6379 host5:6379 host6:6379

file2.txt

shard1 host1:6379 host2:6379 host3:6379 shard2 host4:6379 host5:9999 host6:6379

Python script to compare

def parse_file(filename): """Read shard -> hosts mapping from file.""" shards = {} with open(filename, "r") as f: for line in f: parts = line.strip().split() if not parts: continue shard = parts[0] hosts = parts[1:] shards[shard] = set(hosts) # use set for easy comparison return shards def compare_shards(file1, file2): shards1 = parse_file(file1) shards2 = parse_file(file2) all_shards = set(shards1.keys()) | set(shards2.keys()) for shard in sorted(all_shards): hosts1 = shards1.get(shard, set()) hosts2 = shards2.get(shard, set()) if hosts1 == hosts2: print(f"✅ {shard} is consistent in both files") else: print(f"❌ {shard} mismatch") if hosts1 - hosts2: print(f" Present only in {file1}: {hosts1 - hosts2}") if hosts2 - hosts1: print(f" Present only in {file2}: {hosts2 - hosts1}") # Example usage compare_shards("file1.txt", "file2.txt")

Output for example

✅ shard1 is consistent in both files ❌ shard2 mismatch Present only in file1.txt: {'host5:6379'} Present only in file2.txt: {'host5:9999'}
 
 

 Enhanced program


 

 
 
 
def parse_file(filename):
"""Read shard -> hosts mapping from file."""
shards = {}
with open(filename, "r") as f:
for line in f:
parts = line.strip().split()
if not parts:
continue
shard = parts[0] # first word = shard name
hosts = parts[1:] # rest = host list
shards[shard] = set(hosts) # store as set
return shards


def compare_shards(file1, file2):
shards1 = parse_file(file1)
shards2 = parse_file(file2)

all_shards = set(shards1.keys()) | set(shards2.keys()) # union of all shard names

for shard in sorted(all_shards):
hosts1 = shards1.get(shard, set())
hosts2 = shards2.get(shard, set())

if hosts1 == hosts2:
print(f"[OK] {shard} is consistent in both files")
else:
print(f"[MISMATCH] {shard}")

if shard not in shards1:
print(f" {shard} is missing in {file1}")
if shard not in shards2:
print(f" {shard} is missing in {file2}")

# Compare hosts in detail
extra_in_file1 = hosts1 - hosts2
extra_in_file2 = hosts2 - hosts1

if extra_in_file1:
print(f" Present only in {file1}:")
for h in sorted(extra_in_file1):
print(f" {h}")

if extra_in_file2:
print(f" Present only in {file2}:")
for h in sorted(extra_in_file2):
print(f" {h}")


# Example usage
if __name__ == "__main__":
compare_shards("file1.txt", "file2.txt")
 

 Output

 
 
python3 comp.py file1 file2

[OK] shard1 is consistent in both files
[MISMATCH] shard2
Present only in file1.txt:
host5:6379
Present only in file2.txt: 

Comments

Popular posts from this blog

python basics: effective python ref

pantry = [ ('avocados', 1.25), ('bananas', 2.5), ('cherries', 15), ] for i, (item, count) in enumerate(pantry): print('#%d: %-10s = %.2f' % (i, item, count)) What It Prints (Correctly) #0: avocados = 1.25 #1: bananas = 2.50 #2: cherries = 15.00 #%d → prints the index number (0, 1, 2). %-10s → left-aligns the string (item name) in a field 10 characters wide. Example: "bananas " (adds spaces to align neatly). %.2f → prints the number with 2 decimal places. So the = sign belongs in the format string itself: '%-10s = %.2f' ✅ If you run the exact code above, you will not see separate lines with just the numbers. Instead, you’ll see neatly aligned item + count pairs. 👉 Do you want me to also show you the f-string version (more modern & cleaner than % formatting)? yes Perfect 👍 f-strings make this much cleaner and easier to read. Here’s your same example rewritten with f-strings: pantry = [ ('av...