Post Reply 
Python, how to export variable?
10-16-2023, 08:05 PM
Post: #1
Python, how to export variable?
I bought a Prime a month or so ago and I'm enjoying learning how to program it with python. The lack of official python docs (at least that I can find) make it more challenging, but I'm making progress.

One thing I haven't succeeded at yet and seems should be easy, is how to export a variable from python. Trying to export 'n' into variable 'x' like this doesn't seem to work:

Code:
cmd = 'x=%e; export x' % n
hpprime.eval(cmd)

Some initial learning efforts are shared at https://udel.edu/~mm/hp/

Thanks,
Mike
Visit this user's website Find all posts by this user
Quote this message in a reply
10-17-2023, 07:04 PM
Post: #2
RE: Python, how to export variable?
(10-16-2023 08:05 PM)ab3ap Wrote:  I bought a Prime a month or so ago and I'm enjoying learning how to program it with python. The lack of official python docs (at least that I can find) make it more challenging, but I'm making progress.

One thing I haven't succeeded at yet and seems should be easy, is how to export a variable from python. Trying to export 'n' into variable 'x' like this doesn't seem to work:

Code:
cmd = 'x=%e; export x' % n
hpprime.eval(cmd)

Some initial learning efforts are shared at https://udel.edu/~mm/hp/

Thanks,
Mike

Nice web site you've set up. Now to your problem. As you have figured hpprime.eval() is the key to interface between PPL and Python.
Quote:hpprime.eval(string)

Evaluates string as a PPL command line.
Since strings can be dynamically created, this allows you to communicate from Python to PPL and send data from the Python world to the PPL world, and/or to use facilities available in the calculator but not in Python.
The PPL result will be returned as a Python object if possible (numbers, complexes, strings), else it will be converted into a Python string"
That means, the string you hand over to PPL must be something intelligible for PPL.

I've created two apps that might be of interest and where some features of hpprime.eval() are used.
Mandelbrot Explorer and
Lindenmayer Fractals
Look for hpprime.eval() there, perhaps you can take something from there.

Günter
Find all posts by this user
Quote this message in a reply
10-17-2023, 10:01 PM
Post: #3
RE: Python, how to export variable?
Many thanks, Günter. I don't see export() calls in your programs, or I would happily steal your technique. :-) The eval() I showed executes without error, but seems does not export the variable. Either I'm looking in the wrong place (possible!) or it exists only in scope of that call and is out of scope afterward. To be generally useful, program calculations need to be exportable to stack or variable. All other eval() calls work fine in my code, but no luck yet exporting for use outside python programs. I think there must be a way.

Mike Markowski
Visit this user's website Find all posts by this user
Quote this message in a reply
10-19-2023, 07:46 AM
Post: #4
RE: Python, how to export variable?
(10-17-2023 10:01 PM)ab3ap Wrote:  Many thanks, Günter. I don't see export() calls in your programs, or I would happily steal your technique. :-) The eval() I showed executes without error, but seems does not export the variable. Either I'm looking in the wrong place (possible!) or it exists only in scope of that call and is out of scope afterward. To be generally useful, program calculations need to be exportable to stack or variable. All other eval() calls work fine in my code, but no luck yet exporting for use outside python programs. I think there must be a way.

Mike Markowski
There isn't such thing like export. Briefly only as I'm traveling right now. Look at the end of main.py in L- system. Watch out for "AVars".
Günter
Find all posts by this user
Quote this message in a reply
10-19-2023, 12:37 PM (This post was last modified: 11-08-2023 11:59 AM by ab3ap.)
Post: #5
RE: Python, how to export variable?
Thank you, Günter! I haven't yet read details of creating an app but seems the time has come. Till now, I've simply created .py files and put them in Programs folder.

Mike

To keep this thread up to date, I also corrected my previous PPL code:

Code:
cmd='export X; X:=%e' % n
hpprime.eval(cmd)

and still no luck with that approach. Now, to read up on apps...
Visit this user's website Find all posts by this user
Quote this message in a reply
10-20-2023, 09:13 PM
Post: #6
RE: Python, how to export variable?
(10-19-2023 12:37 PM)ab3ap Wrote:  Thank you, Günter! I haven't yet read details of creating an app but seems the time has come. Till now, I've simply created .py files and put them in Programs folder.

Mike

To keep this thread up to date, I also corrected my previous PPL code:

Code:
cmd='export X; X:=%s' % n
hpprime.eval(cmd)

and still no luck with that approach. Now, to read up on apps...

Of course no luck. Once again: "That means, the string you hand over to PPL must be something intelligible for PPL."
Type that string into the PPL command line and see yourself.
BTW, as hpprime.eval() is a function, it always returns something.

Günter, still not at home
Find all posts by this user
Quote this message in a reply
11-07-2023, 11:31 PM
Post: #7
RE: Python, how to export variable?
(10-20-2023 09:13 PM)Guenter Schink Wrote:  [...] Of course no luck. [...]

Günter, still not at home

Thank you, Günter, and 100% understood. I share that failed effort to new Prime programmers who will inevitably follow my path of trials and errors unless HP (that is, Moravia) updates User's Manual.

I'm now successfully sharing results in variables via python App rather than simple programs as I hoped. The result at last is a program performing some useful functions on the job. It has been many years since I've preferred a calculator over a laptop at work. I don't know why, but this makes me happy. :-)

Mike
Visit this user's website Find all posts by this user
Quote this message in a reply
11-13-2023, 08:39 PM
Post: #8
RE: Python, how to export variable?
Thank you, @ab3ap, for your write-up!
Find all posts by this user
Quote this message in a reply
11-13-2023, 09:51 PM
Post: #9
RE: Python, how to export variable?
(11-13-2023 08:39 PM)Allaman Wrote:  Thank you, @ab3ap, for your write-up!

You're welcome! Until a manual is written, I hope a group effort as we each discover and share different things will turn into an interim unofficial manual of sorts.

-Mike
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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