Post Reply 
Python to RPN converter
11-04-2018, 06:06 AM
Post: #41
RE: Python to RPN converter
(11-03-2018 07:32 PM)Namir Wrote:  How can I write Python code that translates into:

"Prompt message"
PROMPT
STO <reg>

myvar = PROMPT("Prompt message")

You cannot strictly speaking store into a specific register, because from the Python point of view registers don't exist. You just have to think in Python and stick to Python variables - and treat the generated RPN as "machine code" ;-) Thus:

Code:
myvar = PROMPT("length?")
print("sine of", myvar, "is", SIN(myvar))

You can however store into a HP42S named variable - they are ok to specify. Check out the the "Prompting for input" section in the online examples help.
Find all posts by this user
Quote this message in a reply
11-04-2018, 02:02 PM
Post: #42
RE: Python to RPN converter
Can't you access registers like REGS[index]?
You could also implement a special case for REGS, so that REGS[0] can be translated directly to RCL 00 or STO 00, avoiding the use of STOIJ and RCLEL/STOEL.
Visit this user's website Find all posts by this user
Quote this message in a reply
11-04-2018, 08:07 PM
Post: #43
RE: Python to RPN converter
In displaying the contents of the Alpha register (translated from a Python print statement), would it be better to generate PROMPT statements instead of AVIEW?

Namir
Find all posts by this user
Quote this message in a reply
11-04-2018, 09:59 PM
Post: #44
RE: Python to RPN converter
(11-04-2018 08:07 PM)Namir Wrote:  In displaying the contents of the Alpha register (translated from a Python print statement), would it be better to generate PROMPT statements instead of AVIEW?

Namir

You are correct that print() is a synonym for AVIEW(). In Python print doesn't stop program execution so I wanted to keep that behaviour. If you want to stop program execution so that the user can take a good look at something, then simply use PROMPT(msg) and don't worry about assigning the return value of PROMPT to a variable.

Keep in mind that in the converter, print/AVIEW, PROMPT, PRA and alpha all:
  • take arbitrarily long strings
  • accept multiple parameters e.g. alpha("Ans: n=", n, "result is", myvar)
  • support the Python keyword parameters append= and sep=
For more information see the help page section entitled "Text and the Alpha register".

-Andy
Find all posts by this user
Quote this message in a reply
11-04-2018, 10:25 PM
Post: #45
RE: Python to RPN converter
(11-04-2018 02:02 PM)Thomas Okken Wrote:  Can't you access registers like REGS[index]?
You could also implement a special case for REGS, so that REGS[0] can be translated directly to RCL 00 or STO 00, avoiding the use of STOIJ and RCLEL/STOEL.

I suppose I could open up access to the registers in the way you suggest. Keep in mind however that by default, numbered registers are used to store Python variables, so registers are being pretty much being overwritten all the time and thus don't have much value.

Specifically, all lowercase python variables are mapped to RPN registers in the order that the converter parser sees them. Thus a = 10 and b = 20 will be mapped to registers 00 and 01 respectively.

The Python to RPN converter does offer ways of mapping Python variables to RPN named variables, by using uppercase Python variable names, or the special comment directive 'named' e.g.
Code:
y = 100  # rpn: named

however the default behaviour is to use registers, to avoid polluting the HP42S/Free42 global namespace with dozens of variable names from your Python programs.

In retrospect, perhaps the better implementation would have been to store all the Python variables in a dedicated HP42S matrix and grow the matrix as necessary to house new variables etc. This would have been more work to implement and would result in more verbose generated RPN. However now that the converter is built, it's an area that I may revisit one day.

-Andy
Find all posts by this user
Quote this message in a reply
12-11-2018, 07:05 PM
Post: #46
RE: Python to RPN converter
Has the converter moved? I'm getting 404 errors for all the links…

Cambridge, UK
41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 32SII 48GX 50g 35s 30b/WP34S Prime G2
& Casios, Rockwell 18R :)
Find all posts by this user
Quote this message in a reply
12-11-2018, 10:46 PM
Post: #47
RE: Python to RPN converter
(12-11-2018 07:05 PM)cdmackay Wrote:  Has the converter moved? I'm getting 404 errors for all the links…

Try http://pyrpn.atug.com/

The www. prefixed address was misconfigured due to a server change I did the other week, and should rectify itself shortly. Sorry for the scare!

