next up previous [pdf]

Next: Functions Up: Integrating Python with SCons Previous: Dictionaries

Loops

Perhaps the most useful construct from Python that can be added to SConstructs are loops. By using loops, we can automate many redundant processes. To use a Python loop we just use the standard Python syntax in the following fashion:
\begin{lstlisting}
from rsf.proj import *
\par
for i in range(10):
count = str(...
... n1=100 mag=%d' % i)
Plot('spike-'+count,'sfgraph')
\par
End()
\end{lstlisting}
In order for the loop to work, you need to make sure that all the files that are created have a unique file name. The easiest way to do so is to use the loop index in the filename, the count variable in this case. The count variable must be a string, because only strings can be concatenated together in Python. If you want to make more complicated file names (from nested loops) then examine the printf like syntax for Python strings.

All the usual Python rules apply to these loops. Typically, for loops are easier to understand than while loops in Python, and so we recommend using for loops for most purposes.




2011-11-03