Jump to content
Developer Wiki and Function Reference Links ×

Indentation inside PythonExecute procedure


Recommended Posts

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!

Link to comment

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 by MullinRJ
Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...