Re: cleaner way to write this?

2006-10-26 Thread John Salerno
Paul Rubin wrote: > Bruno Desthuilliers <[EMAIL PROTECTED]> writes: >>> But if the user doesn't enter any text, I don't want the method to >>> return at all (even None). >> John, please re-read the FineManual(tm). None is the default return >> value of a function - even if there's no return stateme

Re: cleaner way to write this?

2006-10-26 Thread John Salerno
Steve Holden wrote: > I suspect you need to use a validator so the user can't click OK until > they've put a value int eh text entry item. wow, didn't think of that. that might end up being simpler than anything else i've considered! :) -- http://mail.python.org/mailman/listinfo/python-list

Re: cleaner way to write this?

2006-10-26 Thread Hendrik van Rooyen
"John Salerno" <[EMAIL PROTECTED]> wrote: 8<- > > LOL. Guess I'm doing things right, then? ;) > you can NEVER be sure - Hendrik -- http://mail.python.org/mailman/listinfo/python-list

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
Roy Smith <[EMAIL PROTECTED]> writes: > Isn't this the kind of thing that the new-fangled "with" statement is > supposed to solve? 1) that would require rewriting the wx.TextEntryDialog to have an exit method. 2) It's not necessarily always the case that you want to destroy the dialog when the f

Re: cleaner way to write this?

2006-10-25 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Ben Finney <[EMAIL PROTECTED]> wrote: > John Salerno <[EMAIL PROTECTED]> writes: > > > if dlg.ShowModal() == wx.ID_OK: > > db_name = dlg.GetValue() > > dlg.Destroy() > > return db_name > > else: > >

Re: cleaner way to write this?

2006-10-25 Thread Ben Finney
Farshid Lashkari <[EMAIL PROTECTED]> writes: > Paul Rubin wrote: > > I like > > > > if dlg.ShowModal() == wx.ID_OK: > > db_name = dlg.GetValue() > > else: > > db_name = None > > dlg.Destroy() > > return db_name > > > > better than > > > > db_name = None > >

Re: cleaner way to write this?

2006-10-25 Thread Ben Finney
John Salerno <[EMAIL PROTECTED]> writes: > if dlg.ShowModal() == wx.ID_OK: > db_name = dlg.GetValue() > dlg.Destroy() > return db_name > else: > dlg.Destroy() > return It's for reasons like this that I prefer to ha

Re: cleaner way to write this?

2006-10-25 Thread Steve Holden
John Salerno wrote: > Paul Rubin wrote: > > >>Oh, I see. You really want something like repeat...while, which Python >>doesn't have. Anyway I start to prefer something like (untested): >> >> def create_db_name(self): >> try: >>while True: >> dlg = wx.TextEn

Re: cleaner way to write this?

2006-10-25 Thread Steve Holden
Paul Rubin wrote: > John Salerno <[EMAIL PROTECTED]> writes: > >> if dlg.ShowModal() == wx.ID_OK: >> db_name = dlg.GetValue() >> dlg.Destroy() >> return db_name >> else: >> dlg.Destroy() >> return > > > I like > > i

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
Bruno Desthuilliers <[EMAIL PROTECTED]> writes: > > But if the user doesn't enter any text, I don't want the method to > > return at all (even None). > > John, please re-read the FineManual(tm). None is the default return > value of a function - even if there's no return statement. Correct, but i

Re: cleaner way to write this?

2006-10-25 Thread Bruno Desthuilliers
John Salerno a écrit : > Paul Rubin wrote: > >> John Salerno <[EMAIL PROTECTED]> writes: >> >>> I just need some advice for how to structure >>> the check of the empty string. >> >> >> How about >> >> return db_name or None >> >> since the empty string taken as a boolean is False. > > > But

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Paul Rubin wrote: > John Salerno <[EMAIL PROTECTED]> writes: >> Interesting idea to use try/finally to ensure that dlg.Destroy() runs >> even with a return earlier in the method. > > Note that the code is wrong, the dialog should either be created > outside the while loop, or destroyed inside it.

Re: cleaner way to write this?

2006-10-25 Thread Michael S
How about this? def create_db_name(self): dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:', 'Create New Database') db_name = None #or db_name = "" if dlg.ShowModal() == wx.ID_OK: db_name = dlg.GetValue()

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > Interesting idea to use try/finally to ensure that dlg.Destroy() runs > even with a return earlier in the method. Note that the code is wrong, the dialog should either be created outside the while loop, or destroyed inside it. I don't know wx so I'm not

