|
| 1 | +# m a t r i x . p y |
| 2 | +# |
| 3 | +# Support for 2d matrix that renders to an HTML table |
| 4 | +# |
| 5 | +# Chris Meyers. 09/25/2013 |
| 6 | +# |
| 7 | + |
| 8 | +dftTableAttr = 'cellspacing="0" cellpadding="10"' |
| 9 | + |
| 10 | +class Matrix : |
| 11 | + def __init__ (self, nrows=1, ncols=1, data=None, |
| 12 | + dftFormat="", dftStyle="", title="", |
| 13 | + tableAttr=dftTableAttr, tableHeaders=None, |
| 14 | + Expand=True) : |
| 15 | + self.nrows = nrows |
| 16 | + self.ncols = ncols |
| 17 | + self.values = {} |
| 18 | + self.expanded = Expand |
| 19 | + if Expand : |
| 20 | + # get attributes only on the main Matrix |
| 21 | + self.dftFormat = dftFormat |
| 22 | + self.dftStyle = dftStyle |
| 23 | + self.title = title |
| 24 | + self.tableAttr = tableAttr |
| 25 | + self.tableHeaders = tableHeaders |
| 26 | + self.format = Matrix(nrows, ncols, Expand=False) |
| 27 | + self.style = Matrix(nrows, ncols, Expand=False) |
| 28 | + if data : |
| 29 | + if type(data) == type({}) : data=dictToLol(data) |
| 30 | + if type(data) == type([]) : |
| 31 | + self.populate(data) |
| 32 | + |
| 33 | + def __getitem__(self, coords) : |
| 34 | + row, col = coords |
| 35 | + return self.values.get((row,col)) |
| 36 | + |
| 37 | + def __setitem__(self, coords, value) : |
| 38 | + row, col = coords |
| 39 | + self.values[(row,col)] = value |
| 40 | + self.nrows = max(self.nrows,row+1) |
| 41 | + self.ncols = max(self.ncols,col+1) |
| 42 | + if self.expanded : |
| 43 | + self.format.nrows = self.nrows |
| 44 | + self.format.ncols = self.ncols |
| 45 | + self.style.nrows = self.nrows |
| 46 | + self.style.ncols = self.ncols |
| 47 | + return value |
| 48 | + |
| 49 | +#=========================================== |
| 50 | + |
| 51 | + def setrowVals(self, row, values) : |
| 52 | + "set each column to a seperate value" |
| 53 | + col = 0 |
| 54 | + for col in range(len(values)) : |
| 55 | + self.__setitem__((row,col),values[col]) |
| 56 | + col += 1 |
| 57 | + |
| 58 | + def setrowVal(self, row, value) : |
| 59 | + "set all columns to the same value" |
| 60 | + col = 0 |
| 61 | + while col < self.ncols : |
| 62 | + self.__setitem__((row,col),value) |
| 63 | + col += 1 |
| 64 | + |
| 65 | + def getrow (self, row) : |
| 66 | + vals = [] |
| 67 | + for c in range(self.ncols) : |
| 68 | + vals.append(self.__getitem__( (row,c) )) |
| 69 | + return vals |
| 70 | + |
| 71 | +#=========================================== |
| 72 | + |
| 73 | + def setcolVals(self, col, values) : |
| 74 | + "set each row to a seperate value" |
| 75 | + row = 0 |
| 76 | + for row in range(len(values)) : |
| 77 | + self.__setitem__((row,col),values[row]) |
| 78 | + row += 1 |
| 79 | + |
| 80 | + def setcolVal(self, col, value) : |
| 81 | + "set all rowumns to the same value" |
| 82 | + row = 0 |
| 83 | + while row < self.nrows : |
| 84 | + self.__setitem__((row,col),value) |
| 85 | + row += 1 |
| 86 | + |
| 87 | + def getcol (self, col) : |
| 88 | + vals = [] |
| 89 | + for r in range(self.nrows) : |
| 90 | + vals.append(self.__getitem__( (r,col) )) |
| 91 | + return vals |
| 92 | + |
| 93 | +#=========================================== |
| 94 | + |
| 95 | + def populate(self, lists) : |
| 96 | + "Fill self from a list of lists" |
| 97 | + nRows = len(lists) |
| 98 | + nCols = max([len(l) for l in lists]) |
| 99 | + for row in range(len(lists)) : |
| 100 | + vals = lists[row] |
| 101 | + if type(vals) != list : vals = [vals] # make sing col |
| 102 | + self.setrowVals(row, vals) |
| 103 | + |
| 104 | + def renderHtml(self,wrap=None) : |
| 105 | + lins = ["","<table %s>" % self.tableAttr] |
| 106 | + if self.title : lins[0] = "<div>%s</div>" % self.title |
| 107 | + headers = self.tableHeaders |
| 108 | + if headers : |
| 109 | + lins.append("<tr><th>"+"</th><th>".join(map(str,headers))+ |
| 110 | + "</th></tr>") |
| 111 | + for row in range(self.nrows) : |
| 112 | + rowLin = [" <tr>"] |
| 113 | + vals = self.getrow(row) |
| 114 | + if self.format : formats = self.format.getrow(row) |
| 115 | + else : formats = ['']*self.ncols |
| 116 | + if self.style : styles = self.style.getrow(row) |
| 117 | + else : styles = ['']*self.ncols |
| 118 | + for c in range(self.ncols) : |
| 119 | + val = vals[c]; style=styles[c]; format=formats[c] |
| 120 | + if val == None : val = "" |
| 121 | + if not format : format = self.dftFormat |
| 122 | + if format : |
| 123 | + if type(format)==type("") : val = format % val |
| 124 | + else : val = format(val) |
| 125 | + if not style : style = self.dftStyle |
| 126 | + if style : cell = '<td style="%s">%s</td>' % (style,val) |
| 127 | + else : cell = '<td>%s</td>' % val |
| 128 | + if wrap and c>0 and c%wrap==0 : cell="</tr><tr>"+cell |
| 129 | + rowLin.append(cell) |
| 130 | + rowLin.append("</tr>") |
| 131 | + lins.append("".join(rowLin)) |
| 132 | + lins.append("</table>") |
| 133 | + return "\n".join(lins) |
| 134 | + |
| 135 | + def __str__ (self) : |
| 136 | + return "Matrix-%dx%d" % (self.nrows,self.ncols) |
| 137 | + |
| 138 | +#=========================================== |
| 139 | + |
| 140 | +typeSeq = (type([]), type((1,2))) |
| 141 | + |
| 142 | +def dictToLol(dic) : |
| 143 | + "Convert dict to a list of lists" |
| 144 | + keys = dic.keys(); keys.sort() |
| 145 | + lists = [] |
| 146 | + for key in keys : |
| 147 | + val = dic[key] |
| 148 | + if type(val) not in typeSeq : val = [val] |
| 149 | + lists.append([key]+list(val)) |
| 150 | + return lists |
| 151 | + |
0 commit comments