Skip to content

Instantly share code, notes, and snippets.

@Widdershin
Created October 16, 2014 11:22
Show Gist options
  • Save Widdershin/3281ba1353914cad1e51 to your computer and use it in GitHub Desktop.
Save Widdershin/3281ba1353914cad1e51 to your computer and use it in GitHub Desktop.
Lazy Stream Comparison in Python
#!/usr/bin/env python
"""
Compare files lazily.
Usage:
./stream_comparison.py <file1> <file2>
"""
import sys
CHUNK_SIZE = 1024
def streams_match_and_healthy(a, b):
try:
return all(a_chunk == b_chunk for a_chunk, b_chunk in
zip(chunks_from_file(a), chunks_from_file(b)))
except IOError:
return false
def chunks_from_file(file):
def read_chunk():
return file.read(CHUNK_SIZE)
for chunk in iter(read_chunk, ''):
yield chunk
if __name__ == '__main__':
file1, file2 = sys.argv[1], sys.argv[2]
with open(file1) as a, open(file2) as b:
print(streams_match_and_healthy(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment