AFDesign Posted August 8, 2023 Share Posted August 8, 2023 Hi everybody, I'm trying to use Python code inside Vectorscript using PythonExecute procedure. It works, but when I need to use indentation I receive this error: Quote IndentationError: expected an indented block I tried with different space counts, using tab, and with unicode \u0009 (tab) but nothing works. Indentation characters seems to be ignored. This is the sample code: procedure test_pythoncontext; VAR check:BOOLEAN; BEGIN check:=true; PythonBeginContext; PythonExecute('import vs'); PythonExecute('check_py = vs.GetVSVar("check")'); {INDENTATION CODE THAT GENERATE AN ERROR} PythonExecute('if check_py == true: '); PythonExecute(' check_py = false'); PythonEndContext; message(check); END; run(test_pythoncontext); Anybody can tell me if indentation is admitted in "PythonExecute"? Thanks! Quote Link to comment
MullinRJ Posted August 8, 2023 Share Posted August 8, 2023 TEST - 403 FORBIDDEN message. Quote Link to comment
MullinRJ Posted August 8, 2023 Share Posted August 8, 2023 OK, I can post text, but not code. How long will this last? Quote Link to comment
Pat Stanford Posted August 8, 2023 Share Posted August 8, 2023 The moderators and Juan are aware of the problem. Not certain how long this will take. Apparently there are some key words that effect the parser. CH_R being one of them. Not certain which other ones. We are trying to get an upgrade to the forum software scheduled. 1 Quote Link to comment
MullinRJ Posted August 8, 2023 Share Posted August 8, 2023 (edited) Well, with Pat's input, I'll try again. 😉 ************ Short answer, YES. Python uses indentation, even when submitted from Vectorscript. I typically build my Python code in a text block, then execute it all at once. However, in your simple case you can do this, which works. It avoids the need for indentation. Not an answer, but more of a crutch. PROCEDURE test_pythoncontext; CONST Tb = c h r(9); { remove extra spaces before running code } CR = c h r(13); { remove extra spaces before running code } VAR check :BOOLEAN; BEGIN check := true; PythonBeginContext; PythonExecute('import vs'); PythonExecute('check_py = vs.GetVSVar("check")'); PythonExecute('if check_py: check_py = False'); PythonExecute('vs.SetVSVar("check", check_py)'); PythonEndContext; message(check); END; Run(test_pythoncontext); OR, build everything ahead of time, then execute it like this: PROCEDURE test_pythoncontext; CONST Tb = c h r(9); { remove extra spaces before running code } CR = c h r(13); { remove extra spaces before running code } VAR check :BOOLEAN; PyCode :Dynarray of Char; BEGIN check := true; PyCode := concat('import vs', CR); PyCode := concat(PyCode, 'check_py = vs.GetVSVar("check")', CR); PyCode := concat(PyCode, 'if check_py:', CR); PyCode := concat(PyCode, Tb, 'check_py = False', CR); PyCode := concat(PyCode, 'vs.SetVSVar("check", check_py)', CR); PythonBeginContext; PythonExecute(PyCode); PythonEndContext; message(check); END; Run(test_pythoncontext); One benefit of building your python code this way is you can print your PyCode variable to examine it while you are debugging. For short snippets, use:AlrtDialog(PyCode); Or for longer snippets, use:MoveTo(0,0); CreateText(PyCode); When it looks correct, then you can execute it. HTH, Raymond Edited August 8, 2023 by MullinRJ Quote Link to comment
AFDesign Posted August 9, 2023 Author Share Posted August 9, 2023 Thank you so much Raymond! It would be great if the "PythonBeginContext" would allow to start writing Python code as "multi-line text", or even including a .py file directly as for .px files using "{$INCLUDE filename}". Kind regards Quote Link to comment
Pat Stanford Posted August 9, 2023 Share Posted August 9, 2023 I think you can use $Include to bring in a file into the BeginContext section. But I can't find the sample where I think I used that. 😞 Quote Link to comment
AFDesign Posted August 10, 2023 Author Share Posted August 10, 2023 This would be great, if the file can be a .py file written in standard Python language (without splitting code in lines). Thanks! Quote Link to comment
Pat Stanford Posted August 10, 2023 Share Posted August 10, 2023 I don't think it can be a standard PY file, but close. The include will need to be a long single quote delimited vectorscript string. So don't use single quotes in your PY code. Line breaks (CR) in the string should be fine. But single quotes at the beginning and end will be required. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.