Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Py2many – Transpile Python3 to 7 languages (github.com/adsharma)
9 points by adsharma 8 days ago | hide | past | favorite | 13 comments

Can compile the following 16 python programs to 7 languages, compile and run the resulting code and produce the same output as the source python.

https://github.com/adsharma/py2many/tree/main/tests/cases https://github.com/adsharma/py2many/tree/main/tests/expected

100x speedup on this benchmark


  py2many.py --rust=1 test.py
  rustc -O test.rs

  $ time -p ./test.py
  iterations 3
  sum 1.4999999709476166e+17
  real 436.55
  user 435.45
  sys 1.08

  $ time -p ./test 3
  iterations 3
  sum 1.500000E+17
  real 4.94
  user 4.67
  sys 0.25

There was a i32 -> f64 type cast problem fixed by this PR


Also added this benchmark as a test case, so we don't regress.

I like the idea, but it's seems a little premature to show it, since there's very little information. For example, what is the supported subset? Are there any divergences in behavior? (for example, are the dicts always ordered?)

Also, just wondering how modular it is. How hard would it be to add another target, say Javascript?

I haven't formally defined the supported subset. The 16 test cases, especially coverage.py should give you a sense.

JavaScript is possible via python to dart to JavaScript.

Adding another backend is easy, given there are 7 backends already.

Are dicts ordered? Not by default. It should be possible to translate Python's ordereddict to one in the target language.

Just mapping max/min was hard enough. In some languages max(1,2) produces a float!

Small note: Python's standard dict has been ordered since ~3.7

E.g. https://mail.python.org/pipermail/python-dev/2017-December/1...

Looking for feedback here. How many programmers actually know this and expect it? Is it worth the extra complexity when translating to static languages which don't provide this guarantee?

From an implementation perspective, doing it for the ordereddict special case is much simpler.

I think you can get away with keeping it unordered for 95% of the code. But the remaining 5% are going to fail in very confusing, and possibly inconsistent ways.

Pyccel https://github.com/pyccel/pyccel translates Python3 to C or Fortran and is intended for numerical Python code. I have played with it, and it works pretty well.

This one is more aimed at everyday business applications, including mobile apps and web apps.

Love this kind of thing. Can you add a lua target?

Could you open an issue for me on github? It shouldn't be hard to get hello world working.

Bringing it to the same level as other languages is probably several weeks of work. Pull requests welcome.

Outside my wheelhouse, but I know someone else who might be better with lua code generation.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact