#! /usr/bin/env python """simple class to iterate over the lines of a file or standard input $Id: Utility.py,v 1.1 2004/10/10 02:53:09 nghoffma Exp $ """ import sys class inFile: def __init__( self, filename='' ): if filename == '': self.__fo = sys.stdin else: try: self.__fo = open( filename, 'r' ) except IOError: print '\n\n*\n*%30s\n*' % 'Utility.inFile' print filename, "does not exist" return def __getitem__( self, index ): #try to read a line line = self.__fo.readline() #if nothig is left to read if line == "": #close file object self.__fo.close() #raise index error to get out of for loops raise IndexError else: return line def niceTable(dataArray, gutter=2, columnGuide=0): """Writes a table with left-justified columns separated by space characters. Each column is (gutter) spaces wider than the widest element in the column. dataArray is a list of lists : dataArray[line][element] if columnGuide= a character, places strings of that char repeated over each column. Returns a string.""" lineCount = len(dataArray) ###figure out number of columns colCount = 0 for line in dataArray: colCount = max([colCount, len(line)]) ### determine column widths colWidths = [0]*colCount for j in range(colCount): for i in range(lineCount): try: thisLen = len(dataArray[i][j]) except IndexError: dataArray[i].append('') thisLen = 0 colWidths[j] = max([colWidths[j], thisLen + gutter]) fstr = '%%-%is'*colCount % tuple(colWidths) + '\n' outstr = '' if columnGuide: guideList = [] for w in colWidths: guideList.append(columnGuide*(w-gutter)) outstr = outstr + fstr % tuple(guideList) for line in dataArray: outstr = outstr + fstr % tuple(line) return outstr def describe(): """ Utility.py Miscellaneous utility functions. """ print describe.__doc__ if __name__ == '__main__': describe()