Re: cleaner way to write this?

2006-10-25 Thread Fredrik Lundh
Paul Rubin wrote: >> but given that try/except and try/finally used to be separate >> blocks, > > That old separation was just an artifact of how the parser was > originally written, I believe. $ more Misc/HISTORY New features in 0.9.6: - stricter try stmt syntax: cannot mix except and

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > Interesting idea to use try/finally to ensure that dlg.Destroy() runs > even with a return earlier in the method. Would this be considered > appropriate use though, or would it be misusing try/finally? I thought > the point of a try block was for when you

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Paul Rubin wrote: > def create_db_name(self): > try: > while True: >dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:', >'Create New Database') >if dlg.ShowModal() != wx.ID_OK: >

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Neil Cerutti wrote: > This completes our C version [of a csv reading library]. It > handles arbitrarily large inputs and does something sensible > even with perverse data. The price is that it is more than four > times as long as the first prototype and some of the code is > intricate. S

Re: cleaner way to write this?

2006-10-25 Thread Neil Cerutti
On 2006-10-25, John Salerno <[EMAIL PROTECTED]> wrote: > Paul Rubin wrote: > >> Right. The above handles that case, I believe. > > Oh, you're right! It's amazing how this went from a simple, > two-line method to a big mess of error-checking. :) >From Kernighan & Pike, _The Practice of Programmin

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Paul Rubin wrote: > Right. The above handles that case, I believe. Oh, you're right! It's amazing how this went from a simple, two-line method to a big mess of error-checking. :) -- http://mail.python.org/mailman/listinfo/python-list

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > > def create_db_name(self): > > try: > > while True: > >dlg = wx.TextEntryDialog(self.frame, 'Enter a database > > name:', > >'Create New Database') > >if dl

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Paul Rubin wrote: > Oh, I see. You really want something like repeat...while, which Python > doesn't have. Anyway I start to prefer something like (untested): > > def create_db_name(self): > try: > while True: >dlg = wx.TextEntryDialog(self.frame, 'Ente

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > But if the user doesn't enter any text, I don't want the method to > return at all (even None). I need an error dialog to pop up and tell > him to enter a value (or press Cancel). Oh, I see. You really want something like repeat...while, which Python doe

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Paul Rubin wrote: > John Salerno <[EMAIL PROTECTED]> writes: >> I just need some advice for how to structure >> the check of the empty string. > > How about > > return db_name or None > > since the empty string taken as a boolean is False. But if the user doesn't enter any text, I don't wan

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > I just need some advice for how to structure > the check of the empty string. How about return db_name or None since the empty string taken as a boolean is False. -- http://mail.python.org/mailman/listinfo/python-list

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
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:',

Re: cleaner way to write this?

2006-10-25 Thread Farshid Lashkari
Paul Rubin wrote: > I like > > if dlg.ShowModal() == wx.ID_OK: > db_name = dlg.GetValue() > else: > db_name = None > dlg.Destroy() > return db_name > > better than > > db_name = None > if dlg.ShowModal() == wx.ID_OK: > db_name = dlg.GetValue() >

Re: cleaner way to write this?

2006-10-25 Thread John Salerno
Paul Rubin wrote: > I like > > if dlg.ShowModal() == wx.ID_OK: > db_name = dlg.GetValue() > else: > db_name = None > dlg.Destroy() > return db_name > > better than > > db_name = None > if dlg.ShowModal() == wx.ID_OK: > db_name = dlg.GetValue()

Re: cleaner way to write this?

2006-10-25 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes: > if dlg.ShowModal() == wx.ID_OK: > db_name = dlg.GetValue() > dlg.Destroy() > return db_name > else: > dlg.Destroy() > return I like if dlg.ShowModal() == wx.ID_OK:

Re: cleaner way to write this?

