Python to RPN converter
02-16-2018, 05:27 AM (This post was last modified: 11-02-2018 04:23 AM by tcab.)
Post: #1
 tcab Member Posts: 128 Joined: Dec 2017
Python to RPN converter
I am pleased to announce the Python to HP42S RPN converter website is online.
http://www.pyrpn.atug.com/

You write code in a high level structured language (which happens to be Python 3 syntax), hit a button and RPN is generated.

You then paste the RPN into Free42 or transfer it to your DM42 (by creating a raw) - and it runs.
1. Examples: http://www.pyrpn.atug.com/examples
2. User Guide: http://www.pyrpn.atug.com/help
3. Canvas for 42S Simulator: http://www.pyrpn.atug.com/canvas
4. List of HP42S Commands Supported Reference: http://www.pyrpn.atug.com/cmds

The converter supports core Python syntax (which is very powerful), but does not implement the built in Python libraries that you would get in desktop Python. You have to rely on the ability to call HP42S commands from Python to do your work - which of course you can do. Specifically, it has the following capabilities:
1. Variables
2. Functions, Multiple functions, nested functions
3. Parameter passing, receiving return values, multiple return values
4. if elif else
5. Comparison operators == != > < >= <=
6. Booleans True, False and operators not or and
7. for loops, range(), for..in iteration through lists and dictionary keys
8. while loops, while...else
9. continue and break operations in for loops and while loops
10. Lists and Dictionaries (basic operations only).
11. Matrices, Pythonic matrix element access syntax [row,col]
12. NumPy compatible slicing syntax for sub-matrices
13. Complex numbers using either 42S or Python native syntax
14. Expressions involving nested brackets
15. assert
16. Testing and clearing of flags
17. Access most HP42S commands as function calls e.g. FIX(2)
18. Some enhanced functions to make life easier e.g. varmenu() automates and simplifies the generation of MVAR based code.

Included in the examples are some graphic primitive routines originally written for the Raspberry Pi in C rewritten into Python and converted to RPN. This now gives the 42S a small graphics library to generate lines, circles, rectangles, filled shapes etc. More information in this related thread. This would be of particular interest to owners of the DM42 calculator which has the larger screen, crying out to be taken advantage of.

and targeting the larger screen size of the DM42 - this image:

The purpose of the Python to RPN converter is not to usurp the beauty and role of handcrafted RPN, it is to provide an alternative way of programming the HP42S for those who prefer to to use if statements and for loops etc. rather than GTO's and ISG. The purpose is also to provide powerful new features to make programming the 42S easier: lists, dictionaries, matrix access syntax, native complex number syntax, alpha messages easily built with multiple parameters in one line, simpler menu support using aview() etc.

My hope is that the Python to RPN converter will contribute to keeping this wonderfully designed calculator alive, just as Free42 and the DM42 have done. I look forward to what people build, using the increased ease of use and power of a structured programming language and hope to see some great new programs developed for the 42S - and for Free42 and DM42 in particular - where it is particularly easy to transfer generated RPN to those devices (in the case of Free42 on a mac or pc, its as simple as copy paste - there is a tutorial in the user guide).

I hope people like it.
02-16-2018, 06:26 AM
Post: #2
 pier4r Senior Member Posts: 1,918 Joined: Nov 2014
RE: Python to RPN converter
Nice work!

Wikis are great, Contribute :)
02-16-2018, 09:47 AM
Post: #3
 salvomic Senior Member Posts: 1,364 Joined: Jan 2015
RE: Python to RPN converter
kudos!
thanks.

Salvo

∫aL√0mic (IT9CLU), HP Prime 50g 41CX 71b 42s 12C 15C - DM42 WP34s :: Prime Soft. Lib
02-16-2018, 02:34 PM
Post: #4
 rprosperi Senior Member Posts: 3,164 Joined: Dec 2013
RE: Python to RPN converter
Ambitious and very well-documented! Thanks for sharing with the community.

--Bob Prosperi
02-16-2018, 02:38 PM
Post: #5
 charger73 Member Posts: 50 Joined: Dec 2013
RE: Python to RPN converter
thank you for this great tool!! with small changes this is also usable for
the DM41 und other 41 models.

a dream comes closer. a python -> 41 mcode converter/compiler ;-)

great work!

best regards
Tobie
02-16-2018, 03:04 PM
Post: #6
 Nigel (UK) Senior Member Posts: 322 Joined: Dec 2013
RE: Python to RPN converter
This looks great - well done!

By the way, the main Help page and also the RPN Command List refer to a "varmenu()" Python function, which helps set up variable menus. Where is this function documented?

Apologies if I'm overlooking something obvious!

Nigel (UK)
02-16-2018, 11:06 PM
Post: #7
 tcab Member Posts: 128 Joined: Dec 2017
RE: Python to RPN converter
(02-16-2018 03:04 PM)Nigel (UK) Wrote:  By the way, the main Help page and also the RPN Command List refer to a "varmenu()" Python function, which helps set up variable menus. Where is this function documented?

Apologies - "varmenu()" was in my unit tests but not in the documentation. It now documented in the user guide and a specific example now also exists in the examples section database under the "Introductory Examples" category. Basically it works like this:

Code:
 def area_app():   varmenu("length", "width")   return length * width

All varmenu() really does is allow you to specify multiple variables on the same line and then adds the “boilerplate” calls to MVAR, VARMENU, STOP and EXITALL in for you.
02-17-2018, 01:06 AM
Post: #8
 compsystems Senior Member Posts: 1,102 Joined: Dec 2013
RE: Python to RPN converter
+10
unique project in its type =). I really like Python and now I can export to rpn, although the rpn of the hp42 is not of such high level =( and it is difficult to interpret

A proposal, first to investigate if someone or several enthusiasts are interested in expanding to the hp28 and hp48 series calculator =)

The good enough is the enemy of the excellent.
02-17-2018, 08:42 AM
Post: #9
 Massimo Gnerucci Senior Member Posts: 1,675 Joined: Dec 2013
RE: Python to RPN converter
(02-17-2018 01:06 AM)compsystems Wrote:  +10
unique project in its type =). I really like Python and now I can export to rpn, although the rpn of the hp42 is not of such high level =( and it is difficult to interpret

A proposal, first to investigate if someone or several enthusiasts are interested in expanding to the hp28 and hp48 series calculator =)

In the 42 RPN is at its highest. Maybe you are used to RPL, but that's another story.

Greetings,
Massimo

-+×÷ ↔ left is right and right is wrong
02-17-2018, 01:53 PM
Post: #10
 rprosperi Senior Member Posts: 3,164 Joined: Dec 2013
RE: Python to RPN converter
(02-17-2018 01:45 PM)compsystems Wrote:  In the example of the first publication, the RPN is very similar to the assembly language, very important to develop machine-level computational reasoning in an introductory course of computer systems.

LOL... Hope you're familiar with the phrase 'duck and cover'...

--Bob Prosperi
02-17-2018, 02:27 PM
Post: #11
 Massimo Gnerucci Senior Member Posts: 1,675 Joined: Dec 2013
RE: Python to RPN converter
(02-17-2018 02:00 PM)compsystems Wrote:  I am Colombian and I speak Spanish, the Spanish phrase 'duck and cover' we do not use it, I do not know what its meaning is.

Greetings,
Massimo

-+×÷ ↔ left is right and right is wrong
02-17-2018, 02:33 PM
Post: #12
 rprosperi Senior Member Posts: 3,164 Joined: Dec 2013
RE: Python to RPN converter
(02-17-2018 02:00 PM)compsystems Wrote:  I am Colombian and I speak Spanish, the Spanish phrase 'duck and cover' we do not use it, I do not know what its meaning is.

It means duck your head and hide, because this is totally the wrong place to say that RPN is weak and only for introductory college courses. At least half the members here prefer RPN over RPL. And like any religious preference, each side is right, and is not fond of folks ridiculing their preference.

My comment was really meant in humor, so don't take it as offence, it was only meant to say you will certainly see replies from some people who don't agree with how you view RPN.

And like most jokes, it certainly is no longer funny once it's explained...

--Bob Prosperi
02-17-2018, 03:58 PM
Post: #13
 pier4r Senior Member Posts: 1,918 Joined: Nov 2014
RE: Python to RPN converter
(02-17-2018 02:33 PM)rprosperi Wrote:  At least half the members here prefer RPN over RPL.

I did not investigated by myself, but I wonder if one could develop a sort of "translation" sheet to find equivalent (or almost equivalent) commands between RPN and RPL. This fixed one calculator for RPN, say the 42S, and one for the RPL (say the 48G).

I strongly believe the two quite close to each other. With my little experience maybe the most different commands could be the branching ones or the "go to labels".

Wikis are great, Contribute :)
02-17-2018, 05:29 PM
Post: #14
 John Keith Senior Member Posts: 357 Joined: Dec 2013
RE: Python to RPN converter
(02-17-2018 03:58 PM)pier4r Wrote:  I did not investigated by myself, but I wonder if one could develop a sort of "translation" sheet to find equivalent (or almost equivalent) commands between RPN and RPL. This fixed one calculator for RPN, say the 42S, and one for the RPL (say the 48G).

I strongly believe the two quite close to each other. With my little experience maybe the most different commands could be the branching ones or the "go to labels".

Bill Wickes wrote a book on the subject, "HP41-48 Transitions". It's on your HP Museum memory stick.
02-18-2018, 09:21 AM
Post: #15
 StephenG1CMZ Senior Member Posts: 708 Joined: May 2015
