Post Reply 
Some Python Scripts to understand Variables Transfer
10-21-2022, 10:31 AM (This post was last modified: 10-27-2022 08:01 PM by Ioncubekh.)
Post: #1
Some Python Scripts to understand Variables Transfer
~~~ Transferring Python variables to / from PPL by Guenter Schink ~~~
https://www.hpmuseum.org/forum/thread-17833.html
https://www.hpmuseum.org/forum/thread-18...#pid160378

~~~ Using sys.argv[0] by drbarnack ~~~
https://www.hpmuseum.org/forum/thread-16...#pid148350

A script that produces x2 Matrix assigned data generated from python. Can be used in CAS for plotting multiple data in same graph like: plotlist(M1), plotlist(M2)
Code:

EXPORT ppl_mat(dt1, dt2)
BEGIN
PRINT;                        
// flushing HP's M1 & M2 matrix & inserting all data at once
M1:={}; M1:=dt1;
M2:={}; M2:=dt2;
END;
  
#PYTHON EXPORT ukmat()
#start

# for eval()
from hpprime import *
# for sin() cos() pi
from math import *
# for linspace range() doesnt allow noninteger step
from linalg import linspace

# Will return string {{..},{..},...,{..}}
def _2str(a,b):
   c=list(zip(a,b)) # unpacking syntax isn't supported =[*zip(a,b)]
   return str(c).replace('[','{').replace(']','}').replace('(','{').replace(')','}')
   
# some list comprehensions
uk_x=linspace(-3*pi,3*pi,100)
uk_y1=[cos(i) for i in uk_x]
uk_y2=[sin(i) for i in uk_x]
# Use custom function _2str to generate HP Matrix syntax
uk_y1=_2str(uk_x, uk_y1)
uk_y2=_2str(uk_x, uk_y2)

# PPL function will execute 
ttt=eval('ppl_mat('+uk_y1+','+uk_y2+')')  
print('DONE')

#end

Above can be modified to produce lists instead of Matrix; useful to study them in APPs related to Stats
Code:

EXPORT crtlst(dt1,dt2)
BEGIN
// Explanation in Py.PPL.Matrix.Plotlist.txt
PRINT;                        
L1:={}; L1:=dt1;
L2:={}; L2:=dt2;

END;
  
#PYTHON EXPORT ukvar()
#start

from hpprime import *
from math import *

uk_x=[i*2 for i in range(-10,10)]
uk_y=[cos(i) for i in uk_x]
uk_x=str(uk_x).replace('[','{').replace(']','}')
uk_y=str(uk_y).replace('[','{').replace(']','}')
ttt=eval('crtlst('+uk_x+','+uk_y+')')  
print('Done')

#end

A further implementation of python dict() that avoids excessive use of if else blocks. Perhaps a Python equivalent of PPL CHOOSE()

Code:

EXPORT passgrd(dt)
BEGIN 
  AVars("marks"):=dt;
  PYTHON(pygrd);                     
END;
  
#PYTHON EXPORT pygrd()
#start
from hpprime import *
# python dict() good alternative to CHOOSE()
grdlst = { 'A':range(90,100+1), 'B':range(80,90), 'C':range(70,80), 'D':range(60,70), 'F':range(0,60) }
marks=eval('AVars("marks")')
def _pygrd(arg1):
  for i in grdlst.keys():
    if arg1 in grdlst[i]:
      return i

grd=_pygrd(int(marks))

eval( 'AVars("grd"):='+'"'+grd+'"' )
eval( 'MSGBOX("Your Grade is: '+grd+'")' )

#end

HP Prime G1
Python via Android Termux...
Find all posts by this user
Quote this message in a reply
10-25-2022, 08:02 PM
Post: #2
RE: Some Python Scripts to understand Variables Transfer
I tried to program a python script with that syntax and I keep getting an Invalid Syntax error. I have hardware version 14603. I can get the rk44 to run but I can't write one myself.

Frustrating.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-25-2022, 08:27 PM (This post was last modified: 10-25-2022 08:28 PM by Ioncubekh.)
Post: #3
RE: Some Python Scripts to understand Variables Transfer
(10-25-2022 08:02 PM)Eddie W. Shore Wrote:  I tried to program a python script with that syntax and I keep getting an Invalid Syntax error. I have hardware version 14603. I can get the rk44 to run but I can't write one myself.

Frustrating.

Which syntax you're referring to?
I can run this rk44 python implementation; the function is hardcoded but it works; The iterations & answer stuff can be routed to Matrix / List variables
https://github.com/defencedog/HP-Prime-A...tfinal.txt

Do tell how do I use \t character of python tab is not understandable by HP terminal


Attached File(s) Thumbnail(s)
   

HP Prime G1
Python via Android Termux...
Find all posts by this user
Quote this message in a reply
10-26-2022, 02:04 AM (This post was last modified: 10-26-2022 02:06 AM by Eddie W. Shore.)
Post: #4
RE: Some Python Scripts to understand Variables Transfer
I found what was causing the error:

I typed "import math as *" when it should be just "from math import *".

Here is the code:

Code:
EXPORT VTEST()
BEGIN
PYTHON(vt);
END;

#PYTHON EXPORT vt()
#start
from math import *

# set up equations
y0=lambda x:x+1
y1=lambda x:x**2+1
y2=lambda x:log(x)

# in this mode all has to be typed
def ftab(a,b,n):
  h=(b-a)/n
  print("\nx \ty0 \ty1 \ty2")
  for i in range(n):
    f0=y0(a)
    f1=y1(a)
    f2=y2(a)
    print('%.4f\t%.4f\t%.4f\t%.4f'% (a,f0,f1,f2))
    a+=h

# inputs
a=float(input("low? "))
print(a)
b=float(input("\nhigh? "))
print(b)
s=int(input("\nsteps? "))
print(s)

ftab(a,b,s) 
  
#end

Observations:

1. Every time I changed the code, the HP rebooted. Fortunately the changes are retained.

2. Everything has to be typed out, and in the correct case. There are no command menus to help with the syntax. I guess we can use a blank Python app and then copy the code into a PPL program after testing?

3. Another print statement is required to show the user just entered after an input on the terminal. I find it quite annoying.

4. \t shows a box.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-26-2022, 03:08 AM (This post was last modified: 10-26-2022 03:25 AM by Ioncubekh.)
Post: #5
RE: Some Python Scripts to understand Variables Transfer
Quote:3. Another print statement is required to show the user just entered after an input on the terminal. I find it quite annoying.
Good observation thanks

Quote:4. \t shows a box.

Python chr(9) also doesn't work Sad
Alternative is
Code:
tab=4*chr(32)
print(tab,"how are you",tab)

REMEMBER: there is python built-in eval() & then there is hpprime eval() so always import hpprime lib as alias like
Code:
import hpprime as hp
hp.eval()

Also python fstring print not supported: https://realpython.com/python-f-strings/

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: 1 Guest(s)