HP Forums
Python, how to export variable? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Python, how to export variable? (/thread-20655.html)



Python, how to export variable? - ab3ap - 10-16-2023 08:05 PM

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


RE: Python, how to export variable? - Guenter Schink - 10-17-2023 07:04 PM

(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


RE: Python, how to export variable? - ab3ap - 10-17-2023 10:01 PM

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


RE: Python, how to export variable? - Guenter Schink - 10-19-2023 07:46 AM

(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


RE: Python, how to export variable? - ab3ap - 10-19-2023 12:37 PM

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...


RE: Python, how to export variable? - Guenter Schink - 10-20-2023 09:13 PM

(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


RE: Python, how to export variable? - ab3ap - 11-07-2023 11:31 PM

(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


RE: Python, how to export variable? - Allaman - 11-13-2023 08:39 PM

Thank you, @ab3ap, for your write-up!


RE: Python, how to export variable? - ab3ap - 11-13-2023 09:51 PM

(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