Skip to main content

Python to CLike language transpiler.

Project description

py2many: Python to many CLike languages transpiler

Build License

Why

Python is popular, easy to program in, but has poor runtime performance. We can fix that by transpiling a subset of the language into a more performant, statically typed language.

A second benefit is security. Writing security sensitive code in a low level language like C is error prone and could lead to privilege escalation. Specialized languages such as wuffs exist to address this use case. py2many can be a more general purpose solution to the problem where you can verify the source via unit tests before you transpile.

A third potential use case is to accelerate python code by transpiling it into an extension

Swift and Kotlin dominate the mobile app development workflow. However, there is no one solution that works well for lower level libraries where there is desire to share code between platforms. Kotlin Mobile Multiplatform (KMM) is a player in this place, but it hasn't really caught on. py2many provides an alternative.

Lastly, it's a great educational tool to learn a new language by implementing a backend for your favorite language.

Status

Rust is the language where the focus of development has been. C++14 is historically the first language to be supported.

Preliminary support exists for Julia, Kotlin, Nim, Go and Dart.

py2many can also emit Python 3 code that includes inferred type annotations, and revisions to the syntax intended to simplify parsing of the code.

History

Based on Julian Konchunas' pyrs http://github.com/konchunas/pyrs

Based on Lukas Martinelli Py14 (https://github.com/lukasmartinelli/py14) and Py14/python-3 (https://github.com/ProgVal/py14/tree/python-3) branch by Valentin Lorentz.

Example

Original Python version.

def fib(i: int) -> int:
    if i == 0 or i == 1:
        return 1
    return fib(i - 1) + fib(i - 2)

Transpiled Rust code:

fn fib(i: i32) -> i32 {
    if i == 0 || i == 1 {
        return 1;
    }
    return (fib((i - 1)) + fib((i - 2)));
}

Transpiled code for other languages:

https://github.com/adsharma/py2many/tree/main/tests/expected (fib*)

Trying it out

Requirements:

  • python 3
  • clang
  • rustc

Local installation:

./setup.py install --user  # installs to $HOME/.local

OR

sudo ./setup.py install  # installs systemwide

Add the py2many script to your $PATH and run:

Transpiling:

py2many --cpp=1 /tmp/fib.py
py2many --rust=1 /tmp/fib.py
py2many --julia=1 /tmp/fib.py
py2many --kotlin=1 /tmp/fib.py
py2many --nim=1 /tmp/fib.py
py2many --dart=1 /tmp/fib.py
py2many --go=1 /tmp/fib.py

Compiling:

clang fib.cpp
rustc fib.rs
...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2many-0.3.tar.gz (212.5 kB view details)

Uploaded Source

Built Distribution

py2many-0.3-py3-none-any.whl (116.2 kB view details)

Uploaded Python 3

File details

Details for the file py2many-0.3.tar.gz.

File metadata

  • Download URL: py2many-0.3.tar.gz
  • Upload date:
  • Size: 212.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.10

File hashes

Hashes for py2many-0.3.tar.gz
Algorithm Hash digest
SHA256 26ea45077ef3f7729961d34e7e0b7e3f21a00597f0aad41b86facada91c77c7f
MD5 a671048c845730c2245d71561ec13026
BLAKE2b-256 3d96a30136c170c9438098dca68bcc98bf30fd2276b3da79de204cf38b5a1cce

See more details on using hashes here.

File details

Details for the file py2many-0.3-py3-none-any.whl.

File metadata

  • Download URL: py2many-0.3-py3-none-any.whl
  • Upload date:
  • Size: 116.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.10

File hashes

Hashes for py2many-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 adf8869808dbb768cadc1cc8c97526bd2404e91ffa4c55f4d4550d2a5d3854e9
MD5 9f7cbdb5cf667a4ac152103c4b6afb31
BLAKE2b-256 12ea71670f7148a7a48cc9e258d0e7412e6d398c3415b71aba94a140c262f1a1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page