Post Reply 
Passing values from PPL to Python and vice versa - an alternative to AVars
12-06-2023, 07:56 AM
Post: #5
RE: Passing values from PPL to Python and vice versa - an alternative to AVars
(12-05-2023 10:35 PM)Guenter Schink Wrote:  Sorry Piotr, I believe it doesn't. To verify this, you first have to delete the user variable "pyresult" because it certainly contains what's left over from the various checks you already have done.
In Home call the Vars Menu, select "User", "User Variables" and delete what's in there. Then run this program again.

Hi Günter

I still maintain that it works Smile Just test it on the emulator or on a real Prime. To convince you, I inform you that I restored the factory settings on my Prime, so I have no variables other than the built-in ones. Then I loaded this program, and everything still works as before.

The whole phenomenon of this solution lies in the fact that Prime, from the PPL level, can read local variables from other PPL programs, and they don't have to be of the EXPORT type. In the case of Python on Prime, I use this feature of the PPL language and simply read and write to/from local variables. Calling local functions works the same way. It's not well-documented, but it has been in PPL since the beginning.


(12-05-2023 10:35 PM)Guenter Schink Wrote:  Alternatively change "numbers= range(10)" to "numbers = range(7)" and check the result.

I can change the range value, and each time the list displays correctly.

(12-05-2023 10:35 PM)Guenter Schink Wrote:  In addition I think it's always wise to check the result of an hpprime.eval() statement. I.e change the line:
"ppleval('PYPPL.pyresult:=%s' %numbers);"
to:
"check=ppleval('PYPPL.pyresult:=%s' %numbers); "
and have a look what a consecutive line:
"print(check)" produces. It simply shows you that you have produced "Error: Syntax Error" in the Home environment.

The "print(check)" produces the same list (correctly).

(12-05-2023 10:35 PM)Guenter Schink Wrote:  A minor thing: It's superfluous to end a Python line with ";"

Yes, I'm aware of that. Just switching between programming languages like PPL, Python, C#, C++ or ABAP, I might inadvertently insert an extra character that is required in another language (but it's not a syntax error).

(12-05-2023 10:35 PM)Guenter Schink Wrote:  The fact that you always see the seemingly correct result is owed to the the variable "pyresult" never being changed during your various tests.

I didn't have a closer look to sample #4, but "print(nxn)" after "nxn= ppleval(...)" also reveals that PPL environment is not happy

The "PYPPL.pyresult" is a valid expression in PPL as long as there is a PYPPL program that contains a local variable (in the header, not inside a function) named "pyresult". In reality, there is no difference between LOCAL and EXPORT variables other than the fact that EXPORT appears in "User variables" and can be referenced directly by name, while LOCAL is not visible in "User variables" and requires a prefix with the program's name to reference.
Here's a simple example:

- create new program "PRG"
- enter:
Code:
LOCAL MYVAR:="ABC";

EXPORT PRG()
BEGIN
END;
- go to HOME
- enter:
PRG.MYVAR

and you see the "ABC" as a result.

Best wishes,
Piotr

Piotr Kowalewski
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Passing values from PPL to Python and vice versa - an alternative to AVars - komame - 12-06-2023 07:56 AM



User(s) browsing this thread: 1 Guest(s)