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. >>> >>> Thanks. >>> >>> ---------------------------- >>> >>> import wx >>> >>> >>> class NumbersValidator(wx.PyValidator): >>> >>> def __init__(self): >>> wx.PyValidator.__init__(self) >>> >>> def Clone(self): >>> return NumbersValidator() >>> >>> def Validate(self, parent): >>> text_ctrl = self.GetWindow() >>> text = text_ctrl.GetValue() >>> >>> try: >>> if not text or int(text) <= 0: >>> wx.MessageBox('Enter a valid time.', 'Invalid time >>> entered', wx.OK | wx.ICON_ERROR) >>> return False >>> else: >>> return True >>> except ValueError, error: >>> wx.MessageBox('Enter a valid time.', 'Invalid time entered', >>> wx.OK | wx.ICON_ERROR) >>> return False >> well, assuming you are unsatisfied with the above, you could try to assert >> the >> validation condition and catch together all failures, eg : >> >> def Validate(self, parent): >> text_ctrl = self.GetWindow() >> text = text_ctrl.GetValue() >> try : >> assert int(text)>0 >> return True >> except (ValueError,AssertionError) : >> wx.MessageBox('Enter a valid time.', 'Invalid time entered', >> wx.OK | wx.ICON_ERROR) >> return False >> >> hth, BB > > Assertion statements "go away" when you run python with the '-O' or > '-OO' options.
Makes me wonder how many use those options. I never do, this explains that. > They're only meant for debugging and shouldn't be used > as part of your actual program logic. That's too bad, and bound to evolve if the logic object space of Pypy gains users. > > You run the risk of introducing hard-to-find bugs if you use them like > this and somebody, somewhere, sometime runs your code in "optimized" > mode. Well, I wouldn't formulate it quite like that, but, as you say,... > > Peace, > ~Simon > Well, in that case let's just define def fail(exc) : raise exc and replace assert int(text)>0 by int(text)>0 or fail(ValueError) cheers, BB -- "On naît tous les mètres du même monde" -- http://mail.python.org/mailman/listinfo/python-list