1
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
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
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
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
45 """@param labels: tuple of the form (row,col)"""
46 row,col=self.getIndex(labels)
47
48 return self.__data[row][col]
49
51 """@param labels: tuple of the form (row,col)"""
52 row,col=self.getIndex(labels)
53
54 self.__data[row][col]=val
55
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
68 """Return the minimum of the data in the table"""
69 return min(map(min,self.__data))
70
72 """Return the maximum of the data in the table"""
73 return max(map(max,self.__data))
74
76 """Iterate over the column names"""
77 for c in self.__columnLabels:
78 yield c
79
81 """Iterate over the row names"""
82 for c in self.__rowLabels:
83 yield c
84