Post Reply 
How does python work?
09-06-2021, 04:37 PM
Post: #1
How does python work?
I recently upgraded my Prime to the latest firmware and I'm trying to understand how to write python functions/scripts. I didn't seem to find much a solution while perusing older threads.

Let's say my script looks like this:

PHP Code:
def func1(ab):
    return 
b

def func2
(ab):
    return 
b

def func3
(abc):
    if 
== 0:
        return 
func1(ab)
    else:
        return 
func2(ab

I know this is a contrived example, but I just want to know how to make it work. The question is, how do I call func3 ?
Find all posts by this user
Quote this message in a reply
09-06-2021, 04:53 PM
Post: #2
RE: How does python work?
This will work

Code:
def func1(a, b):
return a + b

def func2(a, b):
return a - b

def func3(a, b, c):
if c == 0:
return func1(a, b)
else:
return func2(a, b)

d=func3(4,2,0)
Find all posts by this user
Quote this message in a reply
09-06-2021, 05:01 PM
Post: #3
RE: How does python work?
(09-06-2021 04:53 PM)Dougggg Wrote:  This will work

Code:
def func1(a, b):
return a + b

def func2(a, b):
return a - b

def func3(a, b, c):
if c == 0:
return func1(a, b)
else:
return func2(a, b)

d=func3(4,2,0)

Maybe, I didn't explain properly. I am not looking to run the code from the python app. I am looking to run the code from the place where you do regular calculations. For example, if I was using python-syntax under XCas, I would do the following:

PHP Code:
#cas
def func1(ab):
    return 
b

def func2
(ab):
    return 
b

def func3
(abc):
    if 
== 0:
        return 
func1(ab)
    else:
        return 
func2(ab
#end 

I'm looking to do the same but with python, not XCas.
Find all posts by this user
Quote this message in a reply
09-06-2021, 08:15 PM (This post was last modified: 09-06-2021 08:16 PM by roadrunner.)
Post: #4
RE: How does python work?
Just paste your program exactly as written into a prime program file:

   

tap <check> and exit out. Typing func3(1,2,3) on the command line returns -1.

-road
Find all posts by this user
Quote this message in a reply
09-07-2021, 07:45 AM
Post: #5
RE: How does python work?
(09-06-2021 08:15 PM)roadrunner Wrote:  Just paste your program exactly as written into a prime program file:



tap <check> and exit out. Typing func3(1,2,3) on the command line returns -1.

-road

Yes, but this doesn't run the program as a python code. It runs it as an XCAS program. It just so happens that XCAS can accept some python syntax. What I am asking how to run python code, not XCAS with python-like syntax.
Find all posts by this user
Quote this message in a reply
09-07-2021, 12:06 PM
Post: #6
RE: How does python work?
Maybe the PYTHON command is what you are looking for?

Code:
#PYTHON test
import sys
from builtins import float
def func1(a, b):
    return a + b

def func2(a, b):
    return a - b

def func3(a, b, c):
    if c == 0:
        return func1(a, b)
    else:
        return func2(a, b)
print(func3(float(sys.argv[0]),float(sys.argv[1]),float(sys.argv[2])))
#end

EXPORT func3(a,b,c)
BEGIN
 PYTHON(test,a,b,c);
END;
Find all posts by this user
Quote this message in a reply
09-07-2021, 09:24 PM
Post: #7
RE: How does python work?
(09-07-2021 12:06 PM)roadrunner Wrote:  Maybe the PYTHON command is what you are looking for?

Code:
#PYTHON test
import sys
from builtins import float
def func1(a, b):
    return a + b

def func2(a, b):
    return a - b

def func3(a, b, c):
    if c == 0:
        return func1(a, b)
    else:
        return func2(a, b)
print(func3(float(sys.argv[0]),float(sys.argv[1]),float(sys.argv[2])))
#end

EXPORT func3(a,b,c)
BEGIN
 PYTHON(test,a,b,c);
END;

Thanks for this. But how do you display the result in the calculations window like regular functions so that they can be used. At the moment, the result only shows in the terminal.
Find all posts by this user
Quote this message in a reply
09-07-2021, 10:05 PM
Post: #8
RE: How does python work?
I think you would have to save it as a variable, then do what ever you want with the variable like in the following example:

PHP Code:
#PYTHON test
from sys import argv
from builtins import float
from cas import caseval

def func1
(ab):
 return 
b

def func2
(ab):
 return 
b

def func3
(abc):
 if 
== 0:
  return 
func1(ab)
 else:
  return 
func2(ab)

answer=func3(
 
float(argv[0]),
 
float(argv[1]),
 
float(argv[2]))

caseval(
 
"pythonanswer1698554:=" str(answer))

#end

EXPORT runtest(a,b,c)
BEGIN
 local d
;
 
PYTHON(test,a,b,c);
 
d:=CAS("pythonanswer1698554");
 
CAS("purge(pythonanswer1698554)");
 
d;
END

You can also save it as a home variable with hpprime.eval instead of cas.caseval.

-road
Find all posts by this user
Quote this message in a reply
09-08-2021, 10:48 AM
Post: #9
RE: How does python work?
(09-07-2021 10:05 PM)roadrunner Wrote:  I think you would have to save it as a variable, then do what ever you want with the variable like in the following example:

PHP Code:
#PYTHON test
from sys import argv
from builtins import float
from cas import caseval

def func1
(ab):
 return 
b

def func2
(ab):
 return 
b

def func3
(abc):
 if 
== 0:
  return 
func1(ab)
 else:
  return 
func2(ab)

answer=func3(
 
float(argv[0]),
 
float(argv[1]),
 
float(argv[2]))

caseval(
 
"pythonanswer1698554:=" str(answer))

#end

EXPORT runtest(a,b,c)
BEGIN
 local d
;
 
PYTHON(test,a,b,c);
 
d:=CAS("pythonanswer1698554");
 
CAS("purge(pythonanswer1698554)");
 
d;
END

You can also save it as a home variable with hpprime.eval instead of cas.caseval.

-road

Thanks for this. This seems more complicated than I hoped for. Is there some resource I can use to learn things like this?
Find all posts by this user
Quote this message in a reply
09-08-2021, 11:08 AM
Post: #10
RE: How does python work?
I don't know of any resources. There may be a better way to do what you want but that's the best I could come up with. Everything I know about Python on the prime was learned by trial and error (mostly error), and by reading posts in this forum.

-road
Find all posts by this user
Quote this message in a reply
Post Reply 




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