Objectlistview Python: Filtering

1.4k views Asked by At

I am trying to filter an objectlistview based on a text value in a certain column of my data. I can't figure out how to construct the filter properly. I have been refering to:

http://objectlistview.sourceforge.net/python/features.html http://objectlistview.sourceforge.net/python/majorClasses.html

I am able to get Filter.Head(n) to work sucessfully but not ableto get Filter.TextSearch(objectListView, columns=(), text="") to work because I don't understand what the parameter types are suppost to be.

I have written the below code:

def OnFilterMeter(self,event):
    list = self.exception_panel.col_list   ## The lenght of my column list
    col  = list[len(list)-1]     ## I want to search the last column 
    meter_flt = Filter.TextSearch(self.exception_panel,columns = (col), text = "10") ## Create filter to find string 10 in last column
    self.exception_panel.SetFilter(meter_flt)
    self.exception_panel.populate()

I can't understand why this doesn't work. The program doesn't filter the list when it tries to populate the list view again. At the very least it should show all blanks because it cannot find any items that are equal to "10". Please help. Thank you

Below is my code :

class Display_Manager(wx.Frame):
def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, -1, title)          
    self.exception_panel =  Exception_Display(self,0)
    # self.exception_panel2 = Exception_Display(self,1)
    ## Setup FileMenu Bar Setup
    filemenu= wx.Menu()                                                                             ## Create Menu Bar
    filemenu.Append(ID_LOG,"Login","Login as user with password.")                                  ## Clicking shoudl load a single dailyi report which user selects
    filemenu.Append(ID_LOAD,"Load Report","Load Daliy Exception Report")                            ## Clicking shoudl load a single dailyi report which user selects
    filemenu.Append(ID_LOADS,"Load All","Load All Daliy Exception Reports")                         ## Clicking should load all daily reports in a directory which user selects
    filemenu.Append(ID_REFRESH,"Refresh DB","Refresh Database. Overwrites any un saved data.")      ## Clicking should refreseh the database and overwrite any unsaved changes
    filemenu.Append(ID_SAVE,"Save DB","Saves and commits any changes to database.")                 ## Clicking will save and commit any changes or new data to the database
    filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")                                       ## exit the program
    ## Setup Edit Menu Bar
    editmenu = wx.Menu()
    editmenu.Append(ID_ADD,"Add","Add Exception")
    editmenu.Append(ID_DELETE,"Delete","Delete Exception")
    editmenu.Append(ID_UNDO,"Stepback DB","Go back to a previous version of the database")
    editmenu.Append(ID_REDO,"Stepfoward DB","Go foward to a previous version of the database")
    ## Setup Report Menu
    reportmenu = wx.Menu()
    reportmenu.Append(ID_REPORT,"Exception Report","Generate an Excel Report of selected exceptions.")
    reportmenu.Append(ID_REPORT2,"Meter Report","Generate an Excel Report of selected exceptions.")
    reportmenu.Append(ID_REPORT3,"Transformer Report","Generate an Excel Report of selected exceptions.")
    ## Setup Bucket Menu
    bucketmenu = wx.Menu()
    bucketmenu.Append(ID_METERPROB,"Meter Problem","Place the meter in the meter problem bucket.")
    bucketmenu.Append(ID_LOWVOLT,"Investigate Low Voltage","Place the meter in the investigate bucket.")
    bucketmenu.Append(ID_TURNS,"Turns Ratio","Place the meter in the turns ratio bucket.")
    ## Setup Configure Menu Menu
    configmenu = wx.Menu()
    configmenu.Append(ID_FILTER,"Set Filters","Filter by district and company.")
    configmenu.Append(ID_SAVEFIL,"Save Filters","Save Filters for Later use.")
    configmenu.Append(ID_LOADFIL,"Load Filters","Load Filters for Later use.")

    ## Add file menu bar
    menuBar = wx.MenuBar()
    menuBar.Append(filemenu,"&File")
    menuBar.Append(editmenu, "&Edit")
    menuBar.Append(reportmenu, "&Reports")
    menuBar.Append(configmenu, "&Config")
    menuBar.Append(bucketmenu, "&Bucket")
    self.SetMenuBar(menuBar)

    ## Create Toolbar
    tb = self.CreateToolBar( wx.TB_HORIZONTAL | wx.NO_BORDER | 
    wx.TB_FLAT | wx.TB_TEXT)
    tb.AddSimpleTool(10, wx.Bitmap('images/database.png'), 'Save Database')
    tb.AddSimpleTool(20, wx.Bitmap('images/excel.png'), 'Get Quick Excel Report')
    tb.AddSimpleTool(70, wx.Bitmap('images/analyze.png'), 'Analyze Selected Exceptions')
    tb.AddSimpleTool(71, wx.Bitmap('images/refresh.png'), 'Refresh Selected Relationships')
    tb.AddSeparator()
    tb.AddSimpleTool(30, wx.Bitmap('images/today.png'), 'Filter only the latest data.')
    tb.AddSimpleTool(40, wx.Bitmap('images/all_time.png'), 'Filter All Data from all time')
    tb.AddSimpleTool(41, wx.Bitmap('images/date_range.png'), 'Select a date range to view data over')
    tb.AddSeparator()
    tb.AddSimpleTool(50, wx.Bitmap('images/AMI_Meter.png'), 'Bucket as meter problem')
    tb.AddSimpleTool(60, wx.Bitmap('images/violation.png'), 'Bucket to District')
    tb.AddSimpleTool(61, wx.Bitmap('images/turns.png'), 'Bucket Turns Ratio')
    tb.AddSimpleTool(62, wx.Bitmap('images/plan.png'), 'Bucket for further review for planning engineer')
    tb.AddSeparator()
    tb.AddSimpleTool(63, wx.Bitmap('images/cyme.png'), 'Load CYME model for specific meter')
    tb.AddSimpleTool(64, wx.Bitmap('images/relate_dot.png'), 'Cluster & Relate Meter Exceptions')
    tb.AddSeparator()
    tb.AddSimpleTool(65, wx.Bitmap('images/bucket_m.png'), 'Filter Meter Bucket')
    tb.AddSimpleTool(66, wx.Bitmap('images/bucket_v.png'), 'Filter Violation Bucket')
    tb.AddSimpleTool(67, wx.Bitmap('images/bucket_t.png'), 'Filter Turns Ratio Bucket')
    tb.AddSimpleTool(68, wx.Bitmap('images/bucket_p.png'), 'Filter Planning Bucket')
    tb.SetToolBitmapSize((84,84))
    tb.Realize()




    self.Bind(wx.EVT_TOOL,self.OnRefresh,id =71)
    self.Bind(wx.EVT_TOOL,self.OnAnalyze,id =70)
    self.Bind(wx.EVT_TOOL,self.OnSave,id =10)

    self.Bind(wx.EVT_TOOL,self.OnBucketMeter,id =50)
    self.Bind(wx.EVT_TOOL,self.OnVioMeter,id =60)
    self.Bind(wx.EVT_TOOL,self.OnTurnsMeter,id =61)
    self.Bind(wx.EVT_TOOL,self.OnPlanMeter,id =62)

    self.Bind(wx.EVT_TOOL,self.OnFilterMeter,id =65)
    # self.Bind(wx.EVT_TOOL,self.OnFilterMeter,id =66)
    # self.Bind(wx.EVT_TOOL,self.OnFilterMeter,id =67)
    # self.Bind(wx.EVT_TOOL,self.OnFilterMeter,id =68)

    ## Create Sizers
    # self.main_sizer = wx.BoxSizer(wx.VERTICAL)
    # self.top_sizer = wx.BoxSizer(wx.HORIZONTAL)
    # self.bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
    # self.main_sizer.Add(self.top_sizer,0,wx.EXPAND)
    # self.main_sizer.Add(self.bottom_sizer,0,wx.EXPAND)

    ## Show the frame
    # self.SetSizer(self.main_sizer)

    self.Center()
    self.Show(True)


