天天看點

wxpython grid重新整理資料_我可以在wxPython的wx.grid.Grid中加速優化GridCellAttr的使用嗎?...

設定單元格屬性會将新的GridCellAttr添加到GridCellAttrProvider的清單中。

随着清單的增長,查找單元格的特定屬性(通過周遊清單并比較坐标)變得越來越慢。

您可以嘗試通過實作自己的PyGridTableBase.SetAttr和GetAttr(例如使用dict)加快速度:

編輯:更新了代碼以允許覆寫屬性并模拟預設實作屬性所有權。

class MyTable(wx.grid.PyGridTableBase):

atts = {}

def Hash(self,row,col):

#FIXME: assumes a constant number of rows and rows > cols

return col + row * self.GetNumberRows()

def SetAttr(self,attr,row,col):

HASH = self.Hash(row, col)

if HASH in self.atts:

# decrement usage count of existing attr

self.atts[HASH].DecRef()

#assign new attribute

self.atts[HASH] = attr

def GetAttr(self,row,col,kind):

HASH = self.Hash(row, col)

if HASH in self.atts:

attr = self.atts[HASH]

attr.IncRef() # increment reference count

return attr

return None要允許設定整個行和列,您還必須實作:

def SetRowAttr(self,attr,row):

for col in range(self.GetNumberCols()):

attr.IncRef() # increment reference count for SetAttr

self.SetAttr(attr,row,col)

attr.DecRef() # attr passed to SetRowAttr no longer needed

def SetColAttr(self,attr,col):

for row in range(self.GetNumberRows()):

attr.IncRef()

self.SetAttr(attr,row,col)

attr.DecRef()注意:将GridCellAttr傳遞給Set*Attr()時,預設實作将擷取該屬性的所有權。

要重新使用相同的屬性(例如存儲在類變量中),您必須使用Clone()或增加其使用計數(IncRef())

在将其傳遞給Set*Attr()方法之前(克隆可能會增加記憶體消耗)。

上面的示例沒有正确删除屬性:SetAttr()可以檢查None attr并減少指定坐标處的ref計數,然後從dict中删除該條目。

SetCol/RowAttr()可以通過為行和列添加dicts來優化,類似于SetAttr()。然後,GetAttr()可以檢查行中的現有條目和col dict,并使用單元格dict中的屬性合并/覆寫屬性(這是預設實作使用的原則)。為了正确清理字典,請在.clear()之前的每個條目上調用DecRef。

或者,您可以從wx.grid.GridCellAttrProvider派生并将其附加到PyGridTableBase.SetAttrProvider()。但是,這會阻止直接通路表。