Skip to main content

Javascript for refined palates: a Python 3 to ES6 Javascript translator

Project description

author:

Alberto Berti

contact:

alberto@metapensiero.it

license:

GNU General Public License version 3 or later

It is based on previous work by Andrew Schaaf.

Goal

JavaScripthon is a small and simple Python 3 translator to JavaScript which aims to be able to translate most of the Python’s core semantics without providing a full python-in-js environment, as most existing translators do. It tries to emit code which is simple to read and check and it does so by switching to ES6 construct when necessary. This allows to simplify the needs of polyfills for many of the expected Python behaviors.

The interface with the js world is completely flat, import the modules or use the expected globals (window, document, etc…) as you would do in JavaScript.

The ES6 code is then converted (if requested) to ES5 code with the aid of the popular BabelJS library together with the fantastic dukpy embedded js interpreter.

Another goal is to just convert single modules or entire dir tree structures without emitting concatenated or minified files. This is left to the Javascript tooling of your choice. I use webpack which has BabelJS integration to getting this job done.

JavaScripthon also generates SourceMap files with the higher detail possible in order to aid development. This means that while you are debugging some piece of translated JavaScript with the browser’s tools, you can actually choose to follow the flow of the code on the original Pyhton 3 source.

This project is far from complete, but it has achieved a good deal of features, please have a look at tests/test_evaljs.py file for the currently supported ones.

Installation

To install the package execute the following command:

$ pip install javascripthon

Usage

To compile or transpile a python source module, use the commandline:

$ python -m pj source.py

or:

$ python -m pj -5 source.py

to transpile. As of now it doesn’t check which features require a transpilation, so the latter is always safer.

Examples

Execute make inside the examples directory.

Testing

To run the tests you should run the following at the package root:

python setup.py test

Build status

https://travis-ci.org/azazel75/metapensiero.pj.svg?branch=master

Contributing

Any contribution is welcome, drop me a line or file a pull request.

Todo

This is a brief list of what needs to be done:

  • use arrow functions for functions created in functions;

  • refactor the comprehensions conversion to use the snippets facility;

  • refactor snippets rendering to write them as a module and import them in the module when tree conversion is enabled;

  • convert dict() calls to ES6 Map object creation;

  • convert set literals to ES6 Set objects;

  • convert async and await to the same proposed features for js (see BabelJS documentation);

  • convert *iterable syntax to ES6 destructuring;

  • convert argument defaults on functions to ES6;

  • multi-line strings to ES6 template strings (does this make any sense?);

  • properties to ES6 properties (getter and setter);

  • class and method decorators to ES7 class and method decorators;

  • implement yield and generator functions;

Done

Stuff that was previously in the todo:

  • translate import statements to ES6;

  • translate __all__ definition to ES6 module exports;

  • write a command line interface to expose the api;

  • make try…except work again and implement try…finally;

External documentation

A good documentation and explanation of ES6 features can be found on the book Exploring ES6 by Axel Rauschmayer (donate if you can).

Tools

Have a look at ECMAScript 6 Tools by Addy Osmani.

To debug source maps have a look at source-map-visualization and its package on npm.

Still i found these links to be helpful:

Here is an example of the latter tool showing code generated by JavaScripthon, have fun!

Notes

  • A post about proposed solutions to use ES6 classes with Backbone. See also the bug open on github.

Changes

0.1 (2016-03-21)

  • First cut of the features.

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

javascripthon-0.1.tar.gz (46.1 kB view hashes)

Uploaded Source

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