The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Passing values from PPL to Python and vice versa - an alternative to AVars
12-03-2023, 09:39 PM (This post was last modified: 12-04-2023 07:26 AM by komame.)
Post: #1
Passing values from PPL to Python and vice versa - an alternative to AVars
During my tests of Python, I found a solution that allows passing values from Python to PPL without the need for using AVars. It enables the exchange of information between both environments even in simple hpprgm programs that use the PPL wrapper and I would like to share this with you.

This solution still utilizes hpprime.eval(), but instead of referring to AVars, it writes values directly into variables in the PPL program. However, for this to be possible, the variable on the PPL side must be local in the global scope of the program or exportable. In the case of a local variable, it is sufficient to declare it in the header of the PPL program (outside of any function).
It is very important to use a prefix with the PPL program's name when indicating PPL variables on the Python side (this should not be confused with the exported function name).

In the examples below, I assumed that the program that uses the PPL wrapper for Python is named "PYPPL" (and that's the prefix I mentioned).

Example 1
Code:
LOCAL pyresult;

#PYTHON pycode
from hpprime import eval as ppleval;
numbers = range(10)
ppleval('PYPPL.pyresult:=%s' %numbers);
#END

EXPORT PPL_PY() 
BEGIN
  PYTHON(pycode);
  RETURN pyresult;
END;

The result in "pyresult" variable:
[0 1 2 3 4 5 6 7 8 9]

Note that one of the known issues in Prime is the transfer of numerical values above three digits (more than 999) to Python as a parameter (via the argv[]) - this leads to a problem with garbage appearing in the value on the Python side. However, the methods described above, instead of passing the value, allow you to directly read on the Python side the content of a variable in which you put the value to be transferred. This allows you to pass any values regardless of their size.

Example 2
Code:
LOCAL value;

#PYTHON pycode
from hpprime import eval as ppleval
fromPPL = ppleval('PYPPL.value')
print('The value read in Python: %s' %fromPPL)
#END

EXPORT PPL_PY() 
BEGIN
  PRINT();
  value:=1234567890;
  PYTHON(pycode);
END;

The printed result:
The value read in Python: 1234567890.0

Additionally, you can even pass the name of a variable to Python (somewhat like simulating passing by reference) and read the contents of that variable on the Python side, then return the result back to that variable.

Example 3
Code:
LOCAL var1,var2;

// shuffle string
#PYTHON pycode
from sys import argv
from hpprime import eval as ppleval
from urandom import randint

def shuffle(string):
    char_list = list(string)
    length = len(char_list)
    for i in range(length):
        random_index = randint(0, length - 1)
        char_list[i], char_list[random_index] = char_list[random_index], char_list[i]
    return ''.join(char_list)

string = ppleval('PYPPL.%s' %argv[0])
shuffled = shuffle(string)
ppleval('PYPPL.%s := "%s"' %(argv[0],shuffled))
#END

EXPORT PPL_PY() 
BEGIN
  PRINT();
  var1:="FIRST";
  PYTHON(pycode,"var1");
  var2:="SECOND";
  PYTHON(pycode,"var2");
  RETURN {var1,var2}; // the results in the same variables
END;

This way, it is also possible to call local PPL functions from Python.

Example 4
Code:
#PYTHON pycode
from hpprime import eval as ppleval
from urandom import randint

n = randint(2,10);
print('random value by Python: %d' %n)
nxn = ppleval('PYPPL.PPL_SUBROUTINE(%d)' %n)
print('calculated by PPL subroutine: %d * %d = %d' %(n,n,nxn))
#END

EXPORT PPL_PY() 
BEGIN
  PRINT();
  PYTHON(pycode);
END;

PPL_SUBROUTINE(n)
BEGIN
  RETURN n*n;
END;

Even passing and receiving the result as a matrix works, because the form of the matrix after conversion to a string is the same in both languages.
In general, this eliminates some limitations associated with Python on the HP Prime and opens up some new possibilities.

I hope this information is useful.

Best wishes,
Piotr
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Passing values from PPL to Python and vice versa - an alternative to AVars - komame - 12-03-2023 09:39 PM



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