[Tutor] Critique of program

Bill Burns billburns at pennswoods.net
Sat Nov 20 02:08:46 CET 2004

On Friday 19 November 2004 10:40 am, David wrote:
> So after all of the help from the list, what does the final guts of the
> program look like?
Hopefully the guts look good but since I'm a programming n00b who knows :-)

I've tried incorporating (as best I can) the suggestions that I received. 
I hope I'm heading in the right direction, all feedback is welcome.

You'll see below that I'm now using one list "lineEdits = []" instead of
looking into self.__dict__ every time I want to access an object or
attribute. I also removed my add() function in favor of the built-in sum()
function and made other suggested changes.

For the most part, I've added a new comment above a piece of code
that has changed from the original.

Some of the code for the report function (that I didn't have working any
way), I've stripped out to make it easier to read.

I'm still using the same "pipeDict" as before but I'm thinking about changing
it to something like this:

pipeDict = \
{('lineEdit1','stlStd','1/2"'): .622, ('lineEdit2','stlStd','3/4"'): .824,
('lineEdit3','stlStd','1"'): 1.049,('lineEdit4','stlStd','1-1/4"'): 1.38}

and then possibly I can do:

for name, typ, size in pipeDict:
    w = getattr(self, name)

which would give me access to instances, lineEdit names, pipe types and
pipe sizes. I need to figure out how to incorporate the keys for the pipeDict
into this list as well. This new dict is probably the way to go since I want
to add some kind of reporting/printing functionality to the program.

Any suggestions are greatly appriecated.

Thank you all for your help!!


#! /usr/bin/env python

Pipe Volume Calc - calculates the water volume (in U.S. gallons) contained
inside various types and sizes of pipe.

The user selects a type of pipe, a size of pipe and then enters a length (in
feet). Water volume is automatically calculated and displayed as the data is
entered into the form.

There's a GUI front-end which is separate from this module. I made the GUI
using Qt Designer, PyQt and pyuic. The GUI is a tabbed dialog containing 
multiple lineEdits. Each lineEdit corresponds to a specific size of pipe, 
i.e., 1/2", 3/4", etc. Each tabbed page corresponds to a different type of 
pipe (steel, copper or PVC). The user selects the page containing the type 
of pipe and then enters the total footage.

The first dictionary below (pipeDict) holds all the inside diameters (in 
inches) for the various pipes. The inside diameter (and length) are needed 
to calculate water volume.