RE: Python to RPN converter
This looks impressive.
My first thought was: How difficult would it be to emulate 42 RPN on a Prime?

So far, I have successfully downloaded demo onto free42 and executed it.
However, as soon as I try to change the Python code, I get Unindent errors from the convertor.

The changes are trivial - demo->demo2 and a change to the final text.

Is it working OK for everyone else?

Stephen Lewkowicz (G1CMZ)
02-18-2018, 10:27 AM
Post: #16
 tcab Member Posts: 128 Joined: Dec 2017
RE: Python to RPN converter
Any "unindent does not match any outer indentation level" error messages simply means that your code indenting is inconsistent somewhere. Indentation is critical to Python code - you soon get used to it. Read up about Python's indention rules here.
02-18-2018, 12:59 PM
Post: #17
 Thomas Okken Senior Member Posts: 707 Joined: Feb 2014
RE: Python to RPN converter
How does Python handle tabs? Does it consider them to be four spaces, or eight, or does it try to guess?

Using the wrong tab width merely makes code look messy in most languages, but in Python, it seems to me that this could be a serious issue.
02-18-2018, 01:13 PM
Post: #18
 tcab Member Posts: 128 Joined: Dec 2017
RE: Python to RPN converter
"The most "pythonic" way is to use 4 spaces per indentation level. The Python interpreter will however recognize spaces or tabs. The only gottcha is you must never mix spaces and tabs, pick one or the other." ref

The Python to RPN converter code editor converts everything you do into spaces - so its impossible to make a mistake. You can even hit TAB to indent your code and the appropriate number of spaces are inserted. SHIFT TAB will similarly de-indent your code.

You can even use two spaces to indent, as long as you are consistent.
02-18-2018, 02:32 PM (This post was last modified: 02-18-2018 04:15 PM by StephenG1CMZ.)
Post: #19
 StephenG1CMZ Senior Member Posts: 708 Joined: May 2015
RE: Python to RPN converter
(02-18-2018 10:27 AM)tcab Wrote:  Any "unindent does not match any outer indentation level" error messages simply means that your code indenting is inconsistent somewhere. Indentation is critical to Python code - you soon get used to it. Read up about Python's indention rules here.

Just to be clear, I was not querying Python's indentation rules.
I was querying the functionality of the on-line Python convertor.

Here is the code it is objecting to:
Code:
 def demo2(): 2     total = 0 3     for i in range(10): 4         result = calc_something(i, 5) 5         print('Counter', i, 'result=', total) 6         total += result 7     print('FinalZtotal was:', total) 8 ​ 9 def calc_something(a,b): 10     # adds two numbers then squares them 11     return (a + b)**2
The changes from the on-line demo are:
Name changed from demo to demo2
String changed from "Final ..." to "FinalZ..." (i.e. space changed to Z, string length unchanged)
The changes were made by tapping into the on-screen demo window in the convertor...
I haven't tried pasting new source files yet.
And the error is:
Code:
 unindent does not match any outer indentation level, line: 7     print('FinalZtotal was:', total)                                     ^
So, how do I fix the indentation?
Tested on both Android 6 and Android 5

Update: Uploading new source to the convertor also seems impractical. I have to delete demo from the window several times - the text keeps re-appearing. When I do finally get an empty window, guess what is in my paste buffer instead of my source? It did work once and the compiler objected to the last line.
Suely there must be a "New source" button somewhere but I haven't seen it.

Stephen Lewkowicz (G1CMZ)
02-18-2018, 09:25 PM
Post: #20
 tcab Member Posts: 128 Joined: Dec 2017
RE: Python to RPN converter
(02-18-2018 02:32 PM)StephenG1CMZ Wrote:  Name changed from demo to demo2
String changed from "Final ..." to "FinalZ..." (i.e. space changed to Z, string length unchanged)
The changes were made by tapping into the on-screen demo window in the convertor...

I just did those two simple edit steps and the converter seems fine. I did it on my iPad and also on my desktop. Perhaps you could supply the exact source code text - without the line numbers you added - then I can simply paste it into the converter to see what happens.

Regarding a 'Clear' button - that's not a bad idea. I've gotten used to cmd-a to select the initial demo code then then del to delete it, but can see how deleting a slab of text on a tablet might be more laborious. As for the initial demo text, it seemed like a good idea to give people a starting point, however perhaps the initial situation should indeed be blank. After all, there are plenty of examples - and each example has a "Load in Converter Editor" link. So - potentially: Python code area initially empty with a Clear button added.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.20120780169963837, "perplexity": 7081.536942326741}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-09/segments/1550247506094.64/warc/CC-MAIN-20190221172909-20190221194909-00388.warc.gz"}