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 operator

  Python provides an if statement with the same semantics as languages like C++ and Java, although with its own sparse syntax: if expression1: suite1 elif expression2: suite2 else: suite3 The first thing that stands out to programmers used to C++ or Java is that there are no parentheses and no braces. The other thing to notice is the colon: This is part of the syntax and is easy to forget when starting out. Colons are used with else, elif, and in many other places to indicate that a block of code (a suite in Python-speak) is to follow. As we would expect, there can be any number. Group Operators Description Comparison <, <=, ==, !=, >=, > The <> operator is also permitted as a synonym for != but is deprecated Identity is, is not These are used to determine if two object references refer to the same underlying object Membership in, not in These are used on lists, dictionaries, and strings Logical not, and,or Both and and or short-circuit; the bit-wise equivalents a...