tldr: Python 3 to Javascript translator written in Python that produces fast portable javascript code, discontinued by its original developer for some reason. Since it's LGPL, I decided to keep a copy around for reference purposes.
Hello, World!:
$ echo "print('Hello, world')" >> hello.py $ pythonium -V hello.py console.log("Hello, world");
A loop.
$ echo "for i in range(10): print(i)" >> loop.py $ pythonium -V loop.py var iterator_i = range(10); for (var i_iterator_index=0; i_iterator_index < iterator_i.length; i_iterator_index++) { var i = iterator_i[i_iterator_index]; console.log(i); }
You can try Pythonium in the online web console
You will need Python 3 to run the translator. Refer to your operating system documentation to know how to do it (most Linux distributions have Python 3 readily available).
Then you can use pip to install the stable translator:
pip install pythonium
The above package installs a pythonium
command that has the following options:
pythonium
Usage:
pythonium [-h][-d][-r][-V][FILE ...] [-o FILE]|[-g]Options:
-h --help show this -v --version show version -o --output FILE specify output file [default: stdout] -d --deep generate file dependencies. If --output is not provided, it will generate for each source file a coresponding .js file. -r --requirejs generate requirejs compatible module -V --veloce use veloce mode, Python syntax with JavaScript semantic -g --generate generate pythonium library
Pythonium exists in two flavors, each with their own strengths and performance characteristics. As you go down the list the more features you'll have, and performance will, of course, decrease with sophistication (but not much):
- Pythonium Veloce
- Fully portable
- Native Javascript speed
- Exact same syntax as Python 3
- Compatible with existing Javascript libraries
- Functions are translated to Javascript functions with support for keyword arguments,
*args
and**kwargs
- Automatic scope handling and
global
support for
iterates over Javascript arrays- Python types are mapped to Javascript types
- Anonymous exceptions with
__exception__
special variable - Support of
yield
print
is translated toconsole.log
new(SomeObject, arg0, arg1)
as a special function that translates to Javascriptnew SomeObject(arg0, args1)
with any number of arguments- if
__DOLLAR__
is found in any name, it will be replaced by$
character. - Support of
from ... import ...
via requirejs - Support of single inheritance classes via classy [1][2]
[1] Nested class definition are not supported
[2] Calling super method is done with super(arg0, arg1, arg2, ...)
- Pythonium Compliant All the above and 100% compliant with CPython 3
LGPL 2.1 or later