Skip to content

Instantly share code, notes, and snippets.

@timmc
Created December 21, 2011 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc/1507712 to your computer and use it in GitHub Desktop.
Save timmc/1507712 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Read from stdin and print chunks with checksums."""
from __future__ import print_function
import sys
import md5
chunk_size = 40
def part_str(s, l):
"""Partition string s into chunks of size l, potentially ragged at end."""
ret = []
while len(s) > 0:
ret.append(s[:l])
s = s[l:]
return ret
def main():
print("Delimiters [ and ], newline ...")
for line in sys.stdin.readlines():
line = line.rstrip("\r\n")
for chunk in part_str(line, chunk_size):
checksum = md5.md5(chunk).hexdigest()
fixchunk = ("[" + chunk + "]").ljust(45, " ")
print("%s %s" % (fixchunk, checksum))
print("...")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment