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