Python 到 JavaScript 转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22595989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:14:55  来源:igfitidea点击:

Python to JavaScript converter

javascriptpython

提问by Kivylius

I want to convert some basic snippets to JavaScript, just pure Python I wrote myself to pure JavaScript. Is there any thing out there? Here the code.

我想将一些基本片段转换为 JavaScript,只是我自己编写的纯 Python 为纯 JavaScript。外面有什么东西吗?这里是代码。

items = init['items']

itemsKeyedById = {i["id"]: i for i in items} # hard to convert.

for item in items:
    if item["parents"][0]['isRoot'] == False:
        parent = itemsKeyedById[item["parents"][0]['id']]

        if "children" not in parent:
            parent["children"] = []
        parent["children"].append(item)

topLevelItems = [item for item in items if item["parents"][0]['isRoot'] == True] # hard to convert.
try:
    return json.dumps(topLevelItems[0]);  
except:
    return '[]'

@Davide: Sadly this question has been closed, otherwise I'd write an answer instead of a comment. The best way to do have python as client side these days is with https://github.com/qquick/Transcrypt

@Davide:很遗憾,这个问题已经结束,否则我会写一个答案而不是评论。这些天将 python 作为客户端的最佳方法是使用https://github.com/qquick/Transcrypt

采纳答案by DJG

You can actually run a Python interpreter directly in JS thanks to emscripten.

由于emscripten ,您实际上可以直接在 JS 中运行 Python 解释器。

The project is called empythoned:

该项目名为empythoned

Empythoned is a build script that uses Emscripten to compile CPython for use in a browser. It attempts to compile the main interpreter as a single small executable and the whole standard library as dynamically loaded libraries.

Empythoned 是一个构建脚本,它使用 Emscripten 编译 CPython 以在浏览器中使用。它尝试将主解释器编译为单个小型可执行文件,并将整个标准库编译为动态加载的库。

but be warned:

但请注意:

The project is in its infancy. Right now the core interpreter works very well, but many of the libraries either don't work at all or contain various bugs.

该项目尚处于起步阶段。现在核心解释器工作得很好,但许多库要么根本不工作,要么包含各种错误。

回答by Remolten

You should try this:

你应该试试这个:

http://gatc.ca/projects/pyjsdl/

http://gatc.ca/projects/pyjsdl/

It works fine with regular python and even supports pygame.

它适用于常规 python,甚至支持 pygame。

回答by Matthias Urlichs

You might want to look into RapydScript. It is actively maintained (as of Oct 2014) and comes with a couple of cute examples which actually work.

您可能想研究一下RapydScript。它得到了积极维护(截至 2014 年 10 月),并附带了几个实际有效的可爱示例。