Post Reply 
Passing values from PPL to Python and vice versa - an alternative to AVars
12-08-2023, 06:45 PM (This post was last modified: 12-08-2023 06:49 PM by komame.)
Post: #7
RE: Passing values from PPL to Python and vice versa - an alternative to AVars
Hi Günter,

I'm glad that you managed to make my examples work Smile
I noticed another additional benefit of using the approach I proposed, which concerns converting values to numbers. When you pass a numeric value as a parameter using the standard method in a PYTHON command, e.g., PYTHON(pycode, 12345.456), the argv[0] on the Python side is of the string type, not float. Furthermore, the content of argv[0] depends on the HSeparator settings and varies accordingly. For values higher than 999, you must use HSeparator=3 for calculations involving argv[0]. Without this, the values can't be converted to a number, as float(argv[0]) will raise a syntax error due to the digit grouping separator, leading to a failed conversion, even in the absence of the garbage. However, with my proposed method, a value of the float type is directly assigned to the Python variable, allowing for more HSeparator settings (0, 1, 2, 3, 9, 10), with each resulting in a correct number.

The following program demonstrates an attempt to convert the parameter passed in argv[0] to a number vs reading values directly from a PPL variable:
Code:
#pragma mode( separator(.,;) integer(h64) )
LOCAL value;

#PYTHON pycode1
from hpprime import eval as ppleval
value = ppleval('PYPPL.value')
print('\npycode1: ', value, type(value))
#END

#PYTHON pycode2
from sys import argv
value = argv[0];
print('pycode2: ', value, type(value))
try:
 flt = float(value)
 print('value: ', flt, end = '')
except ValueError:
 print('value: syntax error', end = '')
#END

EXPORT PPL_PY() 
BEGIN
  LOCAL hs;
  PRINT;
  FOR hs FROM 0 TO 3 DO
    HSeparator:=hs;
    value:=12345.456;
    PRINT("==> Separator: " + hs);
    PYTHON(pycode1);
    PYTHON(pycode2,value);
  END;
END;

   

Although some garbage appeared in certain iterations during the call using standard method, even without the garbage, the conversion would have failed due to digit grouping separators.
This indicates that reading values directly from a variable is much more flexible than passing them using the standard method.

regards,
Piotr
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-08-2023 06:45 PM



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