[Tutor] Sorting/filtering data, dictionary question & Re:OT

Bill Burns billburns at pennswoods.net
Mon Jan 3 07:47:34 CET 2005


Hi,

I have a couple of questions about a program I've been working on. Here's a
overview of what the program does.

Calculates the following data about HVAC/Plumbing pipe:
    A). Total volume of water inside the pipe (US gallons).
    B). The weight of the water inside the pipe.
    C). The actual weight of the pipe, itself.
There's a total of six different types of pipe to choose from and within that
group of six, there's a total of 100 different sizes. Sizes range from 1/2" to
48". The user enters pipe lengths into a GUI and then clicks on a pushButton
to write the data out to a csv file. A dialog then pops up and gives the user
the option to open the csv file (with OpenOffice Calc).

Using suggestions I received from the Tutor List, I created a dictionary for
the program that holds various data about the pipes. The dict holds pipe
sizes, inside diameters, names of the lineEdits on the GUI, weight of the pipe
per foot, etc. 

So, the user enters various pipe lengths, and using the dictionary the program
calculates the totals, the totals go into a list, the list gets sorted and
then the sorted list is written to csv file.

One problem I ran into was sorting my lists. The dictionary I initially came
up with contained a pipe size designation in this format: 1/2", 3/4", 1", etc.
This format is the standard way to write pipe sizes (at least in the US
anyway). When I sorted the data (using this format), I found that the inches
didn't sort the way I wanted them to.
           For example:
               >>>sizes = ['1/2"', '3/4"', '1"', '1-1/4"', '1-1/2"']
               >>>sizes.sort()
               >>>sizes
               ['1"', '1-1/2"', '1-1/4"', '1/2"', '3/4"']
               >>>
Although Python is sorting exactly as it is supposed to, I needed the inches
to be in sequential order. Not knowing what to do, I added another size to the
dictionary before each of the "inch sizes". This new "size" has this type of
format: .5, .75, 1, 1.25, 1.5, etc. And when the data is sorted, it works
perfectly. But now I was left with both a "size" and an "inch size" and I only
wanted the "inch size" in the pipe reports (the csv file).

Below is a small section of the dictionary the program uses (pipeDict). I'm
also providing two functions to give you a general idea of how I'm:
	1). Creating and sorting the pipe data and
	2). How I'm filtering the "size" out of the list.

<code>
pipeDict = \
{('lineEdit1','Steel(Std)',.5,'1/2"',.85): .622, 
('lineEdit2','Steel(Std)',.75,'3/4"',1.13): .824,
('lineEdit3','Steel(Std)',1,'1"',1.678): 1.049,
('lineEdit4','Steel(Std)',1.25,'1-1/4"',2.272): 1.38,
('lineEdit5','Steel(Std)',1.5,'1-1/2"',2.717): 1.61, 
('lineEdit6','Steel(Std)',2,'2"',3.652): 2.067, 
('lineEdit7','Steel(Std)',2.5,'2-1/2"',5.79): 2.469, 
('lineEdit8','Steel(Std)',3,'3"',7.57): 3.068, 
('lineEdit9','Steel(Std)',3.5,'3-1/2"',9.11): 3.548, 
('lineEdit10','Steel(Std)',4,'4"',10.79): 4.026, 
('lineEdit11','Steel(Std)',5,'5"',14.62): 5.047, 
('lineEdit12','Steel(Std)',6,'6"',18.97): 6.065, 
('lineEdit13','Steel(Std)',8,'8"',28.55): 7.981, 
('lineEdit14','Steel(Std)',10,'10"',40.48): 10.02}
      
def somePipeReport():
    report = []
    for name, typ, size, inchSize, weight in pipeDict:
        report.append((typ,size,inchSize,weight))
        report.sort()
        newReport = filterPipeData(report)
    print newReport

def filterPipeData(data):
    filteredData = []
    for typ, size, inchSize, weight in data:
        filteredData.append((typ,inchSize,weight))  
    return filteredData   
</code>

The two functions above are not *exactly*  what I'm using, but it's close
enough to give you an idea of how I'm removing the "size" data. The reason I
created a separate function to filter the "size" out is because I currently
have two reports within the program. I figured it would be better to create
one function that each report could call to filter data. I also thought that
if I create any additional reports in the future, those reports could utilize
the same function as well. 

Question #1:
Am I going about this sorting and filtering thing correctly or have I just
gone insane? My gut feeling is, there's probably an easier/smarter way to do
this.

Question #2:
As I mentioned above, this program calculates the water volume inside of the
pipe. I do that using this function:

        def volCalc(ID, length):
                from math import pi
                gal = ((ID*.5)**2)*pi*(12*length)/(230.9429931) 
                return gal

The ID (inside diameter) is pulled from pipeDict (it's the value in the
dictionary) and the length comes from user input. What I'm wondering is,
would it be a better idea to store in the dictionary a "gallon per foot value"
for each pipe? For example, looking at 10" Steel(Std) pipe in the above
dictionary we find that this type & size of pipe has an ID of 10.02 (inches).
When we plug this ID and a length of 1 foot into the volCalc() function it
returns a total of 4.10 gallons (rounded). Would it be better to store this
"gallon per foot value" (4.10) in the dictionary (as calculated for each pipe)
vs. calculating the gallons the way I'm currently doing it? I guess the real
question is, which method will return the most accurate results? I'm not
looking for a speed improvement.

Any suggestions are appriecated.

Thanks,

Bill

P.S.
Regarding Jacob's OT post:
I'm 34 years old, married 10 yrs w/three kids (boy-8yrs old (soon to be 9),
girl-7yrs old & boy-3yrs old). As you may have gathered from the domain
name "Penn's Woods" I'm from PA . And If you didn't figure it out from my post
above, I'm in the HVAC industry. I'm not a programmer at all. I just stumbled
across Python one day and thought, wow, this is cool, I'm gonna try it out!
I've been *slowly* learning ever since.




More information about the Tutor mailing list