[portland] Dictionary To Switch Contexts
Rich Shepard
rshepard at appl-ecosys.com
Mon Mar 17 21:28:16 CET 2008
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
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
More information about the Portland
mailing list