HP Forums

Full Version: new user python on hp prime g2, casio cg50
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to get a simple hello world python program onto the prime g2.

I have read the post: "https://www.hpmuseum.org/forum/thread-16950.html""

The post mentions a DevKit. Where can I get it?

Also, once I get the program onto the prime, how do I launch/run it.

BTW: The casio cg-50 has upfront file, run, open, shell, ... commands
built into its micropython environment.

Thanks.
The python shell on the prime works fine as a calculator.
eg.
s = 100
a = 21
b = 5
c = a + b
t = s - c
t
c

===

I put the following on the cg50 which i can run on the casio
and from a windows cmd line shell via "python test0004.py".

# 20190704 casio cg50
# micro python
# print string array
# test0004.py
import math
j=3
a=['moe','larry','curly']
print (j)
print (a)
for j in range(3):
x=a[j]
print (x)

===

This blog post does not present the indentation of the python source code
of the last two lines, x=a[j], print(x).

===

Is there a way to put this program into the prime via the
prime hp connectivity kit and then run it from the prime?

Thanks.
(05-24-2021 03:52 PM)Liamtoh Resu Wrote: [ -> ]I put the following on the cg50 which i can run on the casio
and from a windows cmd line shell via "python test0004.py".

# 20190704 casio cg50
# micro python
# print string array
# test0004.py
import math
j=3
a=['moe','larry','curly']
print (j)
print (a)
for j in range(3):
x=a[j]
print (x)

===

This blog post does not present the indentation of the python source code
of the last two lines, x=a[j], print(x).

===

Is there a way to put this program into the prime via the
prime hp connectivity kit and then run it from the prime?

Thanks.

Yes, you can write the program like this:

Code:
#PYTHON(name)
# 20190704 HP Prime
# micro python
# print string array 
# test0004.py
import math
j=3
a=['moe','larry','curly']
print (j)
print (a)
for j in range(3):
  x=a[j]
  print (x)
#end
EXPORT test0004(name)
BEGIN
  PYTHON(name);
END;

Then copy it to the programs folder in the CK. You can run it like an HPPL program from the menu.

You can put your code between code tags (# in the list) to preserve the indentation.
Thanks Tom for your help.

I put the edited code into the prime, but I am still having problems.

I did the following.

) launched Python
) pressed <shift> <programs>
) selected test0004
) pressed the Run tab

The prime presented the yellow exclamation mark.

I double checked the source file.

Thanks again.
(05-24-2021 05:56 PM)Liamtoh Resu Wrote: [ -> ]Thanks Tom for your help.

I put the edited code into the prime, but I am still having problems.

I did the following.

) launched Python
) pressed <shift> <programs>
) selected test0004
) pressed the Run tab

The prime presented the yellow exclamation mark.

I double checked the source file.

Thanks again.

In my example, you don't launch Python. Go to the Functions Home screen then press Shift-1. Select test0004 from the menu and press enter. That's the same way as you launch a regular HPPL program.
I still can not get your edited code to run.

I reflashed the prime g2 with the 20210505 update--same result.
I have the revision D hardware.

Is there a subtle setting(s) change in the prime to be done to get it working?

Thanks.
(05-24-2021 09:46 PM)Liamtoh Resu Wrote: [ -> ]I still can not get your edited code to run.

I reflashed the prime g2 with the 20210505 update--same result.
I have the revision D hardware.

Is there a subtle setting(s) change in the prime to be done to get it working?

Thanks.

Oops! My fault. Try this:

Code:
#PYTHON name
# 20190704 HP Prime
# micro python
# print string array 
# test0004.py
import math
j=3
a=['moe','larry','curly']
print (j)
print (a)
for j in range(3):
  x=a[j]
  print (x)
#end
EXPORT test0004()
BEGIN
  PYTHON(name);
END;
And that finally worked!
Thank you very much.

Incidently, I am a new blog user.
I was wondering how to create the scrollable source code boxes and
how to put the quoted text boxes into white boxes on this blog site.

