同步操作将从 xzhangcqjtu/OnlinePythonTutor 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# m a t r i x . p y## Support for 2d matrix that renders to an HTML table## Chris Meyers. 09/25/2013#dftTableAttr = 'cellspacing="0" cellpadding="10"'class Matrix :def __init__ (self, nrows=1, ncols=1, data=None,dftFormat="", dftStyle="", title="",tableAttr=dftTableAttr, tableHeaders=None,Expand=True) :self.nrows = nrowsself.ncols = ncolsself.values = {}self.expanded = Expandif Expand :# get attributes only on the main Matrixself.dftFormat = dftFormatself.dftStyle = dftStyleself.title = titleself.tableAttr = tableAttrself.tableHeaders = tableHeadersself.format = Matrix(nrows, ncols, Expand=False)self.style = Matrix(nrows, ncols, Expand=False)if data :if type(data) == type({}) : data=dictToLol(data)if type(data) == type([]) :self.populate(data)def __getitem__(self, coords) :row, col = coordsreturn self.values.get((row,col))def __setitem__(self, coords, value) :row, col = coordsself.values[(row,col)] = valueself.nrows = max(self.nrows,row+1)self.ncols = max(self.ncols,col+1)if self.expanded :self.format.nrows = self.nrowsself.format.ncols = self.ncolsself.style.nrows = self.nrowsself.style.ncols = self.ncolsreturn value#===========================================def setrowVals(self, row, values) :"set each column to a seperate value"col = 0for col in range(len(values)) :self.__setitem__((row,col),values[col])col += 1def setrowVal(self, row, value) :"set all columns to the same value"col = 0while col < self.ncols :self.__setitem__((row,col),value)col += 1def getrow (self, row) :vals = []for c in range(self.ncols) :vals.append(self.__getitem__( (row,c) ))return vals#===========================================def setcolVals(self, col, values) :"set each row to a seperate value"row = 0for row in range(len(values)) :self.__setitem__((row,col),values[row])row += 1def setcolVal(self, col, value) :"set all rowumns to the same value"row = 0while row < self.nrows :self.__setitem__((row,col),value)row += 1def getcol (self, col) :vals = []for r in range(self.nrows) :vals.append(self.__getitem__( (r,col) ))return vals#===========================================def populate(self, lists) :"Fill self from a list of lists"nRows = len(lists)nCols = max([len(l) for l in lists])for row in range(len(lists)) :vals = lists[row]if type(vals) != list : vals = [vals] # make sing colself.setrowVals(row, vals)def renderHtml(self,wrap=None) :lins = ["","<table %s>" % self.tableAttr]if self.title : lins[0] = "<div>%s</div>" % self.titleheaders = self.tableHeadersif headers :lins.append("<tr><th>"+"</th><th>".join(map(str,headers))+"</th></tr>")for row in range(self.nrows) :rowLin = [" <tr>"]vals = self.getrow(row)if self.format : formats = self.format.getrow(row)else : formats = ['']*self.ncolsif self.style : styles = self.style.getrow(row)else : styles = ['']*self.ncolsfor c in range(self.ncols) :val = vals[c]; style=styles[c]; format=formats[c]if val == None : val = ""if not format : format = self.dftFormatif format :if type(format)==type("") : val = format % valelse : val = format(val)if not style : style = self.dftStyleif style : cell = '<td style="%s">%s</td>' % (style,val)else : cell = '<td>%s</td>' % valif wrap and c>0 and c%wrap==0 : cell="</tr><tr>"+cellrowLin.append(cell)rowLin.append("</tr>")lins.append("".join(rowLin))lins.append("</table>")return "\n".join(lins)def __str__ (self) :return "Matrix-%dx%d" % (self.nrows,self.ncols)#===========================================typeSeq = (type([]), type((1,2)))def dictToLol(dic) :"Convert dict to a list of lists"keys = dic.keys(); keys.sort()lists = []for key in keys :val = dic[key]if type(val) not in typeSeq : val = [val]lists.append([key]+list(val))return lists
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。