#!/usr/bin/env python # # Copyright (C) 2010, William Trevor King # # 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 . """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)