Thanks!
(05-25-2021 03:47 AM)Liamtoh Resu Wrote: [ -> ]..
I was wondering how to create the scrollable source code boxes and
how to put the quoted text boxes into white boxes on this blog site.
Please have a look at the attached pic.
Thanks for the tip on on the toolbar just below and extending to the right of the font and text size changers.

.
Following on this, is there a way to return the output from a Python script embedded in a PPL program to the stack?

For example, this program will print the output to the Terminal:
Code:

#PYTHON test
#test.py
import sys
 
def sqrx(x):
  return int(x)**2
   
y = sqrx(sys.argv[0])
print(y)

# Return the value of y to the stack? 

#end 
 
EXPORT PYTEST(a)
BEGIN
  PYTHON(test,a);
END;

Is there a way of returning 4 instead of 0 as shown in the screenshot?

[Image: AQgBXog.png]

-DrBarnack
There are several things going on there.

) The use of "y = sqrx(sys.argv[0])" rather than a standard python input statement.
I was surprised (positively) that an input form line was presented.

) The intended square was presented on the terminal screen rather instead of being
returned as a return value.

) If the python environment accepts the C-style input maybe there is a
C-style output statement.
I tweaked your program a bit. I don't know if this was your intent.

Code:

#PYTHON test2
#test2.py
# new test
import sys
def sqrx(x):
  return int(x)**2
y = sqrx(sys.argv[0])
print(y)
#end 
EXPORT PYTEST(a)
BEGIN
  PYTHON(test2,a);
END;


Sorry, I just condensed the white space. I was trying variations on the
prime g2 for a long while trying different variations.

I don't know if hp prime g2 prime python can return pointers to sys.arg[0].
(05-26-2021 06:28 AM)drbarnack Wrote: [ -> ]Following on this, is there a way to return the output from a Python script embedded in a PPL program to the stack?

For example, this program will print the output to the Terminal:
Code:

#PYTHON test
#test.py
import sys
 
def sqrx(x):
  return int(x)**2
   
y = sqrx(sys.argv[0])
print(y)

# Return the value of y to the stack? 

#end 
 
EXPORT PYTEST(a)
BEGIN
  PYTHON(test,a);
END;

-DrBarnack

I figured this out. The trick is to evaluate the output from the Terminal:

Code:

#PYTHON test
#test.py
import sys
 
def sqrx(x):
  """
  Square a number.
  """
  return int(x)**2

# get the number on the stack and square it
y = sqrx(sys.argv[0])
# print the result to the Terminal
print(y)

#end 
 
EXPORT PYTEST(a)
BEGIN
  \\ clear the Terminal
  PRINT;
  \\ run the Python script
  PYTHON(test,a);
  \\evaluate the string in the Terminal and return to the stack
  RETURN EXPR(TERMINAL(1));
END;

This takes a value from the stack ("sys.argv[0]"), does the calculation in Python, prints the answer to the Terminal, parses the first string in the Terminal into an expression and evaluates it, leaving the answer on the stack for future use ("RETURN EXPR(TERMINAL(1));"). So in Home or CAS mode I can use Python scripts like any other function in the Toolbox to manipulate values.
Editing text with the new 20210428 beta connectivity kit seems
to have solved the problem of sending extra characters
along with source code.

I had to install the 20210428 beta on top of the 2020 CK.
The beta software failed to run with an uninstall old software
and then installing the beta.

Clicking on the retro 3.5 disk icon saves the program onto the prime.

The 2020 CK would show a simpler text window with a prompt to save
the program.
At this point after 20 days of using the prime g2, I have decided to slog through
chapter 29 of the 2018 user guide ( just like I did with the K&R 30 years ago).

I am most fortunate to have a laser printer that does duplex printing.

There will be a PPL version of the small python program referred to in this thread.

Thanks again to Tom L and others.
@drbarnack

Quote:This takes a value from the stack ("sys.argv[0]"), does the calculation in Python, prints the answer to the Terminal, parses the first string in the Terminal into an expression and evaluates it, leaving the answer on the stack for future use ("RETURN EXPR(TERMINAL(1));"). So in Home or CAS mode I can use Python scripts like any other function in the Toolbox to manipulate values.

Excellent explanation! Kindly tell what is this Terminal(1) what is this variable?
Reference URL's