[portland] Using Dictionary to Call Functions
Matt McCredie
mccredie at gmail.com
Tue Jan 15 02:59:11 CET 2008
> I've read in the Python Cookbook and various web sites the information on
> using a dictionary as a switch statement would be used in C. But, I've not
> seen -- or figured out -- how to call the keys correctly while passing
> arguments to the called function. Let me explain.
>
> The dictionary:
>
> curvePlot = {
> 'Decay S-Curve' : functions.zCurve(leftEdge, rightEdge),
> 'Bell Curve' : functions.gaussCurve(midpt, width),
> 'Growth S-Curve' : functions.sCurve(leftEdge, rightEdge),
> 'Beta' : functions.betaCurve(center, inflectPt, width),
> 'Data' : functions.dataCurve(ptList),
> 'Linear Increasing' : functions.linearIncrCurve(leftEdge, rightEdge),
> 'Linear Decreasing' : functions.linearDecrCurve(leftEdge, rightEdge),
> 'Left Shoulder' : functions.leftShoulderCurve(hiRight, loRight),
> 'Trapezoid' : functions.trapezoidCurve(loLeft, hiLeft, hiRight, loRight),
> 'Right Shoulder' : functions.rightShoulderCurve(loLeft, hiLeft, hiRight),
> 'Triangle' : functions.triangleCurve(loLeft, center, loRight),
> 'Singleton' : functions.singletonCurve(center, width),
> 'Rectangle' : functions.rectangleCurve(leftEdge, rightEdge),
> 'Outcome' : functions.outcomeCurve()
> }
>
> For each tuple in the list (from a database table) there is a value
> called 'shape.' This is the dictionary's key. The arguments are also values
> in each tuple, and they differ for each function call.
>
> I want to get the dictionary keys in sequential order (there may be 1 to
> 7), and call the appropriate function for each one. The functions use
> mathplotlib to draw the curves on a common set of axes.
I'm a little confused about what you are trying to do. What it sounds
like to me is that you have a list of tuples as follows:
[
('Decay S-Curve' ,leftEdge, rightEdge),
('Bell Curve', midpt, width),
('Growth S-Curve', leftEdge, rightEdge),
('Beta', inflectPt, width),
...
]
Or maybe it looks more like this:
[
('Decay S-Curve' , (leftEdge, rightEdge)),
('Bell Curve', (midpt, width)),
('Growth S-Curve', (leftEdge, rightEdge)),
('Beta', (inflectPt, width)),
...
]
And you want to call the functions that correspond to the shape names
with the values from the database. lets assume the list of names and
arguments is called funcs_to_call. Your code should look something
like this:
# Notice that I'm not actually calling the functions!
curvePlot = {
'Decay S-Curve' : functions.zCurve
'Bell Curve' : functions.gaussCurve
'Growth S-Curve' : functions.sCurve
'Beta' : functions.betaCurve
'Data' : functions.dataCurve
'Linear Increasing' : functions.linearIncrCurve
'Linear Decreasing' : functions.linearDecrCurve
'Left Shoulder' : functions.leftShoulderCurve
'Trapezoid' : functions.trapezoidCurve
'Right Shoulder' : functions.rightShoulderCurve
'Triangle' : functions.triangleCurve
'Singleton' : functions.singletonCurve
'Rectangle' : functions.rectangleCurve
'Outcome' : functions.outcomeCurve
}
# assuming that funcs_to_call is in the first form:
for shape_args in funcs_to_call:
shape = shape_args[0]
args = shape_args[1:]
curvePlot[shape](*args)
# assuming that funcs_to_call is in the second form:
for shape, args in funcs_to_call:
curvePlot[shape](*args)
HTH,
Matt
More information about the Portland
mailing list