def OnSave(self,event):
    session.commit()
    print "ON Save"
def OnRefresh(self,event): 
    self.exception_panel.populate()
    self.ColorBuckets()
    print "OnRelate"
def OnAnalyze(self,event): 
    objectList = self.exception_panel.GetSelectedObjects()          ## Get list of selected objects to relate in the database
    for object in objectList:
        print object
        object.calculate()
    # print self.GetValueAt(self.GetObjectAt(event.rowIndex),event.subItemIndex)
    # self.temp_value = event.editor.GetValue()
    self.exception_panel.populate()
    print "OnAnalze"
def OnFilterDate1(self,event):
    print "on Filter date"
def OnFilterDate2(self,event):
    print "on Filter date"
def OnFilterDate3(self,event):
    print "on Filter date"

def OnFilterMeter(self,event):
    list = self.exception_panel.col_list
    col  = list[len(list)-1]
    # meter_flt = Filter.Head(10)
    meter_flt = Filter.TextSearch(self.exception_panel,columns = (col), text = "10")
    self.exception_panel.SetFilter(meter_flt)
    self.exception_panel.populate()
    # self.exception_panel.Refresh()


    # self.exception_panel.populate()

    # self.exception_panel.SetObjects(qrty_meters_excpt)
    # self.exception_panel.populate()

    print "On Filter Meter"

