Jump to content
RSG

Handling Python indented blocks with ExecString()

Recommended Posts

I am using Python4Delphi in my app to allow users to incorporate Octave (open-source Matlab) scripts via Oct2Py - very cool capabilities here! While not central to what I am doing, I'm hoping to provide a sort of Python console similar to running an interactive Python session from a shell/command line. This is rather trivial for simple one-line code; simply pass the user-provided string to ExecString() and it works. However, if you try to do something like the following trivial code, passing each line to ExecString, the first two lines work, but the third throws an exception as shown below. 

image.thumb.png.b684c318f33de1bad204551bdf929f27.png

pi = 3.1415926
print(pi)
if pi > 0.0:
    print('pi is positive')
else:
	print('pi is negative')

It appears that ExecString really expects a single Python statement in one string. I suppose this isn't terribly surprising, so maybe my goal is not realizable without a lot of work, unless I am missing something that can make this possible. Any ideas?

Share this post


Link to post

Python code needs to be properly indented (see for instance Python Indentation (w3schools.com)).  Otherwise you will get the IndentationError you show above.

 

If you want to built your own interpreter, a good starting point is the InteractiveInterpeter class of the code module.  The runsource method will tell you whether the input is incomplete and more input is required.

Share this post


Link to post
10 hours ago, RSG said:

It appears that ExecString really expects a single Python statement in one string. I suppose this isn't terribly surprising, so maybe my goal is not realizable without a lot of work, unless I am missing something that can make this possible. Any ideas?

This would make Python users recoil in horror, but there are ways to turn Python code into single lines!

 

https://jagt.github.io/python-single-line-convert/

 

I don't know anything about Python4Delphi, but see if
 

exec("""\npi = 3.1415926\nprint(pi)\nif pi > 0.0:\n    print('pi is positive')\nelse:\n    print('pi is negative')\n""")

runs.

 

Maybe it could run without the exec if you turned the \n instances into #13s.

 

I don't know what your app is, but being able to use Octave scripts in it is pretty cool.

Share this post


Link to post
6 hours ago, Joseph MItzen said:

This would make Python users recoil in horror, but there are ways to turn Python code into single lines!

Not much use for a REPL

Share this post


Link to post

Is it really the best way to go delphi to python to octave? 

 

Must be possible to go straight to octave but far better to use python rather than octave/matlab. 

Share this post


Link to post
On 6/25/2023 at 4:51 AM, David Heffernan said:

Is it really the best way to go delphi to python to octave?

I don't know if it is or not, but I already use Python scripting elsewhere...

 

On 6/25/2023 at 4:51 AM, David Heffernan said:

Must be possible to go straight to octave

Maybe, but it isn't possible to go straight from Delphi to Matlab without a C "wrapper" DLL, so I wouldn't bet on it!!

 

On 6/25/2023 at 4:51 AM, David Heffernan said:

but far better to use python rather than octave/matlab. 

True, but I haven't been able to convince my client of that! They are scientists that use Matlab heavily, and are willing to go as far as Octave, but they refuse Python!

Share this post


Link to post
On 6/24/2023 at 4:02 PM, pyscripter said:

Python code needs to be properly indented (see for instance Python Indentation (w3schools.com)).  Otherwise you will get the IndentationError you show above.

 

If you want to built your own interpreter, a good starting point is the InteractiveInterpeter class of the code module.  The runsource method will tell you whether the input is incomplete and more input is required.

Of course, I understood the indentation requirement - sorry if I didn't make that clear.

 

That said, your other paragraph is much more to the point - thanks for the insight!

Edited by RSG

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×