Nested Program Blocks (Python)
From within Python, you can submit command syntax containing a BEGIN
PROGRAM
block, thus allowing you to nest program blocks.
This can be done by including the nested program block in a separate
command syntax file and submitting an INSERT
command
to read in the block. It can also be done by submitting the nested
program block from within a user-defined Python function.
Example: Nesting program blocks using the INSERT command
import spss
spss.Submit("INSERT FILE='/myprograms/nested_block.sps'.")
The file /myprograms/nested_block.sps would contain a BEGIN
PROGRAM
block, as in:
BEGIN PROGRAM PYTHON3.
import spss
<Python code>
END PROGRAM.
INSERT
command to insert a file
containing a program block. If you wish to encapsulate nested program blocks in a Python module that
can be imported, then embed the nesting code in a user-defined function as shown in the following
example.Example: Nesting program blocks with a user-defined Python function
import spss, myfuncs
myfuncs.demo()
-
myfuncs
is a user-defined Python module containing the function (demo
) that will submit the nested program block.A Python module is simply a text file containing Python definitions and statements. You can create a module with a Python IDE, or with any text editor, by saving a file with an extension of .py. The name of the file, without the .py extension, is then the name of the module.
- The
import
statement includesmyfuncs
so that it is loaded along with thespss
module. To be sure that Python can find your module, you may want to save it to your Python "site-packages" directory, typically /Python310/Lib/site-packages. - The code
myfuncs.demo()
calls the functiondemo
in themyfuncs
module.
Following is a sample of the contents of myfuncs
.
import spss
def demo():
spss.Submit("""
BEGIN PROGRAM PYTHON3.
<Python code>
END PROGRAM.""")
- The sample
myfuncs
module includes animport spss
statement. This is necessary since a function in the module makes use of a function from thespss
module--specifically, theSubmit
function. - The nested program block is contained within a Python triple-quoted string. Triple-quoted strings allow you to specify a block of commands on multiple lines, resembling the way you might normally write command syntax.
- Notice that
spss.Submit
is indented but theBEGIN PROGRAM
block is not. Python statements that form the body of a user-defined Python function must be indented. The level of indentation is arbitrary but must be the same for all statements in the function body. TheBEGIN PROGRAM
block is passed as a string argument to theSubmit
function and is processed by IBM® SPSS® Statistics as a block of Python statements. Python statements are not indented unless they are part of a group of statements, as in a function or class definition, a conditional expression, or a looping structure.
Notes
- You can have up to five levels of nesting.
- Python variables specified in a nested program block are local to that block unless they are specified as global variables. In addition, Python variables specified in a program block that invokes a nested block can be read, but not modified, in the nested block.
- Nested program blocks can be Python program blocks or R program blocks.
- If a
Submit
function containing a triple quoted string nests a Python program block containing another triple quoted string, use a different type of triple quotes in the nested block. For example, if the outer block uses triple double quotes, then use triple single quotes in the nested block.