Python to RPN converter

Discussion around the Swiss Micros DM42 calculator.
Posts: 13
Joined: Sun Jan 14, 2018 11:03 am

Python to RPN converter

Post by tcab » Fri Feb 16, 2018 5:30 am

I am pleased to announce the Python to HP42S RPN converter website is online.


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:
  2. User Guide:
  3. Canvas for 42S Simulator:
  4. List of HP42S Commands Supported Reference:
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(), 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 DM42.

I hope people like it.
Last edited by tcab on Fri Nov 02, 2018 7:33 am, edited 3 times in total.

Posts: 563
Joined: Mon Apr 24, 2017 6:38 pm

Re: Python to RPN converter

Post by keithdalby » Fri Feb 16, 2018 7:54 am

Did you make this yourself? It's incredible.

User avatar
Posts: 235
Joined: Thu Jun 01, 2017 9:51 am
Location: Finland

Re: Python to RPN converter

Post by Vitasam » Fri Feb 16, 2018 8:20 am

Amazing job!!!
Elektronika MK-61, Elektronika MK-52, HP15c LE, DM42 SN#16 FW 3.18

Krauts In Space
Posts: 85
Joined: Wed Jan 03, 2018 2:48 pm
Location: Nuremberg, Germany

Re: Python to RPN converter

Post by Krauts In Space » Fri Feb 16, 2018 8:22 am

Wow! Incredible job!
This opens a whole new world.
DM15L S/# 10584 FW v25
DM42 S/# 01015 FW v3.5

Posts: 12
Joined: Fri Nov 17, 2017 9:53 am
Location: Perth, Australia

Re: Python to RPN converter

Post by prasad22 » Fri Feb 16, 2018 8:50 am

Wow. This is brilliant.

That means in the very near future some java programmer like me could write Java code, and convert it to RPN too. Very, very exciting possibilities indeed. Providing memory permits, it probably is possible to write Java code for most of the functions an HP48 or an HP50g provides and port it to DM42. I will surely try this converter.

User avatar
Posts: 58
Joined: Sat Dec 30, 2017 9:09 am
Location: Ragusa, Sicily

Re: Python to RPN converter

Post by salvomic » Fri Feb 16, 2018 9:44 am

Many thanks, Andy!
Amazing program.
I'm sure it could help to have more and more good programs for DM42 and Free42s from the fantasy of a lot of people.
Python sure will help in simplifying all.

∫aL√0mic (IT9CLU) - DM42 (SN: 00881), DM16, HP Prime, 50g, 41CX, 42s, 71b, 15C, 12C, HP 35s, WP34s :: Free42

Posts: 22
Joined: Fri Jul 28, 2017 1:36 am

Re: Python to RPN converter

Post by tgray » Fri Feb 16, 2018 12:47 pm

Very nice! This is pretty amazing.

Posts: 13
Joined: Sun Jan 14, 2018 11:03 am

Re: Python to RPN converter

Post by tcab » Mon Feb 19, 2018 12:49 pm

New release today - Converter page now has a Clear button and a New Project link - as well as no initial demo code - nice and clean. A new LBL main start point for programs is the new default, allowing access to the Python global scope area - great for sharing variables between functions. Programs written with this paradigm are likely to be more appealing to RPN programmers and look like:

Code: Select all


length = 10
width = 20


def report():
  print('length is', length, '[LF]width is', width)
Variable references now look outwards from functions into their outer functions (if any)... all the way to global scope - until they find the variable. This is how nested scope works in most programming languages, including Python. Read all about scope in the help file.

Other features released today are the ability to edit and delete cloned example snippets, as well as vote on them. A new simplified menu() command has been added - there are a couple of examples showing how you can rapidly build custom menus and submenus.

Posts: 88
Joined: Mon Jul 24, 2017 1:19 pm
Location: Germany

Re: Python to RPN converter

Post by Thomas_ER » Mon Feb 19, 2018 3:15 pm

This is really great!
Many thanks for your work!
[ HP48/49/50/42S/WP34/HP Prime/ DM42 (#00185+00318) ]

Posts: 30
Joined: Tue Dec 12, 2017 6:39 pm
Location: France, Normandie

Re: Python to RPN converter

Post by wawachief » Tue Feb 20, 2018 1:36 pm

This is an incredible work ! thank you.

I try to convert a simple program to find the sum of all divisors of a given integer, just for testing :

Code: Select all


def sommediv(n):
  res = 0
  for d in range(2, n):
    if n%d == 0:
      res += d
  return res
But unfortunatly, I get an error :
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Am I doing something wrong ?
DM42 SN:00218
HP-11c - HP-19b - HP25 - HP 45 - HP42s - HP48gx

Post Reply