Last active
August 29, 2015 14:01
-
-
Save n-st/ea4288e20d143031983d to your computer and use it in GitHub Desktop.
"interval cat": print every n-th character received on stdin
This file contains hidden or 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 | |
# 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 | |
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