def OnBucketMeter(self,event):
    objectList = self.exception_panel.GetSelectedObjects()
    for object in objectList:
        print object
        object.known_flags = 20         ## Meter Mismatch Flag Known ***ffa500
    self.exception_panel.populate()
    self.ColorBuckets()
def OnVioMeter(self,event):
    objectList = self.exception_panel.GetSelectedObjects()
    for object in objectList:
        object.known_flags = 10         ## Meter Mismatch Flag Known ***ffa500
    self.exception_panel.populate()
    self.ColorBuckets() 
def OnTurnsMeter(self,event):
    objectList = self.exception_panel.GetSelectedObjects()
    for object in objectList:
        object.known_flags = 30         ## Meter Mismatch Flag Known ***ffa500
    self.exception_panel.populate()
    self.ColorBuckets()
def OnPlanMeter(self,event):
    objectList = self.exception_panel.GetSelectedObjects()
    for object in objectList:
        object.known_flags = 40         ## Meter Mismatch Flag Known ***ffa500
    self.exception_panel.populate()
    self.ColorBuckets() 

def ColorBuckets(self):         ## Color All Buckets according to knowflags
    ## Query the session for only items that have know_flags greate than 0
    qrty_color_buckets = session.query(Exception).filter(Exception.known_flags != "").all()
    print qrty_color_buckets
    for exception in qrty_color_buckets:
        print exception.known_flags == 10
        flag = int(exception.known_flags)       ## alias the flag
        if flag == 20:      ## Meter Mismatch
            self.exception_panel.SetItemBackgroundColour(self.exception_panel.GetIndexOf(exception),'#ffa500')      ## Oranage
        elif flag == 10:        ## Violation
            self.exception_panel.SetItemBackgroundColour(self.exception_panel.GetIndexOf(exception),'#ff0000')      ## Red
        elif flag == 30:        ## Turns Ratio
            self.exception_panel.SetItemBackgroundColour(self.exception_panel.GetIndexOf(exception),'#0066CC')      ## Blue
        elif flag == 40:    ## Plan referal
            self.exception_panel.SetItemBackgroundColour(self.exception_panel.GetIndexOf(exception),'#FFFF00')      ## Yellow

Please see the function OnFilterMeter.

1

There are 1 answers

4
Werner On BEST ANSWER

The following works for me, it does a search on the second column and it shows just one matching entry.

# -*- coding: utf-8 -*-
import datetime
import wx
import wx.lib.sized_controls as SC
import ObjectListView as OLV

class MyData:
    def __init__(self, year, month, day, level, sets, reps):
        self.date = datetime.date(year, month, day)
        self.level = level
        self.sets = sets
        self.reps = reps

    def GetDate(self):
        return self.date

    def GetLevel(self):
        return self.level

    def GetSets(self):
        return self.sets

    def GetRepetitions(self):
        return self.reps


class MyListCtrl(OLV.GroupListView):
    def __init__(self, parent):
        super(MyListCtrl, self).__init__(parent, wx.ID_ANY, style=wx.LC_REPORT)
        self.SetColumns(self._ColumnDefinitions())
        meter_flt = OLV.Filter.TextSearch(self, columns=self.columns[2:3],
                                          text="7")
        self.SetFilter(meter_flt)
        self.SetObjects(self._DataObjects())

    def _DataObjects(self):
        return [MyData(2010,10,8, 1, 2, 3),
                MyData(2005,10,10, 7, 2, 3),
                MyData(2010,10,3, 2, 2, 3),
                MyData(2012,10,10, 1, 2, 3),
                MyData(2014,10,10, 1, 2, 3)
                ]

    def _ColumnDefinitions(self):
        return [OLV.ColumnDefn('Date', valueGetter='GetDate', groupKeyGetter='GetDate'),
                OLV.ColumnDefn('Level', valueGetter='GetLevel', width=150),
                OLV.ColumnDefn('Sets', valueGetter='GetSets', width=100, groupKeyGetter='GetSets'),
                OLV.ColumnDefn('Reps', valueGetter='GetRepetitions', width=200)]


class MyFrame(SC.SizedFrame):
    def __init__(self):
        super(MyFrame, self).__init__(None)
        pane = self.GetContentsPane()

        olv = MyListCtrl(pane)
        olv.SetSizerProps(expand=True, proportion=1)


if __name__ == '__main__':
    import wx.lib.mixins.inspection as WIT
    app = WIT.InspectableApp()
    win = MyFrame()
    win.Show()
    app.MainLoop()