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; 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; 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; This way, it is also possible to call local PPL functions from Python. Example 4 Code: #PYTHON pycode 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 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)