Nested Program Blocks (Python) - IBM

文章推薦指數: 80 %
投票人數:10人

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 ... NestedProgramBlocks(Python) FromwithinPython,youcansubmitcommandsyntaxcontainingaBEGIN PROGRAMblock,thusallowingyoutonestprogramblocks. Thiscanbedonebyincludingthenestedprogramblockinaseparate commandsyntaxfileandsubmittinganINSERTcommand toreadintheblock.Itcanalsobedonebysubmittingthenested programblockfromwithinauser-definedPythonfunction. Example:NestingprogramblocksusingtheINSERTcommand importspss spss.Submit("INSERTFILE='/myprograms/nested_block.sps'.") Thefile/myprograms/nested_block.spswouldcontainaBEGIN PROGRAMblock,asin: BEGINPROGRAMPYTHON3. importspss ENDPROGRAM. Note:YoucannotimportaPythonmodulecontainingcodethatnestsaprogram block,suchastheabovecodethatusestheINSERTcommandtoinsertafile containingaprogramblock.IfyouwishtoencapsulatenestedprogramblocksinaPythonmodulethat canbeimported,thenembedthenestingcodeinauser-definedfunctionasshowninthefollowing example. Example:Nestingprogramblockswithauser-definedPythonfunction importspss,myfuncs myfuncs.demo() myfuncsisauser-definedPythonmodulecontainingthefunction (demo)thatwillsubmitthenestedprogramblock.APython moduleissimplyatextfilecontainingPythondefinitionsandstatements.Youcancreateamodule withaPythonIDE,orwithanytexteditor,bysavingafilewithanextensionof.py.The nameofthefile,withoutthe.pyextension,isthenthenameofthemodule. Theimportstatementincludesmyfuncsso thatitisloadedalongwiththespssmodule.TobesurethatPythoncanfindyour module,youmaywanttosaveittoyourPython"site-packages"directory,typically/Python39/Lib/site-packages. Thecodemyfuncs.demo()callsthefunction demointhemyfuncsmodule. Followingisasampleofthecontentsofmyfuncs. importspss defdemo(): spss.Submit(""" BEGINPROGRAMPYTHON3. ENDPROGRAM.""") Thesamplemyfuncsmoduleincludesanimport spssstatement.Thisisnecessarysinceafunctioninthemodulemakesuseofafunction fromthespssmodule--specifically,theSubmitfunction. ThenestedprogramblockiscontainedwithinaPythontriple-quotedstring. Triple-quotedstringsallowyoutospecifyablockofcommandsonmultiplelines,resemblingtheway youmightnormallywritecommandsyntax. Noticethatspss.SubmitisindentedbuttheBEGIN PROGRAMblockisnot.Pythonstatementsthatformthebodyofauser-definedPython functionmustbeindented.Thelevelofindentationisarbitrarybutmustbethesameforall statementsinthefunctionbody.TheBEGINPROGRAMblockispassedasastring argumenttotheSubmitfunctionandisprocessedbyIBM®SPSS®Statisticsasablockof Pythonstatements.Pythonstatementsarenotindentedunlesstheyarepartofagroupofstatements, asinafunctionorclassdefinition,aconditionalexpression,oraloopingstructure. Notes Youcanhaveuptofivelevelsofnesting. Pythonvariablesspecifiedinanestedprogramblockarelocaltothatblock unlesstheyarespecifiedasglobalvariables.Inaddition,Pythonvariablesspecifiedinaprogram blockthatinvokesanestedblockcanberead,butnotmodified,inthenestedblock. NestedprogramblockscanbePythonprogramblocksorRprogramblocks. IfaSubmitfunctioncontainingatriplequotedstring nestsaPythonprogramblockcontaininganothertriplequotedstring,useadifferenttypeoftriple quotesinthenestedblock.Forexample,iftheouterblockusestripledoublequotes,thenuse triplesinglequotesinthenestedblock. Parenttopic:WorkingwithPythonProgramBlocks Relatedinformation BasicSpecificationforaPythonProgramBlock UnicodeMode(Python)



請為這篇文章評分?