Post Reply 
Sharing Variables between Python and HPppl
05-17-2022, 01:33 PM
Post: #1
Sharing Variables between Python and HPppl
How can I share a string variable between a Python program and a HPppl program?

Can I run a PPL program, from a Python program with pplname(var), if I had a ppl program called pplname, and had generated a variable var in the Python program?
Find all posts by this user
Quote this message in a reply
05-17-2022, 02:49 PM
Post: #2
RE: Sharing Variables between Python and HPppl
Does this:

from hpprime import *
eval("pplprogram(variables)")

do what you want?

-road
Find all posts by this user
Quote this message in a reply
05-17-2022, 03:26 PM (This post was last modified: 05-17-2022 03:37 PM by matalog.)
Post: #3
RE: Sharing Variables between Python and HPppl
It doesn't seem to.

Code:
 
from hpprime import *
#...............................
 return ans;
 # RQ7(ans);
  print("eval(RQ7(ans))").

ans is my final variable from the python program, but that above code (without # or not) doesn't print RQ7(ans) or run it, it takes a long time to run and I would have seen it.
Find all posts by this user
Quote this message in a reply
05-17-2022, 03:45 PM
Post: #4
RE: Sharing Variables between Python and HPppl
I think you will have to store ans as a prime variable first:

from hpprime import *
eval("primevariable:=" + str(ans))
eval("pplprogram(primevariable)")

see if that works.
Find all posts by this user
Quote this message in a reply
05-17-2022, 03:52 PM
Post: #5
RE: Sharing Variables between Python and HPppl
Or you could also do:

eval("pplprogram(" + str(ans) + ")")

if you don't want to create an extra variable in home.

-road
Find all posts by this user
Quote this message in a reply
05-17-2022, 11:34 PM
Post: #6
RE: Sharing Variables between Python and HPppl
Sorry, I'm not sure I am following your tips.

I have a Python program that creates a string variable called ans on the same calculator that I have a hpppl program that can process the rest of the things needed to complete.

If I can bring that ans variable to my HPPPL program then I will have done what I need to do.

Lets say that the Python variable comes from a program called py. The Program it will go to is called hp.

How can I get the variable ans, from py to hp?

As a failsafe, can you please tell me how I should refer to this variable in the HPPPL program?

Thanks for the help.
Find all posts by this user
Quote this message in a reply
05-18-2022, 01:21 AM (This post was last modified: 05-18-2022 01:44 AM by roadrunner.)
Post: #7
RE: Sharing Variables between Python and HPppl
Sorry, i did not realize ans was a string (even though your first post states it very clearly). That makes it a little more tricky, but:

from hpprime import *
eval("primevar:=\"" + ans + "\"")

should give you a home variable called primevar that contains whatever string is stored in ans.

Then you can run your ppl program with:

eval("pplprogram(primevar)")

-road
Find all posts by this user
Quote this message in a reply
05-19-2022, 03:43 PM
Post: #8
RE: Sharing Variables between Python and HPppl
(05-17-2022 01:33 PM)matalog Wrote:  How can I share a string variable between a Python program and a HPppl program?

Can I run a PPL program, from a Python program with pplname(var), if I had a ppl program called pplname, and had generated a variable var in the Python program?

No, you can't because PYTHON and HOME don't know anything of each other but ...

I do not pretend to really KNOW how these things are working. But from tinkering around I've got some findings that should help. Let me elaborate a bit on this.

About hpprime.eval():
it is important to understand that this function only passes on a string to the HOME environment which must be syntactically correct. Therefore you need to ensure the string is understood.
A statement like pplname(var) can't do anything because neither pplname nor var have any meaning except perhaps in their environment where they were created.

There are methods however to communicate between PYTHON and HOME environments, of which I'll explain the path from PYTHON to HOME

consider these two code snippets, first a PPL program
Code:
EXPORT test(arg1)
BEGIN
arg1:=arg1+" adding some text in PPL";
RETURN arg1;
END;
This program receives the variable <arg1> does something with it and returns what's now in arg1. Simply start it from the HOME screen with <test("abc") and see the result. Well you know that of course, but let's go step by step.

Let's now create a PYTHON program to see how to interface using hpprime.eval()
Code:
from hpprime import *
arg1="arg1: text"
ttt=eval('test('+'"'+arg1+'"'+')')
print(ttt)


Now a close look to the statement:
ttt=eval('test('+'"'+arg1+'"'+')')
ttt is the variable that receives what is returned from <test(arg1)>
eval calls this function with a rather complicated argument.
As explained earlier you have to provide a string that's understood by HOME. This is achieved by enclosing it in single quotes. But as you can see there's a lot of almost not discernible quoting. Step by step.
Code:
eval('test('     is starting the string
+ '"'              actually is ' " ' concatenating the double quotes to the string without the blanks
+ arg1          is concatenating the string that is in arg1, which is "arg1: text"
+ '"'             actually is ' " ' concatenating the double quote to the string without the blanks
')'              Concatenates the closing parentheses to the argument handed over to test () while the last parentheses simply closes the evaluate.

Sounds complicated, no? The double quotes enclosed in single quotes ensure that the content of arg1 is passed on to the home environment as a string, rather than as a variable name which doesn't exist and would raise an error

Conclusion: for using hpprime.eval() you have to create a string that's understood by the home environment. It is important to use single and double quotes as demonstrated in this example because HOME environment has a different interpretation of single quotes. Elsewhere I have already shown that a function:
Code:
def string(arg):
  return '"'+str(arg)+'"'

ttt=eval('test('+'"'+arg1+'"'+')')
would change to:
ttt=eval('test('+string(arg1) +')')
makes it less complicated.

If you got it wrong then ttt will respond with "Error: Syntax Error"

Now I'll have a break, but later I'll talk about using AVars which is another way to have PYTHON and HOME exchange contents. Stay tuned

HTH Günter
Find all posts by this user
Quote this message in a reply
05-19-2022, 07:40 PM (This post was last modified: 05-19-2022 07:44 PM by Guenter Schink.)
Post: #9
RE: Sharing Variables between Python and HPppl
Synchronizing vars in PYTHON and HOME

This can be done by using AVars. Create this PPL Program
Code:
EXPORT test(arg1)
BEGIN
arg1:=arg1+" this is the argument with addition by the PPL";
AVars("arg2"):=AVars("arg2")+" this is the AVars arg2 with addition by PPL";
RETURN {arg1,arg2};
END;
and then this PYTHON program
Code:
from hpprime import *
arg1="arg1 text"
arg2="arg2 text"
eval('AVars("arg2"):='+'"'+arg2+'"')
ttt=eval('test('+'"'+arg1+'"'+')')
eval('PRINT')
print(ttt[0],"\n",ttt[1])
like in the previous post the PYTHON program calls the PPL program with an argument. In addition to that it puts a value into AVars <arg2>. the PPL program does something with the given argument <arg1> and with the AVars<arg2> then it returns the results as a list to the PYTHON program where it may be processed further. In this program the two elements of the list are printed.

Günter
Find all posts by this user
Quote this message in a reply
10-20-2022, 06:57 AM
Post: #10
RE: Sharing Variables between Python and HPppl
Lucky me. I registered on this forum for this particular issue

HP Prime G1
Python via Android Termux...
Find all posts by this user
Quote this message in a reply
Post Reply 




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