2006-10-25 Thread Farshid Lashkari
John Salerno wrote: > Hi guys. I'm looking for a nicer, more compact way of writing this code. > It doesn't have to be anything fancy, just something without the > duplication and preferably only one return statement. > > def create_db_name(self): > dlg = wx.TextEntryDialog(self.fram

cleaner way to write this?

2006-10-25 Thread John Salerno
Hi guys. I'm looking for a nicer, more compact way of writing this code. It doesn't have to be anything fancy, just something without the duplication and preferably only one return statement. def create_db_name(self): dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:',

Re: cleaner way to write this try/except statement?

2006-08-03 Thread Boris Borcic
Simon Forman wrote: > Boris Borcic wrote: >> John Salerno wrote: >>> The code to look at is the try statement in the NumbersValidator class, >>> just a few lines down. Is this a clean way to write it? i.e. is it okay >>> to have all those return statements? Is this a good use of try? Etc. >>> >>> T

Re: cleaner way to write this try/except statement?

2006-08-02 Thread Roy Smith
John Salerno <[EMAIL PROTECTED]> wrote: > try: > if int(text) != 0: > return True > else: > self.error_message() > return False > except ValueError: > self.error_message() > return False

Re: cleaner way to write this try/except statement?

2006-08-02 Thread John Salerno
John Salerno wrote: > The code to look at is the try statement in the NumbersValidator class, > just a few lines down. Is this a clean way to write it? i.e. is it okay > to have all those return statements? Is this a good use of try? Etc. Ok, I came up with this. It looks much nicer, I think, th

Re: cleaner way to write this try/except statement?

2006-08-02 Thread John Salerno
Simon Forman wrote: > Maybe I did miss something: Are you actually trying to make a static > method? Say, to be able to call it elsewhere than just the Validate() > method? I don't know if a static method is the best way to do it, but what I was trying to do was make a regular function within

Re: cleaner way to write this try/except statement?

2006-08-02 Thread John Salerno
Peter Otten wrote: > John Salerno wrote: > >> The code to look at is the try statement in the NumbersValidator class, >> just a few lines down. Is this a clean way to write it? i.e. is it okay >> to have all those return statements? Is this a good use of try? Etc. >> >> Thanks. >> >> -

Re: cleaner way to write this try/except statement?

2006-08-02 Thread Peter Otten
John Salerno wrote: > The code to look at is the try statement in the NumbersValidator class, > just a few lines down. Is this a clean way to write it? i.e. is it okay > to have all those return statements? Is this a good use of try? Etc. > > Thanks. > > > > import

Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
Boris Borcic wrote: > John Salerno wrote: > > The code to look at is the try statement in the NumbersValidator class, > > just a few lines down. Is this a clean way to write it? i.e. is it okay > > to have all those return statements? Is this a good use of try? Etc. > > > > Thanks. > > > >

Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
John Salerno wrote: > John Salerno wrote: > > The code to look at is the try statement in the NumbersValidator class, > > just a few lines down. Is this a clean way to write it? i.e. is it okay > > to have all those return statements? Is this a good use of try? Etc. > > I cleaned it up a little and

Re: cleaner way to write this try/except statement?

2006-08-01 Thread Boris Borcic
John Salerno wrote: > The code to look at is the try statement in the NumbersValidator class, > just a few lines down. Is this a clean way to write it? i.e. is it okay > to have all those return statements? Is this a good use of try? Etc. > > Thanks. > > > > import

Re: cleaner way to write this try/except statement?

2006-08-01 Thread infidel
Here's how I would do it: def Validate(self, parent): try: text_ctrl = self.GetWindow() text = text_ctrl.GetValue() if not text: raise ValueError if int(text) <= 0: raise ValueError return True except ValueError, error

Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
Simon Forman wrote: > John Salerno wrote: > > John Salerno wrote: > > > The code to look at is the try statement in the NumbersValidator class, > > > just a few lines down. Is this a clean way to write it? i.e. is it okay > > > to have all those return statements? Is this a good use of try? Etc. >

Re: cleaner way to write this try/except statement?

2006-08-01 Thread Simon Forman
John Salerno wrote: > John Salerno wrote: > > The code to look at is the try statement in the NumbersValidator class, > > just a few lines down. Is this a clean way to write it? i.e. is it okay > > to have all those return statements? Is this a good use of try? Etc. > > I cleaned it up a little and

Re: cleaner way to write this try/except statement?

2006-08-01 Thread John Salerno
John Salerno wrote: > The code to look at is the try statement in the NumbersValidator class, > just a few lines down. Is this a clean way to write it? i.e. is it okay > to have all those return statements? Is this a good use of try? Etc. I cleaned it up a little and did this, but of course this

cleaner way to write this try/except statement?

2006-08-01 Thread John Salerno
The code to look at is the try statement in the NumbersValidator class, just a few lines down. Is this a clean way to write it? i.e. is it okay to have all those return statements? Is this a good use of try? Etc. Thanks. import wx class NumbersValidator(wx.PyVali