[Tutor] Critique of program

Bill Burns billburns at pennswoods.net
Thu Nov 18 05:03:57 CET 2004


Thank you for your help! I appriecate it! I have made some of the changes
that you suggested, please see below:

You wrote:
> Just a few observations:
> >     def clearLineEdits(self):
> >         """Clears the data in every lineEdit
> >         """
> >         for name in self.__dict__:
> >             w = getattr(self, name)
> >             if hasattr(w, "displayText"): # if isinstance(w, QLineEdit):
> >                 w.clear()
> >         self.lineEditTotal.setText("")
> You said:
>  > I'm not really trying to check whether the object has a certain
>  > attribute that I desire, I just want to know if it's the object that
>  > I'm looking for. IOW, are you a lineEdit or are you a button, if
>  > you're a button then go away I only like lineEdits  ;-)
> Is it *likely* to be a lineEdit? Can you *DO* w.clear() on a button? If
> it's likely to be a lineEdit, and it's only possible to do w.clear() on
> a lineEdit, then you might want to use the "Better to ask forgiveness
> than permission" idiom of a try/except loop. Same with your other uses
> of hasattr.
> Something like (pseudocode):
> for name in self.__dict__:
> 	w = getattr(self, name)
> 	try w.clear()
> 	except TypeError:
> 		pass
> Or something like that... (It's morning and I haven't finished my second
> cup of tea yet...)

I took the pseudocode above and did this:

for name in self.__dict__:
    w = getattr(self, name)
    except TypeError:

Upon executing the revised code, I received the following error:

AttributeError: 'builtin_function_or_method' object has no attribute 'clear'

I changed "except TypeError:" to except AttributeError:" and it worked,
sort of....As it turns out, a QLabel also has a method clear() so not only did
the revised code clear all of the lineEdits, it also cleared all of the labels
on the form as well. It sure gives me a nice clean looking interface :-) As
you had anticipated, as long as no other object had a clear() method, I
would be O.K. So I'll work on this one.

> > def volCalc(ID, length):
> >     """ Calculates the water volume inside of the pipe
> >     """
> >     gal = ((ID*.5)**2)*3.14159265*(12*length)/(230.9429931)
> >     gal = "%0.2f" % gal
> >     return gal
> You're going to be adding these things. It's already a float - so, leave
> it as such. Your presentation/reporting functions can deal with
> formatting stuff like the "0.2f"%gal.

I've removed the formatting as you suggested. Now I just take care of it right
before it is displayed in lineEditTotal.

> > def add(args):
> >     """Sums all of the values on the stack
> >     """
> >     sum = 0
> >     for i in args:
> >         i = float(i)
> >         sum = sum + i
> >     sum = str(sum)
> >     return sum
> There is a built-in sum as of Python 2.3 which is very fast. You may
> want to try using that instead. Also, same comment here as in volCalc:
> return a float. Your presentation/reporting functions can stringify it
> as necessary.

I had no idea that there was a built-in sum. I completely removed the add()
function all together. Before I was using add() like this:
 total = add(stack)

Now I'm using:

  total = sum(stack)

which works like a charm, so I don't even need the add() function at all.

Again, thank you for the help!!


More information about the Tutor mailing list