-Andy
Find all posts by this user
Quote this message in a reply
12-11-2018, 11:27 PM
Post: #48
RE: Python to RPN converter
(12-11-2018 10:46 PM)tcab Wrote:  Try http://pyrpn.atug.com/

The www. prefixed address was misconfigured due to a server change I did the other week, and should rectify itself shortly. Sorry for the scare!

phew! thanks Andy Smile

Cambridge, UK
41CL/DM41X 12/15C/16C DM15/16 71B 17B/BII/bII+ 28S 42S/DM42 32SII 48GX 50g 35s 30b/WP34S Prime G2
& Casios, Rockwell 18R :)
Find all posts by this user
Quote this message in a reply
05-22-2020, 05:53 PM
Post: #49
RE: Python to RPN converter
hello all,
as you can see I'm not a specialist ;-). I would like to know if there is a way to convert in the opposite way: RPN to python please?
Thanks for help

Pascal
Find all posts by this user
Quote this message in a reply
06-09-2020, 04:12 PM
Post: #50
RE: Python to RPN converter
Converting standard notation to an RPN

https://xrjunque.nom.es/ConvertAlg2RPN_RPL.aspx
Find all posts by this user
Quote this message in a reply
06-12-2020, 03:05 PM
Post: #51
RE: Python to RPN converter
This is excellent. I love this.

Two problems I ran into:
* the lack of ROUND equivalent for the HP 42S
* how to handle the int command (set number as an integer)
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2021, 03:42 AM
Post: #52
RE: Python to RPN converter
(06-12-2020 03:05 PM)Eddie W. Shore Wrote:  This is excellent. I love this.

Two problems I ran into:
* the lack of ROUND equivalent for the HP 42S
* how to handle the int command (set number as an integer)

> This is excellent. I love this.

Thanks Eddie! - sorry for the late reply, somehow I just saw your post now.

> * the lack of ROUND equivalent for the HP 42S

HP42 has the built in command RND which you can invoke from Python e.g. you type RND(1.5) and convert it via https://pyrpn.atug.com which generates

1.5
RND

but since the built in HP42S/Free42 RND works via the current display format, we seem to have to call FIX as well to pull this off. Thus a full example might be Python code:

Code:
LBL("untitled")
FIX(0)
x = RND(1.5)
print("rounded is", x)

which generates:

Code:
01 LBL "untitle"
02 FIX 00
03 1.5
04 RND
05 STO 00
06 "rounded is"
07 ├" "
08 ARCL 00
09 AVIEW

> * how to handle the int command (set number as an integer)

Perhaps type IP(myvar) into the Python to RPN converter - which generates the built in Free42 command IP. That's the closest command I can find.

Here is a Python program to do both

Code:
def rndip(n):
  FIX(0)
  r = RND(n)
  i = IP(n)
  FIX(2)
  print(n, "rounded is", r, "integer portion", i)

After conversion, and pasting into Free42, run with

1.5 XEQ RNDIP

* Reminder: the Python to RPN converter is not related to Python in HP Prime CAS, nor the beta MicroPython App in HP Prime. It is a online website which converts Python into RPN which you can paste or type into Free42/HP42S/HP41. Help is https://pyrpn.atug.com/help
Find all posts by this user
Quote this message in a reply
06-12-2021, 03:54 AM
Post: #53
RE: Python to RPN converter
A new release of the converter today:
  • Fixed CLΣ access by providing Python function CLStat()
  • Allow access to statistical registers e.g. RCL(12)
  • Added statistical example to the help file.
Here is an example Python program which can be converted to RPN, which finds the sum of x^2 between numbers _from and _to using the built in feature of the HP statistics function Σ+ by accessing register 12.

Code:
def sumsigma(_from, _to):
  CLStat()  # CLΣ
  for i in range(_from, _to + 1):
    StatPlus(i, i) # Σ+
  x = RCL(12)  # recall summation register
  print("sum of x^2 from", _from, "to", _to, "is", x)

Use: On Free42 type

1 Enter 10 XEQ SUMSIGMA

and you'll get the answer 385.

Its just a fun exercise - easily accomplished on the HP Prime with the one liner e.g. Σ(X^2,X,1,10)
Find all posts by this user
Quote this message in a reply
Post Reply 




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