next up previous [pdf]

Next: Modules Up: Integrating Python with SCons Previous: Loops

Functions

Python functions are incredibly useful because you can compartmentalize repeated uses of commonly used Flows, and then use them in multiple SConstructs. For the best use of Python functions you should use the following conventions:

Here's an example of a well-defined function to create a Ricker wavelet with a peak frequency specified by the user:


\begin{verbatimtab}[4]
def ricker(out='wave',freq=20.0,kt=100,nt=1001,dt=0.01,ot...
...%(kt)d \vert
sfricker1 frequency=%(freq)f
''' % (locals())
\end{verbatimtab}

Notice that we use the python function locals() for string substitution. This function returns a dictionary that contains only the names and values of the named arguments for the function.

To call this function, you can use it as a normal Python function. Since there are default arguments, not all arguments need to be passed as long as you are OK with the default value.

ricker('wave',freq=30,kt=50)

If you are using a dictionary that has all of your variables in it, then you can call the function using explicit parameter fetching:

ricker('wave',parameters['freq'],parameters['kt'],...)

where you have to explicitly grab certain variables from the parameter dictionary. Conversely, you can use automatic parameter fetching:

ricker('wave',**parameters)

which will look for all the named arguments to the ricker function in the parameter dictionary, and send their values to the function. When there are many parameters, and you have already set them in the dictionary, then automatic parameter fetching is the best way to go.


next up previous [pdf]

Next: Modules Up: Integrating Python with SCons Previous: Loops

2011-11-03