Conversion from JavaScript to Python code? [closed]

2024/11/20 21:23:29

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.

Answer

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)
https://en.xdnf.cn/q/26276.html

Related Q&A

connect to a DB using psycopg2 without password

I have a postgres database on my localhost I can access without a password$ psql -d mwt psql (8.4.12) Type "help" for help.mwt=# SELECT * from vatid;id | requester_vatid |...-----+---------…

Shebang doesnt work with python3

I have the following program:#!/usr/local/bin/python3print("Hello")Via terminal I do test.py and I get:Traceback (most recent call last):File "/usr/lib/python3.3/site.py", line 629,…

Why does pip freeze list pkg-resources==0.0.0?

On Ubuntu 16.04 with virtualenv 15.0.1 and Python 3.5.2 (both installed with apt) when I create and activate new Python virtual environment withvirtualenv .virtualenvs/wtf -p $(which python3) --no-site…

How to delete an instantiated object Python?

I am relatively new to object oriented programming and I cannot figure out how to delete an instantiated object in Python. if self.hit_paddle(pos) == True or self.hit_paddle2(pos) == True:bar = bar + 1…

Call method from string

If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:class CallMe: # Classdef App(): # Method one...def Foo(…

Scipy sparse... arrays?

So, Im doing some Kmeans classification using numpy arrays that are quite sparse-- lots and lots of zeroes. I figured that Id use scipys sparse package to reduce the storage overhead, but Im a little …

Using virtualenv with spaces in a path

I set up a virtualenv environment on my Mac, but cannot get Pip to install packages. It fails with the following error:/Volumes/Macintosh: bad interpreter: No such file or directoryI tracked the proble…

What is the difference between a stack and a frame?

Under what situations would I want to use one over the other?What is the difference between:>>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(<frame o…

Python Reverse Find in String

I have a string and an arbitrary index into the string. I want find the first occurrence of a substring before the index.An example: I want to find the index of the 2nd I by using the index and str.rfi…

Is there a direct approach to format numbers in jinja2?

I need to format decimal numbers in jinja2. When I need to format dates, I call the strftime() method in my template, like this:{{ somedate.strftime(%Y-%m-%d) }}I wonder if there is a similar approach …