From rshepard at appl-ecosys.com Fri Mar 7 16:57:46 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 7 Mar 2008 07:57:46 -0800 (PST) Subject: [portland] Mail List Archives? Message-ID: I accidently deleted the thread from last month in which you taught me about the itertools groupby() function. I have only my responses which are not the complete messages sent to me. Is the list archived some place where I can retrieve this thread? TIA, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From reidab at gmail.com Fri Mar 7 17:34:59 2008 From: reidab at gmail.com (Reid Beels) Date: Fri, 7 Mar 2008 08:34:59 -0800 Subject: [portland] Mail List Archives? In-Reply-To: References: Message-ID: <82C2A4DF-2EB6-4D5E-AD57-9BDFE7403ECB@gmail.com> On Mar 7, 2008, at 7:57 AM, Rich Shepard wrote: > Is the list archived some place where I can retrieve this thread? http://mail.python.org/pipermail/portland/ From rshepard at appl-ecosys.com Fri Mar 7 17:41:55 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 7 Mar 2008 08:41:55 -0800 (PST) Subject: [portland] Mail List Archives? In-Reply-To: <82C2A4DF-2EB6-4D5E-AD57-9BDFE7403ECB@gmail.com> References: <82C2A4DF-2EB6-4D5E-AD57-9BDFE7403ECB@gmail.com> Message-ID: On Fri, 7 Mar 2008, Reid Beels wrote: > http://mail.python.org/pipermail/portland/ Many thanks! Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From freyley at gmail.com Sun Mar 9 21:59:09 2008 From: freyley at gmail.com (Jeff Schwaber) Date: Sun, 9 Mar 2008 12:59:09 -0800 Subject: [portland] Meeting, Tuesday, March 11th Message-ID: <8db4a1910803091359x5d5c75f9y18afff0d4a9b1d4@mail.gmail.com> Hey all, We're going to have a presentation about mock objects and the library mocker. Then, of course, there shall be beer. Tuesday, March 11th 7pm CubeSpace See you there. Jeff From rshepard at appl-ecosys.com Mon Mar 10 22:03:45 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 10 Mar 2008 14:03:45 -0700 (PDT) Subject: [portland] Meeting Tomorrow? Message-ID: Do I correctly recall seeing an e-mail message about a meeting tomorrow at CubeSpace? 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. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From afaulkner at mainzbradygroup.com Mon Mar 10 21:10:38 2008 From: afaulkner at mainzbradygroup.com (Allen Faulkner) Date: Mon, 10 Mar 2008 13:10:38 -0700 Subject: [portland] Python position Message-ID: <02cc01c882ea$cf5a2a10$6e0e7e30$@com> My client is looking for a python developer for a short term contract that could lead into a long term opportunity. If you or anyone you know would be interested please give me a call at the number listed below. Duties and Responsibilities: " Develop enhancements to PeerPoint web and desktop applications. " Support and debug existing PeerPoint web and desktop applications. " Provide system administration for all Application, Web and Fax servers associated with PeerPoint. " Develop our planned suite of web portal functions around PeerPoint, that will greatly enhance online content, community and collaboration among our users. " Manage and implement full software development life cycle including inception, definition, design, implementation, QA, deployment and support. " Maintain a broad knowledge of operating systems, programming languages and hardware " Use industry best software project management and development practices. " Monitor system logs and activity on all servers and devices " Lead or guide the work of other staff or contractors engaged in similar functions " Coordinate with contract development team as required to schedule releases of software updates Requirements: " 4 years + experience in Software Development and Systems Administration " Application Development experience using Python 2/FIve, Zope, Javascript, CSS, HTML and PostgreSQL. " Advanced knowledge, system administration and troubleshooting of Linux OS servers. " Advanced knowledge, system administration and troubleshooting of Apache web servers. " Advanced knowledge, system administration and troubleshooting of Zope application server. " Advanced knowledge, system administration and troubleshooting of PostGreSQL SQL server. " Experience with Wiki and other collaboration software technologies. " Knowledge of TCP/IP stacks and their functionality " Working knowledge of web application security, understanding of vulnerabilities and countermeasures. Understanding of HIPAA best practices a plus.. " Experience with professional project management methodologies and tools such as RUP, UML, Use Case preparation, Object Modeling, ER diagrams, code archive and release using source safe, quality assurance testing, etc. " Ability and desire to learn new skills quickly " Effectively communicate issues and resolution to all levels of the organization To see a complete listing of all of our job postings visit our website at www.mainzbradygroup.com Mainz Brady Group, a technical staffing firm, is recognized as a leader in comprehensive employment and staffing solutions to the best known companies in Finance, Electronics/Manufacturing, Retail, Research & Development, Services, Government, Biotechnology, and Telecommunications. We specialize in Information Technology, Technical Sales & Marketing and Human Resources staffing-Contract, Contract to Hire, and Direct Hire. We take great pride in our experienced Executive and IT Recruiter's ability to give our clients the very best talent available and, in doing so, we are able to offer the best career choices and perfect jobs for our candidates. Find out more about us, what we do to help clients find the right candidate, and how we can help job seekers find the right position. At Mainz Brady Group, our experienced Professional IT Staffing Specialists and Executive Recruiters take great pride in our ability to offer our clients the very best candidates available. Allen D. Faulkner Sr. Staffing Specialist Mainz Brady Group 503-430-1706 direct 503-453-9850 mobile 503-961-8910 fax HYPERLINK "mailto:afaulkner at mainzbradygroup.com"afaulkner at mainzbradygroup.com HYPERLINK "http://www.mainzbradygroup.com/"www.mainzbradygroup.com HYPERLINK "http://www.mainzbradygroup.com/"mbgicon No virus found in this outgoing message. Checked by AVG. Version: 7.5.518 / Virus Database: 269.21.7/1323 - Release Date: 3/10/2008 11:07 AM -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/portland/attachments/20080310/31553051/attachment.htm -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 2154 bytes Desc: not available Url : http://mail.python.org/pipermail/portland/attachments/20080310/31553051/attachment.gif From rshepard at appl-ecosys.com Tue Mar 11 00:34:19 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 10 Mar 2008 16:34:19 -0700 (PDT) Subject: [portland] Meeting Tomorrow? In-Reply-To: References: Message-ID: 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 Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Tue Mar 11 00:50:45 2008 From: kirby.urner at gmail.com (kirby urner) Date: Mon, 10 Mar 2008 16:50:45 -0700 Subject: [portland] Meeting Tomorrow? In-Reply-To: References: Message-ID: > 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 > My personal advice, coder to coder, would be to implement a non-itertools solution first, then go to itertools later if you feel there's a significant advantage. Kirby From rshepard at appl-ecosys.com Tue Mar 11 00:55:14 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 10 Mar 2008 16:55:14 -0700 (PDT) Subject: [portland] Meeting Tomorrow? In-Reply-To: References: Message-ID: On Mon, 10 Mar 2008, kirby urner wrote: > My personal advice, coder to coder, would be to implement a non-itertools > solution first, then go to itertools later if you feel there's a > significant advantage. Kirby, That's excellent advice. Unfortunately, I did not come up with working code before Dylan suggested the itertools approach. What I got was the first curve, then all the rest on a single set of axes. I had fits trying to find how to loop through all the tupples in the list, grouping each set by item[1]. I don't care which approach is used, but I'm missing the python experience to get it working. Thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Tue Mar 11 02:25:41 2008 From: kirby.urner at gmail.com (kirby urner) Date: Mon, 10 Mar 2008 18:25:41 -0700 Subject: [portland] Meeting Tomorrow? In-Reply-To: References: Message-ID: Yeah, hard to debug this way, I agree. Note that if you're trying to do a sort within sort, you can do like a database key (m, n), where m, n are your two fields. def extract(row): """return (m,n) for sorting purposes""" return (row[2],row[0]) >>> data = [ ('pia', 'field1', 'scurve', 3, 4, 5), ('zaza', 'field1', 'bellcurve', 9, 8, 8), ('kirby', 'field1', 'bellcurve', 1, 2, 3), ('divine', 'field1', 'trapezoid', 1, 2, 3)] >>> sorted(data, key = extract) [('kirby', 'field1', 'bellcurve', 1, 2, 3), ('zaza', 'field1', 'bellcurve', 9, 8, 8), ('pia', 'field1', 'scurve', 3, 4, 5), ('divine', 'field1', 'trapezoid', 1, 2, 3)] i.e. I'm sorting on field0 (proper name) *within* field 2 (test type). Kirby On Mon, Mar 10, 2008 at 4:55 PM, Rich Shepard wrote: > On Mon, 10 Mar 2008, kirby urner wrote: > > > My personal advice, coder to coder, would be to implement a non-itertools > > solution first, then go to itertools later if you feel there's a > > significant advantage. > > Kirby, > > That's excellent advice. Unfortunately, I did not come up with working > code before Dylan suggested the itertools approach. > > What I got was the first curve, then all the rest on a single set of axes. > I had fits trying to find how to loop through all the tupples in the list, > grouping each set by item[1]. > > I don't care which approach is used, but I'm missing the python experience > to get it working. > > Thanks, > > > > Rich > > -- > Richard B. Shepard, Ph.D. | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: 503-667-8863 > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From rshepard at appl-ecosys.com Tue Mar 11 03:27:16 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 10 Mar 2008 19:27:16 -0700 (PDT) Subject: [portland] Meeting Tomorrow? In-Reply-To: References: Message-ID: On Mon, 10 Mar 2008, kirby urner wrote: > Yeah, hard to debug this way, I agree. :-) > Note that if you're trying to do a sort within sort, you can do like > a database key (m, n), where m, n are your two fields. No, I'm not trying to do nested sorts. Once the table data are in the list of tuples, they're in the proper sequence; that is, the curves are in numeric order within each variable name/subcomponent name/component name. Where I keep getting high centered is trying to implement nested loops. For example, select all of the first component, then all of its subcomponents, then all of its variables. Now plot each curve in sequential order. Go to the next variable, then next subcomponent, then component. In the past I've implemented nested loops (FOR, WHILE, DO-WHILE, etc.) in fortran and C. I've not been able to translate that long-ago experience into python. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Tue Mar 11 17:56:23 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Tue, 11 Mar 2008 09:56:23 -0700 (PDT) Subject: [portland] Interating Tuples Through a List In-Reply-To: References: Message-ID: On Mon, 10 Mar 2008, kirby urner wrote: > Note that if you're trying to do a sort within sort, you can do like > a database key (m, n), where m, n are your two fields. Kirby, et al.: I thought of a clearer way to explain what I'm trying to do; I hope that you'll see where I got stuck and can get me going again in the right direction. I have a list of tuples. Each tuple has 17 fields in the range [0,16]. What I need to do is this: For each component (item[3]): For each subcomponent (item[2]): For each variable (item[1]): For each term (item[0] in range [1,item[16]): draw axes plot the curve for each term (item[4), in sequential order (item[5]) by calling the plotting function and passing appropriate parameters (other items) next term close axes (set up for next multicurve plot) next variable next subcomponent next component Here's the non-working (as intended) code: for row in self.appData.tsets: curComp = row[3] tb.textOut(row[3]) # print component name tb.x += 9 for s in row: curSub = s[2] if s[2] == '': tb.x += 36 tb.textOut(curSub) # print subcomponent name tb.x += 9 print s for v in s: curVar = v[1] print curVar tb.textLine(curVar) # print variable name # set up for plotting But, 's' is not what I thought it should be. Therefore, I need to learn how to properly code these nested loops so that all values in the tuple are present in each loop. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Thu Mar 13 01:24:42 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Wed, 12 Mar 2008 17:24:42 -0700 (PDT) Subject: [portland] Interating Tuples Through a List -- RESOLVED In-Reply-To: References: Message-ID: A public 'thank you' to Kirby for pointing out what I had forgotten. That is, to subset a list it is necessary to create a second list with the appropriate tuples (in my case) from the original list. Doing this repeatedly, with the new lists nested, does just what I need to do. Having done this before in other parts of the application I should have remembered how to do it, but for some reason that completely wiped from memory. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Thu Mar 13 23:22:36 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Thu, 13 Mar 2008 15:22:36 -0700 (PDT) Subject: [portland] Executing Python Code Within Emacs Message-ID: I just upgraded python-mode.el so the 'execute buffer' command (C-c C-c) works. However, while ipython is called and the script runs, the output is filled with ANSI escape sequences; for example: 2m/data1/eikos/m 1mNameErrorm: name 'row' is not defined 1m------------------------------------------------------------m 1mIndentationErrorm1m:m unindent does not match any outer indentation level (, line 5) 1m---------------------------------------------------------------------------m 1mexceptions.NameErrorm Traceback (most recent call last) 2m/data1/eikos/m It actually looks worse than this, but pine is correctly translating some of the codes. How do I clean this up? Or, where do I ask for help with this issue? Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From markgross at thegnar.org Fri Mar 14 02:26:24 2008 From: markgross at thegnar.org (mark gross) Date: Thu, 13 Mar 2008 18:26:24 -0700 Subject: [portland] Executing Python Code Within Emacs In-Reply-To: References: Message-ID: <20080314012624.GA19452@thegnar.org> On Thu, Mar 13, 2008 at 03:22:36PM -0700, Rich Shepard wrote: > I just upgraded python-mode.el so the 'execute buffer' command (C-c C-c) > works. However, while ipython is called and the script runs, the output is > filled with ANSI escape sequences; for example: > > 2m/data1/eikos/m > > 1mNameErrorm: name 'row' is not defined > 1m------------------------------------------------------------m > 1mIndentationErrorm1m:m unindent does not match any outer indentation level > (, line 5) > > 1m---------------------------------------------------------------------------m > 1mexceptions.NameErrorm Traceback (most > recent call last) > > 2m/data1/eikos/m > > It actually looks worse than this, but pine is correctly translating some > of the codes. > > How do I clean this up? Or, where do I ask for help with this issue? I'm just guessing but : Try running dos2unix on the file containing the implementation of 1mNameErrorm? It looks like it may be a file from a Windows computer that was copied over / installed on a Linux box. --mgross > > Rich > > -- > Richard B. Shepard, Ph.D. | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: 503-667-8863 > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: Digital signature Url : http://mail.python.org/pipermail/portland/attachments/20080313/255a79dc/attachment.pgp From adam at therobots.org Fri Mar 14 03:06:36 2008 From: adam at therobots.org (Adam Lowry) Date: Thu, 13 Mar 2008 21:06:36 -0500 Subject: [portland] Executing Python Code Within Emacs In-Reply-To: References: Message-ID: On Mar 13, 2008, at 5:22 PM, Rich Shepard wrote: > I just upgraded python-mode.el so the 'execute buffer' command (C- > c C-c) > works. However, while ipython is called and the script runs, the > output is > filled with ANSI escape sequences; for example: > > 2m/data1/eikos/m > > 1mNameErrorm: name 'row' is not defined Just a guess, but ipython by default uses lots of colors. I'd give launching it as "ipython --colors nocolor" a shot, just to see if that's the problem. Adam From rshepard at appl-ecosys.com Fri Mar 14 14:08:06 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 14 Mar 2008 06:08:06 -0700 (PDT) Subject: [portland] Executing Python Code Within Emacs In-Reply-To: References: Message-ID: On Thu, 13 Mar 2008, Adam Lowry wrote: > Just a guess, but ipython by default uses lots of colors. I'd give > launching it as "ipython --colors nocolor" a shot, just to see if that's > the problem. Adam, Insightful answer. Ipython does do colors, and the ANSI codes are definitely color related. Now the trick is to look through python-mode.el and see if I can easily spot where ipython is called when C-c C-c is pressed. However, since emacs uses syntax-specific colors, too, I wonder why ipython doesn't display its colors in another emacs buffer. Thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Fri Mar 14 23:23:19 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 14 Mar 2008 15:23:19 -0700 (PDT) Subject: [portland] Executing Python Code Within Emacs In-Reply-To: References: Message-ID: On Fri, 14 Mar 2008, Rich Shepard wrote: > Insightful answer. Ipython does do colors, and the ANSI codes are > definitely color related. Now the trick is to look through python-mode.el > and see if I can easily spot where ipython is called when C-c C-c is > pressed. After looking through the python-mode.el file I see that it's not calling ipython after all, but reg'lar python: the cpython flavor. Perhaps that's the problem because I don't recall plain python using colors in a console. Well, I wrote to one of the maintainers so we'll see if I can get an answer from him. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Mon Mar 17 21:28:16 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 17 Mar 2008 13:28:16 -0700 (PDT) Subject: [portland] Dictionary To Switch Contexts Message-ID: Rather than using multiple if ... elif ... statements, I'm trying to implement a dictionary to call the proper function based on the value passed to it. I don't know how to express the value when a key is selected. Here's the dictionary: curvePlot = { 'Decay S-Curve' : zCurve, 'Bell Curve' : bellCurve, 'Growth S-Curve' : sCurve, 'Beta' : betaCurve, 'Data' : dataCurve, 'Linear Increasing' : linIncrCurve, 'Linear Decreasing' : linDecrCurve, 'Left Shoulder' : leftShouldCurve, 'Trapezoid' : trapCurve, 'Right Shoulder' : rightShouldCurve, 'Triangle' : triangCurve, 'Singleton' : singleCurve, 'Rectangle' : rectCurve, 'Outcome' : resultsCurve } These curve are actually in a separate module, 'functions,' but python doesn't like me to put the function name in the dictionary, so I have helper functions in the dictionary's module. Here's how I'm trying to use the dictionary: if compList[0][1] == curVar: # call plotting function pylab.hold(True) curvePlot.get(compList[0][4], ???) I don't know what to write where the question marks are. The item at compList[0][4] is a string that matches one of the dictionary keys. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From python at dylanreinhardt.com Mon Mar 17 21:53:20 2008 From: python at dylanreinhardt.com (Dylan Reinhardt) Date: Mon, 17 Mar 2008 13:53:20 -0700 Subject: [portland] Dictionary To Switch Contexts In-Reply-To: References: Message-ID: <4c645a720803171353j1afddc7dl53a8d2e5d534af7@mail.gmail.com> Here's a simplified example of how that might work... hope this helps: ---- def doA(): pass def doB(): pass switcher = { 'a': doA, 'b': doB } x = someFunction() # returns 'a' or 'b' switcher[x]() ---- With obvious changes if you want to pass arguments, check switcher with .get(), etc. Dylan On 3/17/08, Rich Shepard wrote: > > Rather than using multiple if ... elif ... statements, I'm trying to > implement a dictionary to call the proper function based on the value > passed > to it. I don't know how to express the value when a key is selected. > > Here's the dictionary: > > curvePlot = { > 'Decay S-Curve' : zCurve, > 'Bell Curve' : bellCurve, > 'Growth S-Curve' : sCurve, > 'Beta' : betaCurve, > 'Data' : dataCurve, > 'Linear Increasing' : linIncrCurve, > 'Linear Decreasing' : linDecrCurve, > 'Left Shoulder' : leftShouldCurve, > 'Trapezoid' : trapCurve, > 'Right Shoulder' : rightShouldCurve, > 'Triangle' : triangCurve, > 'Singleton' : singleCurve, > 'Rectangle' : rectCurve, > 'Outcome' : resultsCurve > } > > These curve are actually in a separate module, 'functions,' but python > doesn't like me to put the function name in the dictionary, so I have > helper > functions in the dictionary's module. > > Here's how I'm trying to use the dictionary: > > if compList[0][1] == curVar: > # call plotting function > pylab.hold(True) > curvePlot.get(compList[0][4], ???) > > I don't know what to write where the question marks are. The item at > compList[0][4] is a string that matches one of the dictionary keys. > > Rich > > > -- > Richard B. Shepard, Ph.D. > | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: > 503-667-8863 > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/portland/attachments/20080317/884c4657/attachment.htm From rshepard at appl-ecosys.com Mon Mar 17 23:51:52 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 17 Mar 2008 15:51:52 -0700 (PDT) Subject: [portland] Dictionary To Switch Contexts In-Reply-To: <4c645a720803171353j1afddc7dl53a8d2e5d534af7@mail.gmail.com> References: <4c645a720803171353j1afddc7dl53a8d2e5d534af7@mail.gmail.com> Message-ID: On Mon, 17 Mar 2008, Dylan Reinhardt wrote: > Here's a simplified example of how that might work... hope this helps: > ---- > def doA(): > pass > > def doB(): > pass > > switcher = { > 'a': doA, > 'b': doB > } > > x = someFunction() # returns 'a' or 'b' > > switcher[x]() > ---- > > With obvious changes if you want to pass arguments, check switcher with > .get(), etc. Dylan, I miss seeing how I can create a function to provide the value when given the key. My code uses the item within the tuple (compList[0][4]) as the dictionary's key, and I want to call the function whose name is the value of that key. And all this time I thought that I understood dictionaries. :-) Thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From richardson.michael.t at gmail.com Mon Mar 17 23:55:20 2008 From: richardson.michael.t at gmail.com (Michael Richardson) Date: Mon, 17 Mar 2008 15:55:20 -0700 Subject: [portland] Dictionary To Switch Contexts In-Reply-To: References: <4c645a720803171353j1afddc7dl53a8d2e5d534af7@mail.gmail.com> Message-ID: <47DEF6D8.3080401@gmail.com> > I miss seeing how I can create a function to provide the value when given > the key. My code uses the item within the tuple (compList[0][4]) as the > dictionary's key, and I want to call the function whose name is the value of > that key. Python returns the actual function when you get it via the dict - so you can just call it directly. For example: >>> def times_two(value): ... return value*2 ... >>> def times_one(value): ... return value ... >>> func_dict = {'bytwo': times_two, 'byone': times_one} >>> func_dict['bytwo'](4) 8 >>> func_dict['byone'](4) 4 >>> From rshepard at appl-ecosys.com Tue Mar 18 01:50:08 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 17 Mar 2008 17:50:08 -0700 (PDT) Subject: [portland] Dictionary To Switch Contexts In-Reply-To: <47DEF6D8.3080401@gmail.com> References: <4c645a720803171353j1afddc7dl53a8d2e5d534af7@mail.gmail.com> <47DEF6D8.3080401@gmail.com> Message-ID: On Mon, 17 Mar 2008, Michael Richardson wrote: > Python returns the actual function when you get it via the dict - so you > can just call it directly. For example: Michael, > >>> func_dict['byone'](4) It was how I called it that was incorrect. Using brackets made all the difference! And, now that the syntax is correct it accepts the call to the functions module within the dictionary. On to the next bug, ... Many thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Thu Mar 20 18:32:56 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Thu, 20 Mar 2008 10:32:56 -0700 (PDT) Subject: [portland] Need Help With a For Loop Message-ID: I cannot see what's causing the error I see, but it must be something simple that I've overlooked. Your more experienced eyes will probably see it. Here's the for loop: for i in range(1, compList[0][16]): pylab.hold(True) if compList[0][4] == 'Decay S-Curve': testFunctions.zCurve(compList[0][10],compList[0][9]) elif compList[0][4] == 'Bell Curve': testFunctions.gaussCurve(compList[0][14],compList[0][14]) elif compList[0][4] == 'Growth S-Curve': testFunctions.sCurve(compList[0][8],compList[0][11]) elif compList[0][4] == 'Beta': testFunctions.betaCurve(compList[0][13],compList[0][12],compList[0][14]) elif compList[0][4] == 'Data': continue elif compList[0][4] == 'Linear Increasing': testFunctions.linearIncrCurve(compList[0][8],compList[0][11]) elif compList[0][4] == 'Linear Decreasing': testFunctions.linearDecrCurve(compList[0][10],compList[0][9]) elif compList[0][4] == 'Left Shoulder': testFunctions.leftShoulderCurve(compList[0][10],compList[0][11],compList[0][9]) continue elif compList[0][4] == 'Trapezoid': testFunctions.trapezoidCurve(compList[0][8],compList[0][10],compList[0][11],compList[0][9]) continue elif compList[0][4] == 'Right Shoulder': testFunctions.rightShoulderCurve(compList[0][8],compList[0][10],compList[0][11]) continue elif compList[0][4] == 'Triangle': testFunctions.triangleCurve(compList[0][8],compList[0][13],compList[0][9]) elif compList[0][4] == 'Singleton': testFunctions.singletonCurve(compList[0][13],compList[0][14]) elif compList[0][4] == 'Rectangle': testFunctions.rectangleCurve(compList[0][8],compList[0][10],compList[0][11],compList[0][9]) elif compList[0][4] == 'Outcome': testFunctions.outcomeCurve() pylab.savefig(curVar+'.png') The first 'i' passes the first 'if' test and control passes to testFunctions.zCurve(). When that function returns, the program is NOT at the 'for' statement again, but at one earlier in the code. So, instead of incrementing i and running through the if tests (which should take it to the one for 'Growth S-Curve', it repeats the same plotting function call then immediately jumps to pylab.savefig(). Why? I can provide the test code with data and the ancillary module with the plotting functions, but you'd need NumPy and Matplotlib installed to run it. TIA, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From brett at rdnzl.net Thu Mar 20 18:45:50 2008 From: brett at rdnzl.net (Brett Carter) Date: Thu, 20 Mar 2008 10:45:50 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: <47E2A2CE.4030908@rdnzl.net> Rich, are you sure this loop is doing what you want it to? You're incrementing i 'range(1, compList[0][16])' times, but i is never tested in the loop. -Brett Rich Shepard wrote: > I cannot see what's causing the error I see, but it must be something > simple that I've overlooked. Your more experienced eyes will probably see > it. > > Here's the for loop: > > for i in range(1, compList[0][16]): > pylab.hold(True) > if compList[0][4] == 'Decay S-Curve': > testFunctions.zCurve(compList[0][10],compList[0][9]) > elif compList[0][4] == 'Bell Curve': > testFunctions.gaussCurve(compList[0][14],compList[0][14]) > elif compList[0][4] == 'Growth S-Curve': > testFunctions.sCurve(compList[0][8],compList[0][11]) > elif compList[0][4] == 'Beta': > testFunctions.betaCurve(compList[0][13],compList[0][12],compList[0][14]) > elif compList[0][4] == 'Data': > continue > elif compList[0][4] == 'Linear Increasing': > testFunctions.linearIncrCurve(compList[0][8],compList[0][11]) > elif compList[0][4] == 'Linear Decreasing': > testFunctions.linearDecrCurve(compList[0][10],compList[0][9]) > elif compList[0][4] == 'Left Shoulder': > testFunctions.leftShoulderCurve(compList[0][10],compList[0][11],compList[0][9]) > continue > elif compList[0][4] == 'Trapezoid': > testFunctions.trapezoidCurve(compList[0][8],compList[0][10],compList[0][11],compList[0][9]) > continue > elif compList[0][4] == 'Right Shoulder': > testFunctions.rightShoulderCurve(compList[0][8],compList[0][10],compList[0][11]) > continue > elif compList[0][4] == 'Triangle': > testFunctions.triangleCurve(compList[0][8],compList[0][13],compList[0][9]) > elif compList[0][4] == 'Singleton': > testFunctions.singletonCurve(compList[0][13],compList[0][14]) > elif compList[0][4] == 'Rectangle': > testFunctions.rectangleCurve(compList[0][8],compList[0][10],compList[0][11],compList[0][9]) > elif compList[0][4] == 'Outcome': > testFunctions.outcomeCurve() > pylab.savefig(curVar+'.png') > > The first 'i' passes the first 'if' test and control passes to > testFunctions.zCurve(). When that function returns, the program is NOT at the > 'for' statement again, but at one earlier in the code. > > So, instead of incrementing i and running through the if tests (which > should take it to the one for 'Growth S-Curve', it repeats the same plotting > function call then immediately jumps to pylab.savefig(). > > Why? > > I can provide the test code with data and the ancillary module with the > plotting functions, but you'd need NumPy and Matplotlib installed to run it. > > TIA, > > Rich > From rshepard at appl-ecosys.com Thu Mar 20 19:09:23 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Thu, 20 Mar 2008 11:09:23 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: <47E2A2CE.4030908@rdnzl.net> References: <47E2A2CE.4030908@rdnzl.net> Message-ID: On Thu, 20 Mar 2008, Brett Carter wrote: > Rich, are you sure this loop is doing what you want it to? Brett, I'm sure it's _not_ doing what I want. :-( > You're incrementing i 'range(1, compList[0][16])' times, but i is never > tested in the loop. True. The logic is FUBAR. Rather than 'i', what I want to use for the target in the for loop is the curve number. Quick background: Each parent (that's a named entity in the data) is represented by tuples in compList. compList[0][16] is the total number of tuples for each parent (compList[0][1]. I want to interate through all tuples for each parent in the 'for' loop. The sequential number of the curves to be drawn for that parent is in compList[0][5], and the curve name (used by the if/elif tests) is in compList[0][4]. The behavior I want, in pseudocode is this: For each parent: hold plot axes until done use curve #1 and call appropriate plotting function increment curve number (which, I believe) the for loop does automatically) use next curve and call appropriate plotting function when all plots for that parent are drawn ... write plot to file release plot axes for next parent when done, stop I tried adding a test for i < max, then issuing the continue command, but that makes no difference. Obviously, I've missed the bus here. And I have demonstrated no idea how this for loop should be constructed. Thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From python at dylanreinhardt.com Thu Mar 20 20:10:10 2008 From: python at dylanreinhardt.com (Dylan Reinhardt) Date: Thu, 20 Mar 2008 12:10:10 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <47E2A2CE.4030908@rdnzl.net> Message-ID: <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> On 3/20/08, Rich Shepard wrote: > > > The behavior I want, in pseudocode is this: > > For each parent: > hold plot axes until done > use curve #1 and call appropriate plotting function > increment curve number (which, I believe) the for loop does > automatically) > use next curve and call appropriate plotting function Your problem is that you're running the loop once for each item in your collection, but appear to be operating on the same element each time through the loop. It is true that your index will increment, but then if you don't use the index to acquire your next element, the fact that it increments is of no use. In most cases, I would strongly recommend ditching this idiom: for i in range(len(collection)): doSomething(collection[i]) In favor of this one: for item in collection: doSomething(item) HTH, Dylan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/portland/attachments/20080320/f6c1a2c5/attachment.htm From rshepard at appl-ecosys.com Thu Mar 20 21:24:59 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Thu, 20 Mar 2008 13:24:59 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> References: <47E2A2CE.4030908@rdnzl.net> <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> Message-ID: On Thu, 20 Mar 2008, Dylan Reinhardt wrote: > In most cases, I would strongly recommend ditching this idiom: > > for i in range(len(collection)): > doSomething(collection[i]) > > In favor of this one: > > for item in collection: > doSomething(item) Dylan, Tried this before but still could not get it correct. Just tried again, too. Here is a set of three tuples for the same parent variable. I want to loop through the three plotting the appropriate curve each time: [("Few","Abundance","Fish","Wildlife","Decay S-Curve",1,0.0,50.0,0.0,50.0,0.0,50.0,50.0,0.0,50.0,1.0,2), ("Moderate","Abundance","Fish","Wildlife","Bell pCurve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,100.0,1.0,2), ("Many","Abundance","Fish","Wildlife","Growth S-Curve",3,50.0,100.0,50.0,50.0,0.0,50.0,50.0,100.0,50.0,1.0,2)] The parent is "Abundance," the sequence number is item[4], the total number of curves to plot on the same set of axes is item[16], and item[4] is used to call the appropriate plotting function. Starting like this does not work: for item in compList: pylab.hold(True) if compList[0][4] == 'Decay S-Curve': testFunctions.zCurve(compList[0][10],compList[0][9]) elif compList[0][4] == 'Bell Curve': testFunctions.gaussCurve(compList[0][14],compList[0][14]) elif compList[0][4] == 'Growth S-Curve': testFunctions.sCurve(compList[0][8],compList[0][11]) ... I can't do 'for item[0][1] in compList:' because that generates "NameError: global name 'item' is not defined". Thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From dylanr at dylanreinhardt.com Thu Mar 20 23:24:38 2008 From: dylanr at dylanreinhardt.com (Dylan Reinhardt) Date: Thu, 20 Mar 2008 15:24:38 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <47E2A2CE.4030908@rdnzl.net> <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> Message-ID: <4c645a720803201524ned28b1fn9ab2f5e3551eb23e@mail.gmail.com> OK... I think I'm starting to grok the problem. Taking this as a one-off, you might use a list comprehension to great effect here. If you don't know, a list comprehension basically executes some operation on all elements of a collection and returns a list of results. So, for example: >>> a = 'hello' >>> [i for i in a] ['h', 'e', 'l', 'l', 'o'] More complexly, you can return an element *and* the collection itself into a list of tuples: >>> [(i, a) for i in a] [('h', 'hello'), ('e', 'hello'), ('l', 'hello'), ('l', 'hello'), ('o', 'hello')] List comprehensions are especially useful in a case like this where you want to re-pack sequence items in a way that makes sorting, filtering, or grouping easier. I think that might hold the key here. You want a couple specific positions in each tuple to be named, but also want access to the whole tuple. So if you try something like: for arg1, arg2, elems in [(i[4], i[8], i) for i in compList]: ... Now the var named "arg1" corresponds with the element at position 4 in each tuple. You also have another name for a different position and even have a ref to the whole tuple. You would obviously choose more descriptive names... but this pattern allows you to *mostly* keep the positional references socked away in one place and deal with *names* everywhere else. Longer term, though, I'd encourage you to use a class to store information of this complexity, especially if you find yourself doing this kind of stuff in more than one place. That would allow you to write code like: for item in collection: if item.curve == 'some value': item.plot(some_arg) ... or even better, you could pack that logic into the class, such that you're left with: for item in collection: if item.some_property: item.plot() HTH, Dylan On 3/20/08, Rich Shepard wrote: > > On Thu, 20 Mar 2008, Dylan Reinhardt wrote: > > > In most cases, I would strongly recommend ditching this idiom: > > > > for i in range(len(collection)): > > doSomething(collection[i]) > > > > In favor of this one: > > > > for item in collection: > > doSomething(item) > > > Dylan, > > Tried this before but still could not get it correct. Just tried again, > too. Here is a set of three tuples for the same parent variable. I want to > loop through the three plotting the appropriate curve each time: > > [("Few","Abundance","Fish","Wildlife","Decay > S-Curve",1,0.0,50.0,0.0,50.0,0.0,50.0,50.0,0.0,50.0,1.0,2), > ("Moderate","Abundance","Fish","Wildlife","Bell > pCurve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,100.0,1.0,2), > ("Many","Abundance","Fish","Wildlife","Growth > S-Curve",3,50.0,100.0,50.0,50.0,0.0,50.0,50.0,100.0,50.0,1.0,2)] > > The parent is "Abundance," the sequence number is item[4], the total > number of curves to plot on the same set of axes is item[16], and item[4] > is > used to call the appropriate plotting function. > > Starting like this does not work: > > for item in compList: > > pylab.hold(True) > if compList[0][4] == 'Decay S-Curve': > testFunctions.zCurve(compList[0][10],compList[0][9]) > elif compList[0][4] == 'Bell Curve': > testFunctions.gaussCurve(compList[0][14],compList[0][14]) > elif compList[0][4] == 'Growth S-Curve': > testFunctions.sCurve(compList[0][8],compList[0][11]) > > ... > > I can't do 'for item[0][1] in compList:' because that generates > "NameError: global name 'item' is not defined". > > > Thanks, > > Rich > > -- > Richard B. Shepard, Ph.D. > | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: > 503-667-8863 > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/portland/attachments/20080320/1eec4a20/attachment.htm From rshepard at appl-ecosys.com Thu Mar 20 23:46:36 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Thu, 20 Mar 2008 15:46:36 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: <4c645a720803201524ned28b1fn9ab2f5e3551eb23e@mail.gmail.com> References: <47E2A2CE.4030908@rdnzl.net> <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> <4c645a720803201524ned28b1fn9ab2f5e3551eb23e@mail.gmail.com> Message-ID: On Thu, 20 Mar 2008, Dylan Reinhardt wrote: > Taking this as a one-off, you might use a list comprehension to great > effect here. If you don't know, a list comprehension basically executes > some operation on all elements of a collection and returns a list of > results. So, for example: Dylan, I thought a list comprehension would be the ticket, but my feeble efforts got me nowhere. > More complexly, you can return an element *and* the collection itself into a > list of tuples: > >>>> [(i, a) for i in a] > [('h', 'hello'), ('e', 'hello'), ('l', 'hello'), ('l', 'hello'), ('o', > 'hello')] I'll need to work with this, as it looks exactly what I want. > Longer term, though, I'd encourage you to use a class to store information > of this complexity, especially if you find yourself doing this kind of > stuff in more than one place. That would allow you to write code like: The data are stored in a SQLite table; two tables, actually, and retrieved with a SELECT statement into the list of tuples. For testing purposes I copied the results into a static list. I'm going to read again about list comprehensions in the 3rd edition of 'Learning Python' that recently arrived, as well as in 'Python in a Nutshell, 2nd ed.' Then, with practice, I'll understand it well enough to implement the multi-criteria target you illustrated. Many thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From richardson.michael.t at gmail.com Thu Mar 20 23:49:42 2008 From: richardson.michael.t at gmail.com (Michael Richardson) Date: Thu, 20 Mar 2008 15:49:42 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <47E2A2CE.4030908@rdnzl.net> <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> <4c645a720803201524ned28b1fn9ab2f5e3551eb23e@mail.gmail.com> Message-ID: > The data are stored in a SQLite table; two tables, actually, and > retrieved > with a SELECT statement into the list of tuples. For testing > purposes I > copied the results into a static list. For any dealings with databases, I generally find an ORM to be an absolute lifesaver - if you're not doing web stuff, Elixir ( http://elixir.ematia.de/trac/wiki ) is my favorite; otherwise, I stick with Django. From dylanr at dylanreinhardt.com Fri Mar 21 00:16:28 2008 From: dylanr at dylanreinhardt.com (Dylan Reinhardt) Date: Thu, 20 Mar 2008 16:16:28 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <47E2A2CE.4030908@rdnzl.net> <4c645a720803201210y6ca787c2y822b2828a5adce3c@mail.gmail.com> <4c645a720803201524ned28b1fn9ab2f5e3551eb23e@mail.gmail.com> Message-ID: <4c645a720803201616s465122d3lb08fb48ee954bf33@mail.gmail.com> I was just about to suggest SQLAlchemy. Elixir is a great way to get at SQLAlchemy. Either way, once you get the database metadata stuff in line, it's an incredibly easy way to manipulate SQL-stored data as instances of a Python class. Here's a decent tutorial that should give a high-level view of what that might look like: http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html HTH, Dylan On 3/20/08, Michael Richardson wrote: > > > The data are stored in a SQLite table; two tables, actually, and > > retrieved > > with a SELECT statement into the list of tuples. For testing > > purposes I > > copied the results into a static list. > > > For any dealings with databases, I generally find an ORM to be an > absolute lifesaver - if you're not doing web stuff, Elixir ( > http://elixir.ematia.de/trac/wiki > ) is my favorite; otherwise, I stick with Django. > > > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/portland/attachments/20080320/baa9fefe/attachment.htm From markgross at thegnar.org Fri Mar 21 03:38:02 2008 From: markgross at thegnar.org (mark gross) Date: Thu, 20 Mar 2008 19:38:02 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: <20080321023802.GA9354@thegnar.org> On Thu, Mar 20, 2008 at 10:32:56AM -0700, Rich Shepard wrote: > I cannot see what's causing the error I see, but it must be something > simple that I've overlooked. Your more experienced eyes will probably see > it. > > Here's the for loop: > > for i in range(1, compList[0][16]): > pylab.hold(True) > if compList[0][4] == 'Decay S-Curve': > testFunctions.zCurve(compList[0][10],compList[0][9]) > elif compList[0][4] == 'Bell Curve': > testFunctions.gaussCurve(compList[0][14],compList[0][14]) > elif compList[0][4] == 'Growth S-Curve': > testFunctions.sCurve(compList[0][8],compList[0][11]) > elif compList[0][4] == 'Beta': > testFunctions.betaCurve(compList[0][13],compList[0][12],compList[0][14]) > elif compList[0][4] == 'Data': > continue > elif compList[0][4] == 'Linear Increasing': > testFunctions.linearIncrCurve(compList[0][8],compList[0][11]) > elif compList[0][4] == 'Linear Decreasing': > testFunctions.linearDecrCurve(compList[0][10],compList[0][9]) > elif compList[0][4] == 'Left Shoulder': > testFunctions.leftShoulderCurve(compList[0][10],compList[0][11],compList[0][9]) > continue > elif compList[0][4] == 'Trapezoid': > testFunctions.trapezoidCurve(compList[0][8],compList[0][10],compList[0][11],compList[0][9]) > continue > elif compList[0][4] == 'Right Shoulder': > testFunctions.rightShoulderCurve(compList[0][8],compList[0][10],compList[0][11]) > continue > elif compList[0][4] == 'Triangle': > testFunctions.triangleCurve(compList[0][8],compList[0][13],compList[0][9]) > elif compList[0][4] == 'Singleton': > testFunctions.singletonCurve(compList[0][13],compList[0][14]) > elif compList[0][4] == 'Rectangle': > testFunctions.rectangleCurve(compList[0][8],compList[0][10],compList[0][11],compList[0][9]) > elif compList[0][4] == 'Outcome': > testFunctions.outcomeCurve() > pylab.savefig(curVar+'.png') > > The first 'i' passes the first 'if' test and control passes to > testFunctions.zCurve(). When that function returns, the program is NOT at the > 'for' statement again, but at one earlier in the code. perhaps I'm missing something but I'm not seeing where the i variable is used in the loop block. > > So, instead of incrementing i and running through the if tests (which > should take it to the one for 'Growth S-Curve', it repeats the same plotting > function call then immediately jumps to pylab.savefig(). > > Why? not referencing i anywhere ? --mgross > > I can provide the test code with data and the ancillary module with the > plotting functions, but you'd need NumPy and Matplotlib installed to run it. > > TIA, > > Rich -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: Digital signature Url : http://mail.python.org/pipermail/portland/attachments/20080320/cfad6b28/attachment.pgp From rshepard at appl-ecosys.com Fri Mar 21 14:17:46 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 06:17:46 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: <20080321023802.GA9354@thegnar.org> References: <20080321023802.GA9354@thegnar.org> Message-ID: On Thu, 20 Mar 2008, mark gross wrote: > perhaps I'm missing something but I'm not seeing where the i variable > is used in the loop block. Mark, I went off in the wrong direction with this function from the start, and I need to re-write it using list comprehension. What I need to do is cycle through different items in the tuples in a nested fashion. That is: For each level-1 name in the list of tuples: For each level-2 name in each level-1 name: For each level-3 name in each level-2 name: For each level-4 name in each level-3 name3: Call the appropriate plotting function based on curve shape. At my level of python experience this is proving to be a major challenge that has taken my attention for well too much time. For those of you with more experience the algorithm implementation is probably simple and obvious. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Fri Mar 21 14:46:28 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 06:46:28 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: Note: my recommendation at the last PPUG, among a smattering of I hoped useful comments, was for Rich to publish to this list a black box unit test we could simply cut and paste and help out with the missing part. def testfuction(td): missing part: what we help with includes print statements testdata = (whatever tuples) testfunction(testdata) Printed output: Plot 1: do function A with args a,b,c do function B with args d,e,f .... Plot 2: do function C with args a,b,c do function B with args d,e,f .... I'm hazy on the printed output part, because I'm still confused about what Rich is squeezing from his data input sponge -- that's what I have a hard time abstracting from any recent post. The printed output of testfunction (presumably a loop of some kind) should emulate calling each of those plots, some superimposed on the same set of axes (that's an important wrinkle, I get that -- would be easy if each graph had its own set of axes). If the output correctly mirrors what's needed to satisfy the plotting functions, then we have the basis for a useful/relevant unit test. I would suggest that the data look deliberately phony, not deliberately realistic, e.g. ('term1', 'name1', 'a', 'b', 'c', 'xx', 'yy', 'zz') or whatever, but correctly mirror the underlying input, i.e. the patterns should be there. Doesn't have to be a *lot* of data, maybe 20 tuples? The key is to use these same strings in the ouput, so the testfunction (a kind of mapping) can do its thing. I don't think this is "busy work" by today's criteria as a lot of us believe in "writing tests first" e.g. you make little tests like this as a basis for building up code, and you keep the tests in the module, run them again and again, any time you make changes. Note that *not* included in the above is any discussion of nested for loops, as that's all about implementation (a black box). We only care about input and output. Nor is there any discussion of how "I tried this but got this output instead of that output" i.e. there's no debugging of any attempted solution. As assistants to Rich, our job would be to generate his output exactly (to the letter), from his input (cutting and pasting a valid data structure). If that is successful, then Rich would have his solution -- provided the input and output were crafted appropriately (which is the challenge at this point). Yes. he's done something *like* this a number of times, but not quite simply enough I don't think, when it comes to the printed output especially. Kirby On Fri, Mar 21, 2008 at 6:17 AM, Rich Shepard wrote: > On Thu, 20 Mar 2008, mark gross wrote: > > > perhaps I'm missing something but I'm not seeing where the i variable > > is used in the loop block. > > Mark, > > I went off in the wrong direction with this function from the start, and I > need to re-write it using list comprehension. > > What I need to do is cycle through different items in the tuples in a > nested fashion. That is: > > For each level-1 name in the list of tuples: > For each level-2 name in each level-1 name: > For each level-3 name in each level-2 name: > For each level-4 name in each level-3 name3: > Call the appropriate plotting function based on curve shape. > > At my level of python experience this is proving to be a major challenge > that has taken my attention for well too much time. For those of you with > more experience the algorithm implementation is probably simple and obvious. > > > Rich > > -- > Richard B. Shepard, Ph.D. | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: 503-667-8863 > _______________________________________________ > > > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From rshepard at appl-ecosys.com Fri Mar 21 16:32:43 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 08:32:43 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Fri, 21 Mar 2008, kirby urner wrote: > Note: my recommendation at the last PPUG, among a smattering of I hoped > useful comments, was for Rich to publish to this list a black box unit > test we could simply cut and paste and help out with the missing part. Kirby, I posted the code and data, but that required NumPy and Matplotlib. Attached is a stripped down version (only 5 tuples in the lis) that prints instead of plots. (The plotting code is commented out; instead of printing the curve shape I will call the appropriate function.) It's stand-alone and runs. The output is: Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: Variety Decay S-Curve Ending this plot ... Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: Variety Growth S-Curve Ending this plot ... Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Decay S-Curve Ending this plot ... Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Bell Curve Ending this plot ... Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Growth S-Curve Ending this plot ... What I'm trying to see is output like this: Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: Variety Decay S-Curve Growth S-Curve Ending this plot ... Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Decay S-Curve Bell Curve Growth S-Curve Ending this plot ... I hope this makes it easy for you! Thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 -------------- next part -------------- #!/usr/bin/env python import os, config #, testFunctions #import matplotlib #import matplotlib.numerix as nx #import pylab varData = [("Low","Variety","Fish","Wildlife","Decay S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2), ("High","Variety","Fish","Wildlife","Growth S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2), ("Low","SpecialConcern","Fish","Wildlife","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), ("Moderate","SpecialConcern","Fish","Wildlife","Bell Curve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,50.0,1.0,3), ("Many","SpecialConcern","Fish","Wildlife","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)] def testCode(): for row in varData: compList = [] curComp = row[3] if row[3] == curComp: compList.append(row) subList = [] curSub = compList[0][2] if compList[0][3] == curComp: subList.append(compList[0][2]) varList = [] curVar = compList[0][1] print "Starting a new plot ..." print "Component: ", curComp, "Subcomponent: ", curSub, "Parent: ", curVar if compList[0][1] == curVar: varList.append(compList[0][1]) print '\t', compList[0][4] print "Ending this plot ...", '\n' """ for item in row: print curVar pylab.hold(True) if row[0][4] == 'Decay S-Curve': testFunctions.zCurve(row[0][10],row[0][9]) elif row[0][4] == 'Bell Curve': testFunctions.gaussCurve(row[0][14],row[0][14]) elif row[0][4] == 'Grow[0]th S-Curve': testFunctions.sCurve(row[0][8],row[0][11]) elif row[0][4] == 'Beta': testFunctions.betaCurve(row[0][13],row[0][12],row[0][14]) elif row[0][4] == 'Data': continue elif row[0][4] == 'Linear Increasing': testFunctions.linearIncrCurve(row[0][8],row[0][11]) elif row[0][4] == 'Linear Decreasing': testFunctions.linearDecrCurve(row[0][10],row[0][9]) elif row[0][4] == 'Left Shoulder': testFunctions.leftShoulderCurve(row[0][10],row[0][11],row[0][9]) elif row[0][4] == 'Trapezoid': testFunctions.trapezoidCurve(row[0][8],row[0][10],row[0][11],row[0][9]) elif row[0][4] == 'Right Shoulder': testFunctions.rightShoulderCurve(row[0][8],row[0][10],row[0][11]) elif row[0][4] == 'Triangle': testFunctions.triangleCurve(row[0][8],row[0][13],row[0][9]) elif row[0][4] == 'Singleton': testFunctions.singletonCurve(row[0][13],row[0][14]) elif row[0][4] == 'Rectangle': testFunctions.rectangleCurve(row[0][8],row[0][10],row[0][11],row[0][9]) elif row[0][4] == 'Outcome': testFunctions.outcomeCurve() pylab.savefig(curVar+'.png') print "Wrote ", curVar, item, row[0][16] pylab.hold(False) """ if __name__ == "__main__": testCode() From kirby.urner at gmail.com Fri Mar 21 18:53:20 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 10:53:20 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: OK Rich, so just to be sure: This is the input: varData = [("Low","Variety","Fish","Wildlife","Decay S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2), ("High","Variety","Fish","Wildlife","Growth S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2), ("Low","SpecialConcern","Fish","Wildlife","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), ("Moderate","SpecialConcern","Fish","Wildlife","Bell Curve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,50.0,1.0,3), ("Many","SpecialConcern","Fish","Wildlife","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)] And this is the desired output: Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: Variety Decay S-Curve Growth S-Curve Ending this plot ... Starting a new plot ... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Decay S-Curve Bell Curve Growth S-Curve Ending this plot ... > I hope this makes it easy for you! I think so. Back to you shortly. Thanks for helping out, good team work. Kirby From rshepard at appl-ecosys.com Fri Mar 21 19:47:23 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 11:47:23 -0700 (PDT) Subject: [portland] Fwd: Need Help With a For Loop In-Reply-To: <4c645a720803211140i5dd78edck32b346d739706ad0@mail.gmail.com> References: <20080321023802.GA9354@thegnar.org> <4c645a720803211140i5dd78edck32b346d739706ad0@mail.gmail.com> Message-ID: On Fri, 21 Mar 2008, Dylan Reinhardt wrote: > Looks like I may have managed not to send this to the list. :-) > This makes no sense... Does row[3] change in-place after you assign to > curComp? Looks like you're testing the value you just set... Probably not > what you meant to do. Dylan, No, the value doesn't change. As I wrote, I dug myself into a deep rut and cannot see over the rim to a better way of coding it all. Part of the missing code puts the appropriate data items in the ReportLab structure so they're printed in a nested hierarchy. That's why the redundancy is there. I've tried to construct list comprehensions to do the job, but still know too little about them to fit them to my need. However, from what I'm reading, they'd be the way to go because they run much faster and the data set (in this testing sample) is > 360 tuples. In an actual situation there may well be more data. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Fri Mar 21 20:19:04 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 12:19:04 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: OK, so here's my solution, using only basic Python: varData = [ ('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low', 'SpecialConcern', 'Fish', 'Wildlife', '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), ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many', 'SpecialConcern', 'Fish', 'Wildlife', '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)] def testfunction(thelist): def header(): print "Starting a new plot..." print "Component: %s Subcomponent: %s Parent: %s" \ % (component, subcomponent, parent) def footer(): print "Ending this plot\n" ranked = sorted(thelist,key=lambda x: x[3]) ranked = sorted(thelist,key=lambda x: x[2]) ranked = sorted(thelist,key=lambda x: x[1]) starting = True for row in ranked: if starting: component = row[3] subcomponent = row[2] parent = row[1] header() starting = False if (component == row[3] and subcomponent == row[2] and parent == row[1]): print "\t%s" % row[4] else: footer() component = row[3] subcomponent = row[2] parent = row[1] header() print "\t%s" % row[4] footer() def test(): print varData print "---------" testfunction(varData) ========= Interactively: >>> reload(richproject) >>> richproject.test() [('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low', 'SpecialConcern', 'Fish', 'Wildlife', '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), ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many', 'SpecialConcern', 'Fish', 'Wildlife', '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)] --------- Starting a new plot... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Decay S-Curve Bell Curve Growth S-Curve Ending this plot Starting a new plot... Component: Wildlife Subcomponent: Fish Parent: Variety Decay S-Curve Growth S-Curve Ending this plot On Fri, Mar 21, 2008 at 10:53 AM, kirby urner wrote: > OK Rich, so just to be sure: > > This is the input: > > > varData = [("Low","Variety","Fish","Wildlife","Decay > S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2), > ("High","Variety","Fish","Wildlife","Growth > S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2), > ("Low","SpecialConcern","Fish","Wildlife","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), > ("Moderate","SpecialConcern","Fish","Wildlife","Bell > Curve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,50.0,1.0,3), > ("Many","SpecialConcern","Fish","Wildlife","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)] > > And this is the desired output: > > > Starting a new plot ... > Component: Wildlife Subcomponent: Fish Parent: Variety > Decay S-Curve > Growth S-Curve > Ending this plot ... > > Starting a new plot ... > Component: Wildlife Subcomponent: Fish Parent: SpecialConcern > Decay S-Curve > Bell Curve > Growth S-Curve > Ending this plot ... > > > I hope this makes it easy for you! > > I think so. Back to you shortly. > > Thanks for helping out, good team work. > > Kirby > From rshepard at appl-ecosys.com Fri Mar 21 20:32:52 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 12:32:52 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Fri, 21 Mar 2008, kirby urner wrote: > OK, so here's my solution, using only basic Python: Thank you, Kirby. I'll work on this and see if I can translate it to the actual, more extensive function. The list, varData, is sorted after being selected from the database tables. Sort order is component, subcomponent, parent, plot sequence number. Perhaps I headed off in the wrong direction because I was trying to use the already sorted tuples rather than sorting them as needed. Probably be tomorrow before I've thoroughly explored this. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Fri Mar 21 20:40:56 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 15:40:56 -0400 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: Hi Rich, I didn't go as far as plot sequence number, but that could be added with one more line of code inserted after the existing lines: ranked = sorted(ranked, key=little lambda with the right key number). The plots would then just naturally be in that sequence, no other code needed (although when plots get superimposed, I'm not sure what "sequence" even means -- but that's your department, no need to explain). Kirby On Fri, Mar 21, 2008 at 3:32 PM, Rich Shepard wrote: > On Fri, 21 Mar 2008, kirby urner wrote: > > > > OK, so here's my solution, using only basic Python: > > Thank you, Kirby. I'll work on this and see if I can translate it to the > actual, more extensive function. > > The list, varData, is sorted after being selected from the database > tables. Sort order is component, subcomponent, parent, plot sequence number. > Perhaps I headed off in the wrong direction because I was trying to use the > already sorted tuples rather than sorting them as needed. > > Probably be tomorrow before I've thoroughly explored this. > > > Rich > > -- > Richard B. Shepard, Ph.D. | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: 503-667-8863 > > > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From kirby.urner at gmail.com Fri Mar 21 20:45:57 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 12:45:57 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Fri, Mar 21, 2008 at 12:40 PM, kirby urner wrote: > Hi Rich, > > I didn't go as far as plot sequence number, but that could be added > with one more line of code inserted after the existing lines: > > ranked = sorted(ranked, key=little lambda with the right key number). > Now wait, this is wrong... I'm screwing up with this sorting thing. Hold on... Kirby From kirby.urner at gmail.com Fri Mar 21 20:52:50 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 12:52:50 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: OK, sorry, I was messing up: here's the real answer. If your SQL is able to retrieve the rows in the needed order (which SQL is good at) then you don't need these lines of code: def primarykey(x): return (x[3],x[2],x[1],x[5]) ranked = sorted(thelist,key=primarykey) Keep everything else the same and I think it'll work? Kirby >>> reload(richproject) >>> richproject.test() [('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low', 'SpecialConcern', 'Fish', 'Wildlife', '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), ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many', 'SpecialConcern', 'Fish', 'Wildlife', '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)] --------- ('Wildlife', 'Fish', 'Variety', 1) ('Wildlife', 'Fish', 'Variety', 2) ('Wildlife', 'Fish', 'SpecialConcern', 1) ('Wildlife', 'Fish', 'SpecialConcern', 2) ('Wildlife', 'Fish', 'SpecialConcern', 3) Starting a new plot... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Decay S-Curve Bell Curve Growth S-Curve Ending this plot Starting a new plot... Component: Wildlife Subcomponent: Fish Parent: Variety Decay S-Curve Growth S-Curve Ending this plot =============== richproject.py ================= varData = [ ('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low', 'SpecialConcern', 'Fish', 'Wildlife', '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), ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many', 'SpecialConcern', 'Fish', 'Wildlife', '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)] def testfunction(thelist): def header(): print "Starting a new plot..." print "Component: %s Subcomponent: %s Parent: %s" \ % (component, subcomponent, parent) def footer(): print "Ending this plot\n" def primarykey(x): return (x[3],x[2],x[1],x[5]) ranked = sorted(thelist,key=primarykey) for i in thelist: print (i[3],i[2],i[1],i[5]) starting = True for row in ranked: if starting: component = row[3] subcomponent = row[2] parent = row[1] header() starting = False if (component == row[3] and subcomponent == row[2] and parent == row[1]): print "\t%s" % row[4] else: footer() component = row[3] subcomponent = row[2] parent = row[1] header() print "\t%s" % row[4] footer() def test(): print varData print "---------" testfunction(varData) On Fri, Mar 21, 2008 at 12:45 PM, kirby urner wrote: > On Fri, Mar 21, 2008 at 12:40 PM, kirby urner wrote: > > Hi Rich, > > > > I didn't go as far as plot sequence number, but that could be added > > with one more line of code inserted after the existing lines: > > > > ranked = sorted(ranked, key=little lambda with the right key number). > > > > Now wait, this is wrong... > > I'm screwing up with this sorting thing. Hold on... > > Kirby > From jek at discorporate.us Fri Mar 21 20:58:06 2008 From: jek at discorporate.us (jason kirtland) Date: Fri, 21 Mar 2008 12:58:06 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: <47E4134E.5010904@discorporate.us> And here's one for Python 2.5: def testfunction(rows): """Plots a sequence of data rows. [(impact, parent, subcategory, category, plot type, sequence, data...), ... ] """ from itertools import groupby from operator import itemgetter ranked = sorted(rows, key=itemgetter(3, 2, 1, 5)) grouping_iterator = groupby(ranked, key=itemgetter(3, 2, 1)) for (component, subcomponent, parent), grouped in grouping_iterator: print "Starting a new plot .." print "Component: %s Subcomponent: %s Parent: %s" % ( component, subcomponent, parent) for row in grouped: plot_type, data = row[4], row[6:] print "\t%s" % plot_type print "Ending this plot ..." testfunction(varData) kirby urner wrote: > OK, so here's my solution, using only basic Python: > > varData = [ > ('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', > 1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), > ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', > 2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), > ('Low', 'SpecialConcern', 'Fish', 'Wildlife', '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), > ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', > 2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), > ('Many', 'SpecialConcern', 'Fish', 'Wildlife', '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)] > > def testfunction(thelist): > > def header(): > print "Starting a new plot..." > print "Component: %s Subcomponent: %s Parent: %s" \ > % (component, subcomponent, parent) > > def footer(): > print "Ending this plot\n" > > ranked = sorted(thelist,key=lambda x: x[3]) > ranked = sorted(thelist,key=lambda x: x[2]) > ranked = sorted(thelist,key=lambda x: x[1]) > > starting = True > > for row in ranked: > > if starting: > component = row[3] > subcomponent = row[2] > parent = row[1] > header() > starting = False > > if (component == row[3] > and subcomponent == row[2] > and parent == row[1]): > > print "\t%s" % row[4] > > else: > > footer() > > component = row[3] > subcomponent = row[2] > parent = row[1] > > header() > print "\t%s" % row[4] > > > footer() > > def test(): > print varData > print "---------" > testfunction(varData) > > ========= > Interactively: > >>>> reload(richproject) > >>>> richproject.test() > [('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, > 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High', > 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0, > 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low', > 'SpecialConcern', 'Fish', 'Wildlife', '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), ('Moderate', > 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0, > 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many', > 'SpecialConcern', 'Fish', 'Wildlife', '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)] > --------- > Starting a new plot... > Component: Wildlife Subcomponent: Fish Parent: SpecialConcern > Decay S-Curve > Bell Curve > Growth S-Curve > Ending this plot > > Starting a new plot... > Component: Wildlife Subcomponent: Fish Parent: Variety > Decay S-Curve > Growth S-Curve > Ending this plot > > > On Fri, Mar 21, 2008 at 10:53 AM, kirby urner wrote: >> OK Rich, so just to be sure: >> >> This is the input: >> >> >> varData = [("Low","Variety","Fish","Wildlife","Decay >> S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2), >> ("High","Variety","Fish","Wildlife","Growth >> S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2), >> ("Low","SpecialConcern","Fish","Wildlife","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), >> ("Moderate","SpecialConcern","Fish","Wildlife","Bell >> Curve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,50.0,1.0,3), >> ("Many","SpecialConcern","Fish","Wildlife","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)] >> >> And this is the desired output: >> >> >> Starting a new plot ... >> Component: Wildlife Subcomponent: Fish Parent: Variety >> Decay S-Curve >> Growth S-Curve >> Ending this plot ... >> >> Starting a new plot ... >> Component: Wildlife Subcomponent: Fish Parent: SpecialConcern >> Decay S-Curve >> Bell Curve >> Growth S-Curve >> Ending this plot ... >> >> > I hope this makes it easy for you! >> >> I think so. Back to you shortly. >> >> Thanks for helping out, good team work. >> >> Kirby >> > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From kirby.urner at gmail.com Fri Mar 21 21:01:15 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 13:01:15 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: Shoot, I'm still doing something wrong. Why don't my primary key tuples sort the way I think they should?? Damn. Gotta run an errand, then I'm gonna solve this! Kirby On Fri, Mar 21, 2008 at 12:52 PM, kirby urner wrote: > OK, sorry, I was messing up: here's the real answer. > > If your SQL is able to retrieve the rows in the needed > order (which SQL is good at) then you don't need > these lines of code: > > def primarykey(x): > return (x[3],x[2],x[1],x[5]) > > ranked = sorted(thelist,key=primarykey) > > Keep everything else the same and I think it'll work? > > Kirby From kirby.urner at gmail.com Fri Mar 21 21:07:19 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 13:07:19 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: Arrgh, it was the *debugging* code that was borked, my sorting solution was correct after all. def primarykey(x): return (x[3],x[2],x[1],x[5]) ranked = sorted(thelist,key=primarykey) for i in ranked: # was iterating through presorted list before print (i[3],i[2],i[1],i[4],i[5]) Anyway, Jason's 2.5 solution is cooler. OK, gotta run... Kirby from random import shuffle varData = [ ('Low', 'Variety', 'Fish', 'Wildlife', 'Decay S-Curve', 1, 0.0, 100.0, 0.0, 50.0, 0.0, 50.0, 50.0, 50.0, 100.0, 1.0, 2), ('High', 'Variety', 'Fish', 'Wildlife', 'Growth S-Curve', 2, 0.0, 100.0, 0.0, 50.0, 0.0, 100.0, 50.0, 50.0, 100.0, 1.0, 2), ('Low', 'SpecialConcern', 'Fish', 'Wildlife', '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), ('Moderate', 'SpecialConcern', 'Fish', 'Wildlife', 'Bell Curve', 2, 0.0, 100.0, 0.0, 100.0, 0.0, 50.0, 50.0, 50.0, 50.0, 1.0, 3), ('Many', 'SpecialConcern', 'Fish', 'Wildlife', '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)] def testfunction(thelist): shuffle(thelist) def header(): print "Starting a new plot..." print "Component: %s Subcomponent: %s Parent: %s" \ % (component, subcomponent, parent) def footer(): print "Ending this plot\n" def primarykey(x): return (x[3],x[2],x[1],x[5]) ranked = sorted(thelist,key=primarykey) for i in ranked: print (i[3],i[2],i[1],i[4],i[5]) starting = True for row in ranked: if starting: component = row[3] subcomponent = row[2] parent = row[1] header() starting = False if (component == row[3] and subcomponent == row[2] and parent == row[1]): print "\t%s" % row[4] else: footer() component = row[3] subcomponent = row[2] parent = row[1] header() print "\t%s" % row[4] footer() def test(): print varData print "---------" testfunction(varData) --------- ('Wildlife', 'Fish', 'SpecialConcern', 'Decay S-Curve', 1) ('Wildlife', 'Fish', 'SpecialConcern', 'Bell Curve', 2) ('Wildlife', 'Fish', 'SpecialConcern', 'Growth S-Curve', 3) ('Wildlife', 'Fish', 'Variety', 'Decay S-Curve', 1) ('Wildlife', 'Fish', 'Variety', 'Growth S-Curve', 2) Starting a new plot... Component: Wildlife Subcomponent: Fish Parent: SpecialConcern Decay S-Curve Bell Curve Growth S-Curve Ending this plot Starting a new plot... Component: Wildlife Subcomponent: Fish Parent: Variety Decay S-Curve Growth S-Curve Ending this plot On Fri, Mar 21, 2008 at 1:01 PM, kirby urner wrote: > Shoot, I'm still doing something wrong. Why don't my primary > key tuples sort the way I think they should?? > > Damn. > > Gotta run an errand, then I'm gonna solve this! > > Kirby > > > > > On Fri, Mar 21, 2008 at 12:52 PM, kirby urner wrote: > > OK, sorry, I was messing up: here's the real answer. > > > > If your SQL is able to retrieve the rows in the needed > > order (which SQL is good at) then you don't need > > these lines of code: > > > > def primarykey(x): > > return (x[3],x[2],x[1],x[5]) > > > > ranked = sorted(thelist,key=primarykey) > > > > Keep everything else the same and I think it'll work? > > > > Kirby > From rshepard at appl-ecosys.com Fri Mar 21 22:09:24 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 14:09:24 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Fri, 21 Mar 2008, kirby urner wrote: > Now wait, this is wrong... > > I'm screwing up with this sorting thing. Hold on... Kirby, Now I don't feel so bad. :-) I can cope with sorting a list, and selecting elements based related to a given value of another element, but this nested recursion has been difficult. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Fri Mar 21 22:14:24 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 14:14:24 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: <47E4134E.5010904@discorporate.us> References: <20080321023802.GA9354@thegnar.org> <47E4134E.5010904@discorporate.us> Message-ID: On Fri, 21 Mar 2008, jason kirtland wrote: > And here's one for Python 2.5: Thank you. I have 2.5 on my notebook, but there's an incompatibility with matplotlib that I need to resolve before I upgrade my workstation. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Fri Mar 21 22:22:35 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 14:22:35 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: On Fri, Mar 21, 2008 at 2:09 PM, Rich Shepard wrote: > On Fri, 21 Mar 2008, kirby urner wrote: > > > > Now wait, this is wrong... > > > > I'm screwing up with this sorting thing. Hold on... > > Kirby, > > Now I don't feel so bad. :-) > Yeah, no reason to. > I can cope with sorting a list, and selecting elements based related to a > given value of another element, but this nested recursion has been > difficult. > > > > Rich > The happy ending is my basic Python solution was correct and no nested recursion is necessary. Kirby From jek at discorporate.us Fri Mar 21 23:17:27 2008 From: jek at discorporate.us (jason kirtland) Date: Fri, 21 Mar 2008 15:17:27 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> <47E4134E.5010904@discorporate.us> Message-ID: <47E433F7.5060300@discorporate.us> Rich Shepard wrote: > On Fri, 21 Mar 2008, jason kirtland wrote: > >> And here's one for Python 2.5: > > Thank you. > > I have 2.5 on my notebook, but there's an incompatibility with matplotlib > that I need to resolve before I upgrade my workstation. For the record, the "2.5" part of my ditty is the 'itemgetter(x, y, z)'. Prior to 2.5, itemgetter only accepted a single argument. You can do the same thing in older Pythons with a function: 'lambda row: row[x], row[y], row[z]'. On the topic of iterator-based solutions, I really enjoyed Jim Baker's "More Iterators In Action" talk at PyCon this year: http://us.pycon.org/2008/conference/schedule/event/75/ Rumor has it that the PSF will post video of all of this year's talks, so you'll be able to catch Jim's session if you missed it. (And Kirby's and my sessions too.) And there is audio of Jim's original "Iterators in Action" talk from last year available in the PyCon 2007 podcasts: http://advocacy.python.org/podcasts/ -j From kirby.urner at gmail.com Sat Mar 22 00:19:03 2008 From: kirby.urner at gmail.com (kirby urner) Date: Fri, 21 Mar 2008 19:19:03 -0400 Subject: [portland] Need Help With a For Loop In-Reply-To: <47E433F7.5060300@discorporate.us> References: <20080321023802.GA9354@thegnar.org> <47E4134E.5010904@discorporate.us> <47E433F7.5060300@discorporate.us> Message-ID: Jason wrote: > On the topic of iterator-based solutions, I really enjoyed Jim Baker's > "More Iterators In Action" talk at PyCon this year: > > http://us.pycon.org/2008/conference/schedule/event/75/ > Plus iterators in general moving into the foreground with Python 3.x, in that many built-ins that used to return lists, like range and adict.keys() will now return iterables, which are more like generators. Exploring in Python 3.0 a2: IDLE 3.0a2 >>> t = {"and":None,"now":None,"for":None,"something":None,"completely":None,"different":None}.keys() >>> type(t) # Python 2.x you'd get back a list type here >>> next(t) # oops, can't pretend it's a generator yet Traceback (most recent call last): File "", line 1, in next(t) TypeError: dict_keys object is not an iterator >>> oops = iter(t) >>> next(oops) # ah, now we can 'and' >>> help(iter) Help on built-in function iter in module builtins: iter(...) iter(collection) -> iterator iter(callable, sentinel) -> iterator Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence. In the second form, the callable is called until it returns the sentinel. >>> dir(t) # note: no __next__ method... ['__and__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__str__', '__sub__', '__xor__'] >>> type(oops) # ...you'll find it'll have one > Rumor has it that the PSF will post video of all of this year's talks, > so you'll be able to catch Jim's session if you missed it. (And Kirby's > and my sessions too.) I walked away from the podium a lot, camera not tracking. I'm fortunate to be getting some post production services from Ian Benson's sociality.tv -- presumably a slicker version of my talk is in the pipeline (looking forward to seeing). Wanna see Jim's and Jason's. I also recommend the Unicode talk when it comes out too, was just praising it on edu-sig: http://farmdev.com/talks/unicode/ > > And there is audio of Jim's original "Iterators in Action" talk from > last year available in the PyCon 2007 podcasts: > > http://advocacy.python.org/podcasts/ > Muchas gracias! Kirby > -j > > > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From rshepard at appl-ecosys.com Sat Mar 22 01:20:27 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Fri, 21 Mar 2008 17:20:27 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Fri, 21 Mar 2008, kirby urner wrote: > OK, so here's my solution, using only basic Python: > starting = True ... > starting = False Kirby, The flag was the key. I've used flags for control in C code so I don't know why it did not occur to me to use one in the Python code. But, it didn't. I cleaned up the code significantly, Dylan, and now have to fix some problems with the plotting functions and matplotlib itself. But, all you lifted me out of the rut, and I greatly appreciate it. The working code is attached. Carpe weekend, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 -------------- next part -------------- #!/usr/bin/env python import os, config, functions import matplotlib import matplotlib.numerix as nx import pylab varData = [("Low","Variety","Fish","Wildlife","Decay S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2), ("High","Variety","Fish","Wildlife","Growth S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2), ("Low","HabitatComplexity","Terrestrial","Wildlife","Decay S-Curve",1,0.0,100.0,0.0,50.0,0.0,50.0,50.0,50.0,100.0,1.0,2), ("High","HabitatComplexity","Terrestrial","Wildlife","Growth S-Curve",2,0.0,100.0,0.0,50.0,0.0,100.0,50.0,50.0,100.0,1.0,2), ("Low","Variety","Terrestrial","Wildlife","Decay S-Curve",1,50.0,50.0,0.0,100.0,0.0,0.0,50.0,50.0,100.0,1.0,2), ("High","Variety","Terrestrial","Wildlife","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), ("Low","HabitatComplexity","Fish","Wildlife","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), ("High","HabitatComplexity","Fish","Wildlife","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), ("Low","SpecialConcern","Fish","Wildlife","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), ("Moderate","SpecialConcern","Fish","Wildlife","Bell Curve",2,0.0,100.0,0.0,100.0,0.0,50.0,50.0,50.0,50.0,1.0,3), ("Many","SpecialConcern","Fish","Wildlife","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)] def testCode(): for row in varData: curComp = row[3] curSub = row[2] curVar = row[1] plotting = False plotting = True if plotting: pylab.hold(True) if row[4] == 'Decay S-Curve': functions.zCurve(row[10],row[9]) elif row[4] == 'Bell Curve': functions.gaussCurve(row[13],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]) elif row[4] == 'Data': continue elif row[4] == 'Linear Increasing': functions.linearIncrCurve(row[8],row[11]) elif row[4] == 'Linear Decreasing': functions.linearDecrCurve(row[10],row[9]) elif row[4] == 'Left Shoulder': functions.leftShoulderCurve(row[10],row[11],row[9]) elif row[4] == 'Trapezoid': functions.trapezoidCurve(row[8],row[10],row[11],row[9]) elif row[4] == 'Right Shoulder': functions.rightShoulderCurve(row[8],row[10],row[11]) elif row[4] == 'Triangle': functions.triangleCurve(row[8],row[13],row[9]) elif row[4] == 'Singleton': functions.singletonCurve(row[13],row[14]) elif row[4] == 'Rectangle': functions.rectangleCurve(row[8],row[10],row[11],row[9]) elif row[4] == 'Outcome': functions.outcomeCurve() print "Current curve: ", row[5], '\t', "Max. curves: ", row[16] if row[5] == row[16]: plotting = False pylab.savefig(curVar+'.png') pylab.hold(False) if __name__ == "__main__": testCode() From rshepard at appl-ecosys.com Sat Mar 22 21:43:17 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Sat, 22 Mar 2008 13:43:17 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: Kirby told me he didn't understand why each curve was not plotted separately. I've attached a couple of examples to illustrate the output. I still need to tweak the plotting code to change the shape of the plot, remove the frame but use the left and bottom axes, and add text to each one. But, this gives you the idea. Thanks for all the help. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 -------------- next part -------------- A non-text attachment was scrubbed... Name: SpecialConcern_Fish_Wildlife.png Type: image/png Size: 42289 bytes Desc: Url : http://mail.python.org/pipermail/portland/attachments/20080322/7d8b749e/attachment.png -------------- next part -------------- A non-text attachment was scrubbed... Name: Variety_Terrestrial_Wildlife.png Type: image/png Size: 31998 bytes Desc: Url : http://mail.python.org/pipermail/portland/attachments/20080322/7d8b749e/attachment-0001.png From kirby.urner at gmail.com Mon Mar 24 19:29:41 2008 From: kirby.urner at gmail.com (kirby urner) Date: Mon, 24 Mar 2008 11:29:41 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: Maybe someday Kirby will understand why varieties of wild fish have a smoothly varied "membership grade" in a "universe of discourse" -- maybe more than my wee brain can handle of Rich's big Python project on a day like today (slow moving). And now for something completely different: check out these proposed insignia for EuroPython, votes being counted as we speak (I'm not voting, given my outside observer status): http://www.europython.org/community/Planning/ProposedLogo Kirby On Sat, Mar 22, 2008 at 1:43 PM, Rich Shepard wrote: > Kirby told me he didn't understand why each curve was not plotted > separately. I've attached a couple of examples to illustrate the output. I > still need to tweak the plotting code to change the shape of the plot, > remove the frame but use the left and bottom axes, and add text to each one. > But, this gives you the idea. > > Thanks for all the help. > > > Rich > > -- > Richard B. Shepard, Ph.D. | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: 503-667-8863 > -------------- next part -------------- > > A non-text attachment was scrubbed... > Name: SpecialConcern_Fish_Wildlife.png > Type: image/png > Size: 42289 bytes > Desc: > Url : http://mail.python.org/pipermail/portland/attachments/20080322/7d8b749e/attachment.png > > -------------- next part -------------- > A non-text attachment was scrubbed... > Name: Variety_Terrestrial_Wildlife.png > Type: image/png > Size: 31998 bytes > Desc: > Url : http://mail.python.org/pipermail/portland/attachments/20080322/7d8b749e/attachment-0001.png > > > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From rshepard at appl-ecosys.com Mon Mar 24 19:38:09 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 24 Mar 2008 11:38:09 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Mon, 24 Mar 2008, kirby urner wrote: > Maybe someday Kirby will understand why varieties of wild fish have a > smoothly varied "membership grade" in a "universe of discourse" -- maybe > more than my wee brain can handle of Rich's big Python project on a day > like today (slow moving). Because "varieties" is a ratio number with a true zero and equal increments up to the maximum numbers. The UoD (Universe of Discourse) is the range of numbers (from zero to whatever), and the term sets represent variables such as "few" and "many." Incremental changes in the numbers cause incremental changes in the degree of membership of that number in the set of "few varieties" and/or "many varieties." Since you have the time, read up on set theory. :-) Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Mon Mar 24 19:41:03 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 24 Mar 2008 11:41:03 -0700 (PDT) Subject: [portland] Need Help With a For Loop In-Reply-To: References: <20080321023802.GA9354@thegnar.org> Message-ID: On Mon, 24 Mar 2008, kirby urner wrote: > check out these proposed insignia for EuroPython, votes being counted as > we speak (I'm not voting, given my outside observer status): > > http://www.europython.org/community/Planning/ProposedLogo Considering what's happening in the EU, perhaps the logo should be two pythons eating each other. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From kirby.urner at gmail.com Mon Mar 24 19:44:40 2008 From: kirby.urner at gmail.com (kirby urner) Date: Mon, 24 Mar 2008 11:44:40 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: > Considering what's happening in the EU, perhaps the logo should be two > pythons eating each other. > > > Rich Why, what's happening in the EU? Master of set theory I already am, but of wildlife fish, still learning (a lot from Jim Buxton, the watershed management guy in my blogs). Used to work for Xerces Society, pre-Python (butterflies), when Melody was CEO. Kirby > > -- > Richard B. Shepard, Ph.D. | Integrity Credibility > Applied Ecosystem Services, Inc. | Innovation > Voice: 503-667-4517 Fax: 503-667-8863 > > > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From jeff at taupro.com Mon Mar 24 22:21:20 2008 From: jeff at taupro.com (Jeff Rush) Date: Mon, 24 Mar 2008 16:21:20 -0500 Subject: [portland] Need Help With a For Loop In-Reply-To: References: Message-ID: <47E81B50.7070306@taupro.com> kirby urner wrote: >> Considering what's happening in the EU, perhaps the logo should be two >> pythons eating each other. > > Why, what's happening in the EU? I would like to know this as well. The EuroPython conference appears to be struggling and Guido has asked if there is anything those with con experience or the PSF can do to assist. But I've not been able to figure out what the challenges for EuroPython are and how it differs from PyCon USA. -Jeff From kirby.urner at gmail.com Mon Mar 24 22:38:35 2008 From: kirby.urner at gmail.com (kirby urner) Date: Mon, 24 Mar 2008 14:38:35 -0700 Subject: [portland] Need Help With a For Loop In-Reply-To: <47E81B50.7070306@taupro.com> References: <47E81B50.7070306@taupro.com> Message-ID: Based on my exposure at the last Europython, there were issues with Indigo, built to serve CERN, but a fish out of water in other venues, hard to just mine using freehand SQL, which is what some veterans feel good about, all GUIs aside. Understood Django was being looked at, with Laura making the good point that a website first and foremost needs to serve the organizers, serving attenders a side effect, but also a net effect, as organizer message traffic is pretty wide open (as with Pycon, which I think has generated a lot of useful feedback). We all become organizers at least vicariously, though not the the degree of a Barcamp (only been to two of those, one in alpha version). POV supplied a perfectly competent wireless experience, in addition to Reval Hotel's, in contrast to Pycon's (not faulting the organizers, as 1K geeks is a lot more, plus all those XOs wanted to chatter) -- had to wing it for my talk, but was adequately prepared for this contingency (copies in circulation, not on YouTube yet I don't think). The current EuroPython venue is very sophisticated about conferences: http://www.revalhotels.com/en/Hotels/Lithuania/Reval-Hotel-Lietuva/Conferences Good to have you aboard Jeff. Are you in Portland too then? We should meet someday, have coffee or whatever, Kirby 4D On Mon, Mar 24, 2008 at 2:21 PM, Jeff Rush wrote: > kirby urner wrote: > >> Considering what's happening in the EU, perhaps the logo should be two > >> pythons eating each other. > > > > > Why, what's happening in the EU? > > I would like to know this as well. The EuroPython conference appears to be > struggling and Guido has asked if there is anything those with con experience > or the PSF can do to assist. But I've not been able to figure out what the > challenges for EuroPython are and how it differs from PyCon USA. > > -Jeff > > > > _______________________________________________ > Portland mailing list > Portland at python.org > http://mail.python.org/mailman/listinfo/portland > From jeff at taupro.com Tue Mar 25 03:55:37 2008 From: jeff at taupro.com (Jeff Rush) Date: Mon, 24 Mar 2008 21:55:37 -0500 Subject: [portland] Need Help With a For Loop In-Reply-To: References: <47E81B50.7070306@taupro.com> Message-ID: <47E869A9.5090200@taupro.com> kirby urner wrote: > > Good to have you aboard Jeff. Are you in Portland too then? No, sorry I'm still in Dallas. I'm just subscribed to a few of the usergroup mailing lists, to keep in touch with the needs of the communities and help out where I can. I try not to post too much, since it really is for local folks. > We should meet someday, have coffee or whatever, I kept trying to meet up with you at PyCon but it never worked out. Maybe next year or a EuroPython con. -Jeff From kirby.urner at gmail.com Tue Mar 25 23:46:06 2008 From: kirby.urner at gmail.com (kirby urner) Date: Tue, 25 Mar 2008 15:46:06 -0700 Subject: [portland] Pycon 2008 #53 on Google Video Message-ID: FYI ya'll, I've got my Pycon talk on-line (28 mins): http://worldgame.blogspot.com/2008/03/my-talk-in-chicago.html (blog-embedded). Or click through to: http://video.google.com/videoplay?docid=4007622124508476663&hl=en I'm finding some others in the related links, cool. Kirby From jek at discorporate.us Mon Mar 31 22:31:54 2008 From: jek at discorporate.us (jason kirtland) Date: Mon, 31 Mar 2008 13:31:54 -0700 Subject: [portland] Pycon 2008 #53 on Google Video In-Reply-To: References: Message-ID: <47F14A3A.4010304@discorporate.us> kirby urner wrote: > FYI ya'll, I've got my Pycon talk on-line (28 mins): > > http://worldgame.blogspot.com/2008/03/my-talk-in-chicago.html > > (blog-embedded). > > Or click through to: > > http://video.google.com/videoplay?docid=4007622124508476663&hl=en > > I'm finding some others in the related links, cool. Great talk! For some reason my workstation didn't like the Google video, but the talk popped up in the PyCon2008 YouTube feed today and I watched it there: http://www.youtube.com/watch?v=hbeHdg8mtdc http://www.youtube.com/pycon08 Also, have you checked out the "Reinteract" shell? http://fishsoup.net/software/reinteract/ I watched the screencast and it seems pretty neat, especially for math. A little Reinteract show and tell would make a nice addition to the lineup for the April meeting, if someone wanted to... ;-) -j From rshepard at appl-ecosys.com Mon Mar 31 23:08:27 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 31 Mar 2008 14:08:27 -0700 (PDT) Subject: [portland] zip() tutorial needed Message-ID: Not too long ago when I tried to specify x, y pairs to plot data, python threw the exception of too many pairs to unpack. My original code and the error was: x,y = [(15.0, 0.0), (30.0, 1.0), (70.0, 1.0), (85.0, 0.0)] ValueError: too many values to unpack Someone on the matplotlib mail list wrote, You are looking for the classic "unzip" trick: x,y = zip(*[(15.0, 0.0), (30.0, 1.0), (70.0, 1.0), (85.0, 0.0)]) And sure enough, that solved the problem. However, I apparently don't understand it well enough to apply it to a similar situation. Reading the Python doc on built-in functions doesn't help me. I read, "zip( [iterable, ...]) This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list. New in version 2.0. "Changed in version 2.4: Formerly, zip() required at least one argument and zip() raised a TypeError instead of returning an empty list." Which tells me that I should have a result that looks like my original list of tuples. I think this is where I'm confused about what zip() is doing. Now, using a different plotting library, I'm running into the same error, but the same solution is not working for me. A curve-generating function in one model writes the x and y values to separate lists in the common values module for the application. If I 'print config.appData.plotX' and 'print config.appData.plotY', each list is printed in succession. The values in each list are separated by spaces. When I try to pass these lists to the plotting function I get an error (which is specific to that library). So I tried unzipping the two lists but I still get the error: X, Y = zip(*[config.appData.plotX, config.appData.plotY]) ValueError: too many values to unpack So, I'd appreciate learning more about what zip() does, and why it's not giving me a list of x, y values. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From rshepard at appl-ecosys.com Mon Mar 31 23:19:29 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 31 Mar 2008 14:19:29 -0700 (PDT) Subject: [portland] zip() tutorial needed In-Reply-To: References: Message-ID: On Mon, 31 Mar 2008, Rich Shepard wrote: > A curve-generating function in one model writes the x and y values to ^^^^^ That should be 'module'. > When I try to pass these lists to the plotting function In a third module Obviously, this has me greatly confused all day. Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863 From python at dylanreinhardt.com Mon Mar 31 23:39:31 2008 From: python at dylanreinhardt.com (Dylan Reinhardt) Date: Mon, 31 Mar 2008 14:39:31 -0700 Subject: [portland] zip() tutorial needed In-Reply-To: References: Message-ID: <4c645a720803311439g192b1341ga4a90f7515d95c2@mail.gmail.com> On Mon, Mar 31, 2008 at 2:08 PM, Rich Shepard wrote: > Not too long ago when I tried to specify x, y pairs to plot data, python > threw the exception of too many pairs to unpack. My original code and the > error was: > > x,y = [(15.0, 0.0), (30.0, 1.0), (70.0, 1.0), (85.0, 0.0)] > ValueError: too many values to unpack > > Someone on the matplotlib mail list wrote, > > You are looking for the classic "unzip" trick: > x,y = zip(*[(15.0, 0.0), (30.0, 1.0), (70.0, 1.0), (85.0, 0.0)]) Your immediate problem is that your list is a *collection* of two-element tuples. You are trying to unpack the *list* and what you want to do is unpack each *element*. Ex: mylist = [(15.0, 0.0), (30.0, 1.0), (70.0, 1.0), (85.0, 0.0)] for x,y in mylist: doSomething(x) doSomething(y) I'm not a huge fan of zip... I think it tends to make code less readable, but YMMV. HTH, Dylan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/portland/attachments/20080331/1c71fe0d/attachment.htm From rshepard at appl-ecosys.com Mon Mar 31 23:58:57 2008 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Mon, 31 Mar 2008 14:58:57 -0700 (PDT) Subject: [portland] zip() tutorial needed In-Reply-To: <4c645a720803311439g192b1341ga4a90f7515d95c2@mail.gmail.com> References: <4c645a720803311439g192b1341ga4a90f7515d95c2@mail.gmail.com> Message-ID: On Mon, 31 Mar 2008, Dylan Reinhardt wrote: > Your immediate problem is that your list is a *collection* of two-element > tuples. You are trying to unpack the *list* and what you want to do is > unpack each *element*. Ex: > > mylist = [(15.0, 0.0), (30.0, 1.0), (70.0, 1.0), (85.0, 0.0)] > for x,y in mylist: > doSomething(x) > doSomething(y) Dylan, That was the original problem, and your explanation makes it perfectly clear. Especially with the for loop showing an alternate approach. > I'm not a huge fan of zip... I think it tends to make code less readable, > but YMMV. Obviously, I'm not a fan of structures I don't understand. But, there's so much to Python and I'm learning as I code this application. Many thanks, Rich -- Richard B. Shepard, Ph.D. | Integrity Credibility Applied Ecosystem Services, Inc. | Innovation Voice: 503-667-4517 Fax: 503-667-8863