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

Source Code for Module PyFoam.Basics.TableData

 1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/TableData.py 7786 2012-01-24T22:50:47.011292Z bgschaid  $  
 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 apply(self,func):
36 """Return the table with a function applied to it 37 @param func: the function to apply to each element""" 38 tab=TableData(self.__rowLabels,self.__columnLabels) 39 for r in self.__rowLabels: 40 for c in self.__columnLabels: 41 tab[(r,c)]=func(self[(r,c)]) 42 return tab
43
44 - def __getitem__(self,labels):
45 """@param labels: tuple of the form (row,col)""" 46 row,col=self.getIndex(labels) 47 48 return self.__data[row][col]
49
50 - def __setitem__(self,labels,val):
51 """@param labels: tuple of the form (row,col)""" 52 row,col=self.getIndex(labels) 53 54 self.__data[row][col]=val
55
56 - def __str__(self):
57 """The table as a restructured text object""" 58 59 tab=ReSTTable() 60 tab[0]=[""]+self.__columnLabels 61 tab.addLine(head=True) 62 for i,l in enumerate(self.__data): 63 tab[i+1]=[self.__rowLabels[i]]+l 64 65 return str(tab)
66
67 - def min(self):
68 """Return the minimum of the data in the table""" 69 return min(map(min,self.__data))
70
71 - def max(self):
72 """Return the maximum of the data in the table""" 73 return max(map(max,self.__data))
74
75 - def columns(self):
76 """Iterate over the column names""" 77 for c in self.__columnLabels: 78 yield c
79
80 - def rows(self):
81 """Iterate over the row names""" 82 for c in self.__rowLabels: 83 yield c
84