Created
October 16, 2014 11:22
-
-
Save Widdershin/3281ba1353914cad1e51 to your computer and use it in GitHub Desktop.
Lazy Stream Comparison in Python
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
#!/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