cleaner way to write this?

John Salerno johnjsal at NOSPAMgmail.com
Wed Oct 25 20:32:47 CEST 2006


John Salerno wrote:
> Hi guys. I'm looking for a nicer, more compact way of writing this code. 

Ok, one more thing. Let's assume I'm using this (or the other):

     def create_db_name(self):
         dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:',
                                  'Create New Database')
         if dlg.ShowModal() == wx.ID_OK:
             db_name = dlg.GetValue()
         else:
             db_name = None
         dlg.Destroy()
         return db_name

Now, one thing I want to prevent is assigning db_name the value of the 
text when OK is pressed if the text is an empty string. So I was 
thinking I could do this:

if dlg.ShowModal() == wx.ID_OK and dlg.GetValue()

I think 'dlg' would exist by the time Python evaluates the second half 
of the expression. But this would involve using dlg.GetValue() again 
within the if statement, which seems redundant.

But I'm also trying to avoid a lot of little if tests all over the 
place, and I definitely don't want to nest them if possible.

Of course, just using the above line isn't good enough, because this 
still assigns db_name to None and returns it. Instead, I'll need an 
error dialog box to pop up. This will involve a wxPython Validator class 
probably (although I suppose this is simple enough not to), but before I 
get to all that, I just need some advice for how to structure the check 
of the empty string.

Thanks again.



More information about the Python-list mailing list