List or Dictionaries for events? (Newbie question)

First off, hello to all wxPython users from a newbie. I’m just getting started with Python (~3 months) and wxPython (~2 weeks). Second, BIG THANKS to all Python users/contributors etc, its awesome to have such a great FREE language with so many cool tools.

Third (my question):

I have seen in the docs and source code many instances where a big long list of if, elif, else are used. Why is this?? Could this be done easier with a dictionary? (see below for an example 1 for events). Also, I have seen similar things when creating menus.

EXAMPLE 1 from pydocview.py)

    id = event.GetId()
    if id == wx.ID_CUT:
        event.Enable(False)
        return True
    elif id == wx.ID_COPY:
        event.Enable(False)
        return True
    elif id == wx.ID_PASTE:
        event.Enable(False)
        return True
    elif id == wx.ID_CLEAR:
        event.Enable(False)
        return True
    elif id == wx.ID_SELECTALL:
        event.Enable(False)
        return True
    elif id == SAVEALL_ID:
        filesModified = False
        docs = wx.GetApp().GetDocumentManager().GetDocuments()
        for doc in docs:
            if doc.IsModified():
                filesModified = True
                break
        event.Enable(filesModified)
        return True
    else:
        return wx.GetApp().ProcessUpdateUIEvent(event)

EXAMPLE 2 from docview.py)

    wx.EVT_MENU(self, wx.ID_OPEN, self.OnFileOpen)
    wx.EVT_MENU(self, wx.ID_CLOSE, self.OnFileClose)
    wx.EVT_MENU(self, wx.ID_CLOSE_ALL, self.OnFileCloseAll)
    wx.EVT_MENU(self, wx.ID_REVERT, self.OnFileRevert)
    wx.EVT_MENU(self, wx.ID_NEW, self.OnFileNew)
    wx.EVT_MENU(self, wx.ID_SAVE, self.OnFileSave)
    wx.EVT_MENU(self, wx.ID_SAVEAS, self.OnFileSaveAs)
    wx.EVT_MENU(self, wx.ID_UNDO, self.OnUndo)
    wx.EVT_MENU(self, wx.ID_REDO, self.OnRedo)
    wx.EVT_MENU(self, wx.ID_PRINT, self.OnPrint)
    wx.EVT_MENU(self, wx.ID_PRINT_SETUP, self.OnPrintSetup)
    wx.EVT_MENU(self, wx.ID_PREVIEW, self.OnPreview)
    wx.EVT_UPDATE_UI(self, wx.ID_OPEN, self.OnUpdateFileOpen)
    wx.EVT_UPDATE_UI(self, wx.ID_CLOSE, self.OnUpdateFileClose)
    wx.EVT_UPDATE_UI(self, wx.ID_CLOSE_ALL, self.OnUpdateFileCloseAll)
    wx.EVT_UPDATE_UI(self, wx.ID_REVERT, self.OnUpdateFileRevert)
    wx.EVT_UPDATE_UI(self, wx.ID_NEW, self.OnUpdateFileNew)
    wx.EVT_UPDATE_UI(self, wx.ID_SAVE, self.OnUpdateFileSave)
    wx.EVT_UPDATE_UI(self, wx.ID_SAVEAS, self.OnUpdateFileSaveAs)
    wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self.OnUpdateUndo)
    wx.EVT_UPDATE_UI(self, wx.ID_REDO, self.OnUpdateRedo)
    wx.EVT_UPDATE_UI(self, wx.ID_PRINT, self.OnUpdatePrint)
    wx.EVT_UPDATE_UI(self, wx.ID_PRINT_SETUP, self.OnUpdatePrintSetup)
    wx.EVT_UPDATE_UI(self, wx.ID_PREVIEW, self.OnUpdatePreview)

For the menu buttons, I wrote a couple of simple little helper functions:

def AddMenuButton(self, menu, text, function, tip=""):
    """
    Helper function for adding a menu button
    Automatically assigns new id and binds the menu event
    """
    item = menu.AppendItem( wx.MenuItem(menu, wx.NewId(), text))
    menu.SetHelpString(item.GetId(), tip)
    self.Bind(wx.EVT_MENU, function, id=item.GetId())       

def AddMenuButtons(self, menu, buttons):
    """
    Helper function to adding many buttons to a menu
    button text and callback are passed in as tuples
    """
    for button in buttons:
        self.AddMenuButton(menu, button[0], button[1])

Then the buttons are added in another function with

    buttons = [ ('Expand All', self.OnExpandAll),       
                ('Collapse All', self.OnCollapseAll),        
                ('New', self.OnNetworkNew),         
                ('Clear', self.OnNetworkClear),
                ('Activate', self.OnNetworkActivate),
                ('Properties...', self.OnViewObjectProperties)
              ]
    self.AddMenuButtons(menu, buttons)

I am wondering if what I have done is actually a bad idea (since being a newb, if it was a good idea, someone would already be doing it). Coming from a C background, transitioning to C++, then Perl (and discovering how easy lists and dictionaries can be), I now cringe at looking at the long list of if, elif, else statements. I tend to always look for a map, dictionary or list for these types of problems now.

Any thoughts or opinions are welcome. TIA

···

Send e-mail faster without improving your typing skills. Get your Hotmail® account.

Wednesday, December 17, 2008, 12:09:54 AM, Josh Petitt wrote:

I have seen in the docs and source code many instances where a big
long list of if, elif, else are used. Why is this?? Could this be
done easier with a dictionary? (see below for an example 1 for
events).

I'm not sure if it's easier; maybe is just a question of style.

Also, I have seen similar things when creating menus.

Yes. I used to waste a lot of time creating and rearranging menus. Now
I only write lists: elements_28 | Page not found
:slight_smile:

-- tacao

No bits were harmed during the making of this e-mail.

That's really nice -- thanks for sharing it! The only thing I might want on top of that is to read the menu definitions from a file (though I realize that only takes a few extra lines).

I'm going to investigate this more closely in the morning. I love stuff that makes stuff easier.

Thanks,
- Joe

···

On Dec 16, 2008, at 9:12 PM, E. A. Tacao wrote:

Also, I have seen similar things when creating menus.

Yes. I used to waste a lot of time creating and rearranging menus. Now
I only write lists: elements_28 | Page not found
:slight_smile:

Josh Petitt wrote:

I have seen in the docs and source code many instances where a big long list of if, elif, else are used. Why is this?? Could this be done easier with a dictionary?

Well, I think it was Guido who said: " the proper way to spell switch-case in python is not if..elif...elif, but rather a dictionary" - so yes, a dict is often a good way to do it.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov