Package PyFoam :: Package Basics :: Module DataStatistics
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.DataStatistics

 1  #  ICE Revision: $Id$  
 2  """A simple object for table data where data is accessed with a tuple 
 3  (rowLabel,colLabel)""" 
 4   
 5  from PyFoam.Basics.RestructuredTextHelper import ReSTTable 
 6   
7 -class TableData(object):
8 """A simple table. Current limitiation is that column and row 9 labels have to be known at creation time""" 10
11 - def __init__(self,rowLabels,columnLabels):
12 """ 13 @param rowLables: the names of the rows 14 @param columnLabels: the names of the columns 15 """ 16 self.__rowLabels=rowLabels 17 self.__columnLabels=columnLabels 18 19 self.__data=[[None]*len(self.__columnLabels) for i in range(len(self.__rowLabels))]
20
21 - def getIndex(self,labels):
22 """Return the numeric indizes for these labels""" 23 rowName,colName=labels 24 25 try: 26 row=self.__rowLabels.index(rowName) 27 col=self.__columnLabels.index(colName) 28 except ValueError: 29 raise IndexError("Labels",labels,"not in valid labels.", 30 "Rows:",self.__rowLabels, 31 "Col:",self.__columnLabels) 32 33 return (row,col)
34
35 - def __getitem__(self,labels):
36 """@param labels: tuple of the form (row,col)""" 37 row,col=self.getIndex(labels) 38 39 return self.__data[row][col]
40
41 - def __setitem__(self,labels,val):
42 """@param labels: tuple of the form (row,col)""" 43 row,col=self.getIndex(labels) 44 45 self.__data[row][col]=val
46
47 - def __str__(self):
48 """The table as a restructured text object""" 49 50 tab=ReSTTable() 51 tab[0]=[""]+self.__columnLabels 52 tab.addLine(head=True) 53 for i,l in enumerate(self.__data): 54 tab[i+1]=[self.__rowLabels[i]]+l 55 56 return str(tab)
57