The lineEdit "groups" are broken out as follows:                                                             
lineEdit1   -> lineEdit25   (standard steel pipe)        
lineEdit1_1 -> lineEdit25_1 (extra strong steel pipe)     
lineEdit1_2 -> lineEdit15_2 (type L copper pipe)       
lineEdit1_3 -> lineEdit13_3 (schedule 40 PVC pipe)       
lineEdit1_4 -> lineEdit13_4 (schedule 80 PVC pipe)
pipeDict = \
{'lineEdit1': .622, 'lineEdit2': .824, 'lineEdit3': 1.049, 
'lineEdit4': 1.38, 'lineEdit5': 1.61, 'lineEdit6': 2.067, 
'lineEdit7': 2.469, 'lineEdit8': 3.068, 'lineEdit9': 3.548, 
'lineEdit10': 4.026, 'lineEdit11': 5.047, 'lineEdit12': 6.065, 
'lineEdit13': 7.981, 'lineEdit14': 10.02, 'lineEdit15': 12.00, 
'lineEdit16': 13.25, 'lineEdit17': 15.25, 'lineEdit18': 17.25, 
'lineEdit19': 19.25, 'lineEdit20': 21.25, 'lineEdit21': 23.25, 
'lineEdit22': 29.25, 'lineEdit23': 35.25, 'lineEdit24': 41.25, 
'lineEdit25': 47.25, 

'lineEdit1_1': .546, 'lineEdit2_1': .742, 'lineEdit3_1': .957, 
'lineEdit4_1': 1.278, 'lineEdit5_1': 1.50, 'lineEdit6_1': 1.939, 
'lineEdit7_1': 2.323, 'lineEdit8_1': 2.90, 'lineEdit9_1': 3.364, 
'lineEdit10_1': 3.826, 'lineEdit11_1': 4.813, 'lineEdit12_1': 5.761, 
'lineEdit13_1': 7.625, 'lineEdit14_1': 9.75, 'lineEdit15_1': 11.75, 
'lineEdit16_1': 13.00, 'lineEdit17_1': 15.00, 'lineEdit18_1': 17.00, 
'lineEdit19_1': 19.00, 'lineEdit20_1': 21.00, 'lineEdit21_1': 23.00, 
'lineEdit22_1': 29.00, 'lineEdit23_1': 35.00, 'lineEdit24_1': 41.00, 
'lineEdit25_1': 47.50, 

'lineEdit1_2': .585, 'lineEdit2_2': .830, 'lineEdit3_2': 1.075, 
'lineEdit4_2': 1.32, 'lineEdit5_2': 1.565, 'lineEdit6_2': 2.055, 
'lineEdit7_2': 2.545, 'lineEdit8_2': 3.035, 'lineEdit9_2': 3.525, 
'lineEdit10_2': 4.015, 'lineEdit11_2': 5.00, 'lineEdit12_2': 5.985, 
'lineEdit13_2': 7.925, 'lineEdit14_2': 9.875, 'lineEdit15_2': 11.845,  

'lineEdit1_3': .731, 'lineEdit2_3': .937, 'lineEdit3_3': 1.182, 
'lineEdit4_3': 1.52, 'lineEdit5_3': 1.755, 'lineEdit6_3': 2.221, 
'lineEdit7_3': 2.672, 'lineEdit8_3': 3.284, 'lineEdit9_3': 4.263, 
'lineEdit10_3': 6.345, 'lineEdit11_3': 8.303, 'lineEdit12_3': 10.385, 
'lineEdit13_3': 12.344, 

'lineEdit1_4': .693, 'lineEdit2_4': .896, 'lineEdit3_4': 1.136, 
'lineEdit4_4': 1.469, 'lineEdit5_4': 1.70, 'lineEdit6_4': 2.157, 
'lineEdit7_4': 2.599, 'lineEdit8_4': 3.20, 'lineEdit9_4': 4.163, 
'lineEdit10_4': 6.193, 'lineEdit11_4': 8.125, 'lineEdit12_4': 10.157, 
'lineEdit13_4': 12.063}

import sys
import types
from qt import *

from pipeCalcGUI import PipeForm

"""create an empty list that will hold the info/attributes of the lineEdits.
   if I put "lineEdits = []" directly under the class, then I need to call it
   like this > self.lineEdit. I don't know which spot is the preferred 
lineEdits = []

class PipeConnector(PipeForm):
    #lineEdits = []
    def __init__(self, parent=None):
       PipeForm.__init__(self, parent)
       self.connect(self.buttonClear,SIGNAL("clicked()"), self.clearLineEdits)
       for name in pipeDict:
           w = getattr(self, name)
           """append tuple to list, index[0] of tuple holds all lineEdit 
              instances, index[1] holds all lineEdit names. we need the 
              instances to perform the built-in QLineEdit functions 
              displayText() & clear()
           self.connect(w,SIGNAL("textChanged(const QString &)"),self.calc)
           validator=QIntValidator(0.00, 9999999.00, w)

    def calc(self):
        stack = []
        """use the lineEdits list"""
        for w, name in lineEdits:
            """don't perform calc() operations on lineEditTotal,
               it's used to display the sum of our calculations, this
               is the same as before
            if name != "lineEditTotal": 
                length = w.displayText()
                """changed from, if not length == """""                
                if length:                      
                   """at this point, length is an instance and we need an int.
                      first convert it to a string and then convert to an int.
                      this is the only way I can seem to convert this, if I 
                      try to convert it directly to an int, I get errors...
                   length = int(str(length),10)                             
                   ID = pipeDict[name]        
                   x = volCalc(ID, length)
        """use built-in sum() to add everthing on the stack,
           previously I was using my own add() function
        total = sum(stack)
        """format total before it is displayed, before
           I was formatting it in volCalc()
        total = "%0.2f" % total
    def clearLineEdits(self): 
       """use the lineEdits list"""  
       for w, name in lineEdits:

def volCalc(ID, length):
    """calculates the water volume"""
    gal = ((ID*.5)**2)*3.14159265*(12*length)/(230.9429931) 
    return gal
if __name__ == "__main__":
    app = QApplication(sys.argv)
    QObject.connect(app, SIGNAL("lastWindowClosed()"),
                    app, SLOT("quit()"))
    win = PipeConnector()

More information about the Tutor mailing list