Skip to content

Instantly share code, notes, and snippets.

@n-st
Last active August 29, 2015 14:01
Show Gist options
  • Save n-st/ea4288e20d143031983d to your computer and use it in GitHub Desktop.
Save n-st/ea4288e20d143031983d to your computer and use it in GitHub Desktop.
"interval cat": print every n-th character received on stdin
#!/usr/bin/env python
# intcat = interval cat:
# Prints a human-readable character for every n-th character received on stdin.
# Throughput depends heavily on n (faster for larger n).
n = 1000000
import sys, os
try:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # disable stdout buffering
# string.printable, minus the newline characters
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ '
while True:
s = sys.stdin.read(n)
if s == '': # encountered EOF
print
sys.exit(0)
ch = s[0]
if not ch in printable:
ch = printable[ord(ch)%len(printable)]
sys.stdout.write(ch)
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment