Add ASCII hist post and ascii_hist.py script.
authorW. Trevor King <wking@drexel.edu>
Wed, 24 Nov 2010 13:34:35 +0000 (08:34 -0500)
committerW. Trevor King <wking@drexel.edu>
Wed, 24 Nov 2010 13:34:35 +0000 (08:34 -0500)
posts/ASCII_hist.mdwn [new file with mode: 0644]
posts/ASCII_hist/ascii_hist.py [new file with mode: 0755]

diff --git a/posts/ASCII_hist.mdwn b/posts/ASCII_hist.mdwn
new file mode 100644 (file)
index 0000000..0c6d9c2
--- /dev/null
@@ -0,0 +1,7 @@
+[[ascii_hist.py]] is a very simple script to count the frequency of
+each byte in a file.  This is useful if you're playling around with
+crypto.
+
+[[!tag tags/fun]]
+[[!tag tags/programming]]
+[[!tag tags/python]]
diff --git a/posts/ASCII_hist/ascii_hist.py b/posts/ASCII_hist/ascii_hist.py
new file mode 100755 (executable)
index 0000000..57ffaa0
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2010, William Trevor King <wking@drexel.edu>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""Calculate ASCII character frequencies.
+"""
+
+import sys
+
+
+def read(stream):
+    hist = [0]*256
+    while True:
+        data = stream.read(1024)
+        if len(data) == 0:
+            break
+        for char in data:
+            hist[ord(char)] += 1
+    return hist
+
+def write(hist, stream=sys.stdout):
+    for char,count in enumerate(hist):
+        if count != 0:
+            print '%02x (%s)\t%d' % (char, chr(char), count)
+
+
+if __name__ == '__main__':
+    try:
+        filename = sys.argv[1]
+    except IndexError:
+        filename = None
+
+    if filename == None:
+        hist = read(sys.stdin)
+    else:
+        with open(filename, 'rb') as f:
+            hist = read(f)
+
+    write(hist)