[portland] Meeting Tomorrow?

Rich Shepard rshepard at appl-ecosys.com
Tue Mar 11 00:34:19 CET 2008


On Mon, 10 Mar 2008, Rich Shepard wrote:

>   If so, could I get some one-on-one tutoring on applying itertools'
> groupby() function to my program? Despite the great response by Dylan on
> the mail list last month, I'm not properly translating it to the
> application's specific needs. I think it's a matter of grouping and keying
> on the proper items in each tuple, but I'm not finding the correct ones by
> trial-and-error.

   Let's try on the list, first.

   Here are several tuples of the sorted list extracted from the database:

   self.appData.tsets --

   [(u'Poor', u'Wildlife', u'Values', u'Wetlands', u'Left Shoulder', 1, 0.0,
30.0, 0.0, 30.0, 0.0, 15.0, 0.5, 15.0, 30.0, 1.0, 3)
(u'Average', u'Wildlife', u'Values', u'Wetlands', u'Trapezoid', 2, 15.0,
85.0, 15.0, 85.0, 30.0, 70.0, 0.5, 85.0, 70.0, 1.0, 3)
(u'Good', u'Wildlife', u'Values', u'Wetlands', u'Right Shoulder', 3, 70.0,
100.0, 70.0, 100.0, 85.0, 100.0, 0.5, 85.0, 30.0, 1.0, 3)
(u'Low', u'HabitatComplexity', u'Fish', u'Wildlife', u'Decay S-Curve', 1,
0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 0.0, 100.0, 1.0, 2)
(u'High', u'HabitatComplexity', u'Fish', u'Wildlife', u'Growth S-Curve', 2,
0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 100.0, 100.0, 1.0, 2)
(u'Few', u'SpecialConcern', u'Terrestrial', u'Wildlife', u'Decay S-Curve',
1, 0.0, 50.0, 0.0, 50.0, 0.0, 50.0, 50.0, 0.0, 50.0, 1.0, 3)
(u'Moderate', u'SpecialConcern', u'Terrestrial', u'Wildlife', u'Bell Curve',
2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 3)
(u'Many', u'SpecialConcern', u'Terrestrial', u'Wildlife', u'Growth S-Curve',
3, 50.0, 100.0, 50.0, 50.0, 0.0, 100.0, 50.0, 100.0, 50.0, 1.0, 3)]

   The method to produce the report first prints the current component
(item[3]), subcomponent (item[2]), and variable name (item[1]). Then, for
each variable, it calls the appropriate function based on the curve shape
(item[4]) to plot that function based on the parameters for each curve. The
curves for each variable are plotted on the same set of axes. For the
example rows above, there would be three plots: one for Wetland Wildlife
Values, one for Fish (Wildlife) Habitat Complexity, and one for Terrestrial
Wildlife species of Special Concern.

   The relevant code (producing a ReportLab report) is:

for row in self.appData.tsets:
       # select component, subcomponent, variable names
       curComp = row[3]
       curSub = row[2]
       curVar = row[1]
       tb.textOut(row[3])      # print component name
       tb.x += 9
       if row[2] == '':
         tb.x += 36
       tb.textOut(curSub)      # print subcomponent name
       tb.x += 9
       tb.textLine(curVar)     # print variable name
       # set up for plotting
       for var, grouped in groupby(row, key=itemgetter(1)):
         print var, map(itemgetter(0), grouped)

which doesn't work at the last two lines. What follows is (in part):

       pylab.hold(True)
       if row[4] == 'Decay S-Curve':
         functions.zCurve(row[10],row[9])
       elif row[4] == 'Bell Curve':
         functions.gaussCurve(row[14],row[14])
       elif row[4] == 'Growth S-Curve':
         functions.sCurve(row[8],row[11])
       elif row[4] == 'Beta':
         functions.betaCurve(row[13],row[12],row[14]
       ...

   Here's the error:

Traceback (most recent call last):
   File "eikos.py", line 145, in OnProjParms
     projectReports().inputVals()
   File "/data1/eikos/reports.py", line 407, in inputVals
     print var, map(itemgetter(0), grouped)
IndexError: string index out of range

   My problem is translating the examples I've read of the itertools'
groupby() function (which focus on dictionaries) to pick the appropriate
items from each tuple I've retrieved from the database. While I now
understand the purpose of groupby(), I'm still not using it correctly.

Rich

-- 
Richard B. Shepard, Ph.D.               |  Integrity            Credibility
Applied Ecosystem Services, Inc.        |            Innovation
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


More information about the Portland mailing list