Conversion from JavaScript to Python code? [closed]
Asked Answered
H

4

54

Is there a relatively quick program out there to accomplish at least the basics of this? Just a few regexes? I'm willing to do some manual conversion, but this is a pretty big set of scripts.

Handicap answered 27/2, 2010 at 7:43 Comment(10)
ur question sounds very vague..please add moreMonolatry
What exactly do you want to convert?Finella
I'm not sure whether such a conversion tool exists. To be sure, this will involve a lot more than "a few regexes"; the thing is that neither javscript nor python programs are regular languages...Hyperboloid
I've seen people on SO wanting to use regexes for lots of things they shouldn't be used for... converting python to js takes the cake though.Prosenchyma
this question isn't that vagueMaleficent
@Tom: what about parsing dates?Dibucaine
How is this question vague?Amorita
I recommend reviewing the answer of @Piotr Dabkows, I believe my answer is now superseded and out of date.Maleficent
Found a good online utility tool to convert Javascript to Python - javainuse.com/js2pySomerville
i ended up using ChatGPT to translate 150 KByte of javascript in chunks of 30 KByte. so this works better on modular code, which can easily be splitted into chunks. for an offline solution see PolyCoderMight
M
20

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

Maleficent answered 27/2, 2010 at 8:4 Comment(3)
Thanks, I guess I will have to do it manually to do it right.Handicap
Does js2py.eval_js support "require" statements?Rooks
@AlekhyaSatya I have no idea. Please do not ask questions in comments (there is a Ask Question button for that :-) )Maleficent
W
40

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'$'
var.put(u'$', PyJsHoistedNonPyName)
Whew answered 10/12, 2014 at 21:56 Comment(1)
Seems Js2Py does not support classesCastorena
M
20

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

Maleficent answered 27/2, 2010 at 8:4 Comment(3)
Thanks, I guess I will have to do it manually to do it right.Handicap
Does js2py.eval_js support "require" statements?Rooks
@AlekhyaSatya I have no idea. Please do not ask questions in comments (there is a Ask Question button for that :-) )Maleficent
A
11

This answer might be about 2 years late but how about js -> CoffeeScript -> python? If you use something like http://js2.coffee/ to convert to cs, then things like indentation and lists are already done for you.

Anthia answered 4/10, 2012 at 10:43 Comment(4)
except that coffeescript converts to js, not the other way aroundLalla
which is why I suggested using js2coffeeAnthia
too bad it says the site isn't available anymoreForgotten
but this will not work when you have arrow functions in you js code.Prepossession
K
0

If what you are asking is converting a few regexs in javascript to Python equivalent, the basics of regular expressions are mostly pretty standard. check out the Python re module doc. See if what you are using is documented. Also of interest to you is this page.

If you are talking about converting javascript code to Python equivalent, the best converter is you. Learn about Python, and then convert them manually. there is nothing else better than the human being. Programming constructs like loops, variables, arrays are pretty common and standard you will recognize instantly how to use them straight away.

Keenan answered 27/2, 2010 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.