[Tutor] Critique of program
Anna Ravenscroft
revanna at mn.rr.com
Wed Nov 17 10:41:06 CET 2004
Bill Burns 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...)
> 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.
> 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.
HTH
Anna
More information about the Tutor
mailing list