You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).
Short demo:
>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3
This is how the translated function looks like internally (it's rather ugly):
>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'$'])
@Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
var.registers([u'a'])
return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'
', PyJsHoistedNonPyName)
Answer from Piotr Dabkowski on Stack OverflowVideos
You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).
Short demo:
>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3
This is how the translated function looks like internally (it's rather ugly):
>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'$'])
@Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
var.registers([u'a'])
return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'
', PyJsHoistedNonPyName)
Updated
Now several (4) years later this (almost certainly) can be done; though certainly not with RegEx. I suggest future readers look to @Piotr Dabkowski's answer.. Or some of the other answers. (I don't know having not tried them)
Original Answer
Hm this is a hard one. The definition of a compiler is translates from a higher level language to a lower level language. eg python to machine-code. or java to javascript (google has a rather famous compiler for this somewhere - its' what makes google doc easier to make) Python to javascript compilers abound. technically javascript to python would be a decompiler. (afaik)
I found some speculation about a javascript-python converter here: follow the tread through. it mostly speaks of how it wouldn't be too hard to do. I can't find anything , but that doesn't mean it's no out there.
Regex is not suitable, regex is suitable only for regular languages. programming languages are not normally regular languages. see this
I stayed away from python at first since I wanted to build cool stuff immediately but as a programming beginner the mingling syntax between js, html and css just frustrates me and I can’t make a lot of progress quickly.
I’m wondering if switching to python would be a good move since I’m studying engineering and I won’t have that frustrating part of not knowing the syntax for three different things.
Just focus on one program and build it correctly you know.
Thanks for reading.
Hi all, I am wanting to learn some JS/Node to help a friend develop some systems. While not a developer by trade, I use Python to automate tasks at work and building simple programs.
I am just wanting to know if anyone can recommend a good resource. Basically how I learnt Python was using the Python official manuals and O'Reilly's Python Pocket Reference. Can anyone recommend resources similar to this that don't focus on front end programming?
MDN GuideMDN ReferenceThe Rauschmayer books
Here is the first 3 hours of a course that helped me transition from Python to YouTube. I shelled out the $10 for his course on Udemy (there are always sales there if you open it in incognito mode), but it looks like people have reuploaded the whole course to YouTube if you just search for the name.
Anyways, this isn't what I'd suggest if you have no JavaScript knowledge at all, but if you understand the basic syntax I'd highly recommend it. JavaScript does have some very weird mechanics and stuff in it, and this helped me a ton.
I already tried to ask this on r/javascript but they removed the post