我很高兴看到现在可以在浏览器中编写Python代码.这些是主要候选人(请添加我可能忽略的任何一个):
但如何在它们之间做出选择?我能看到的唯一明显区别是Skulpt基于Python 2,而Brython基于Python 3.
请注意:这不是建议或意见的要求.我正在寻找能够为受过教育的选择提供信息的客观事实.
对于Transcrypt Python到JavaScript编译器的 3.7.1版本,我目前正在使用新的@dataclass装饰器.根据PEP的摘要,我原本预计==, !=, <, >, >=, <=会得到支持,但似乎并非如此:
from dataclasses import dataclass
@dataclass
class C:
x: int = 10
Run Code Online (Sandbox Code Playgroud)
有些比较不起作用:
>>> c1 = C(1)
>>> c2 = C(2)
>>> c1 == c2 # ok
False
>>> c1 < c2 # crash
TypeError: '<' not supported between instances of 'C' and 'C'
Run Code Online (Sandbox Code Playgroud)
为什么不支持比较运算符,除了==和!=?或者我忽略了什么?
在Transcrypt Python to JavaScript编译器中实现协同程序时,我遇到了以下奇怪的问题.
Transcrypt使用CPython 3.6的本机解析器生成AST.对于异步全局函数defs,它会生成AsyncFunctionDef节点.但对于异步方法,它没有!然而,CPython本身似乎正确地编译异步方法.
所以下面的一段代码用CPython运行,但是Transcrypt无法运行它,因为CPython的AST模块生成的AST似乎缺少方法的AsyncFunctionDef节点(而不是全局函数).
所以下面的代码不会生成AsyncFunctionDef节点:
class C:
def __init__ (self):
self.aTime = 2
async def g (self, waw, asio):
print ('g0')
await waw (self.aTime, asio)
print ('g1')
Run Code Online (Sandbox Code Playgroud)
我错过了什么?异步方法是官方支持的,不是吗?在PEP 492中找不到任何具体内容.
该示例的完整代码是:
from org.transcrypt.stubs.browser import __pragma__, __envir__
# Note that CPython will ignore all pragma's
# Provide waitAWhile for Transcrypt
__pragma__ ('js', '{}', '''
function waitAWhile (aTime, asio) {
return new Promise (resolve => {
setTimeout (() => {
resolve (aTime);
}, 1000 * …Run Code Online (Sandbox Code Playgroud) 我一直在谷歌搜索一段时间,例如'typeof'和'performance',但我还没有找到一个满意的答案来解决以下问题.
我正在尝试使用本地运算符重载为Transcrypt Python到JavaScript编译器实现复数.由于我们正在处理动态类型语言,因此无法预测变量中的数据类型.
如果我转换x + y为JavaScript,打开运算符重载,它将转换为例如__add__ (x, y)为了做正确的事情,__add__函数必须检查两者x以及y它们是否是"普通"JavaScript数字,或者它们中的一个或两者都是属于"复杂"类型,因为这需要特殊操作.
最明显的方法是测试typeof x == 'number'.然而,来自C/C++背景,用一个带有六个字符的字符串来测试相等性似乎是非常低效的,首先必须从内存中检索它,只有可能添加两个整数,这对于许多处理器来说,一次解析后,只会有一条指令.
让我感到惊讶的是,像这样的检查建议在互联网的各个地方作为正常的事情.有谁知道是否x == 'number'或可能x === 'number'以某种方式巧妙地优化,以防止完整的字符串比较.
为了进一步澄清问题,这里是我的__add__操作符的当前代码,使用字符串比较.
def __add__ (self, other):
if __typeof__ (other) == 'number': # Translates to: if (typeof other == 'number') {
return complex (self.real + other, self.imag)
else: # Other is complex
return complex (self.real + other.real, self.imag + other.imag)
Run Code Online (Sandbox Code Playgroud)
如果没有,任何人都可以通过更快的方式暗示我区分数字和任意非数字对象.
谢谢你的提示.来源现在是:
def __sub__ (self, …Run Code Online (Sandbox Code Playgroud) 我正在开发Transcrypt Python to JavaScript 编译器的3.7.1 版。发布过程的一部分是出货测试,其中 Transcrypt 与 CPython 背靠背测试。
它曾经在 CPython 3.7 的 beta 版中完美运行,但在发布时出现了问题。
该程序:
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class Test:
x: ClassVar = 10
y: int = 10
t = Test ()
t.x = 20
print (repr (t))
Run Code Online (Sandbox Code Playgroud)
用于打印(使用 CPython):
Test(x=20, y=10)
Run Code Online (Sandbox Code Playgroud)
但是随着它打印的发布(再次仅使用 CPython):
Test(y=10)
Run Code Online (Sandbox Code Playgroud)
所以它从表示中遗漏了类变量 x。谁能告诉我这个改变是否是故意的,我在哪里可以找到证明它合理的讨论?
有了这个变化,Transcrypt 的行为与 CPython 不同,这是我不想要的。所以我想知道我应该采用 Transcrypt 还是应该将其视为 CPython 回归并等待它被解决。
我尝试了 Transcrypt 教程中的“hello”示例,但收到以下错误消息:
bash-4.3$ transcrypt hello
Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.9.0
Copyright (C) Geatec Engineering. License: Apache 2.0
Saving target code in:
/home/User/hello/__target__/org.transcrypt.__runtime__.js
Saving minified target code in:
/home/User/hello/__target__/org.transcrypt.__runtime__.js
Error while compiling (offending file last):
File 'org.transcrypt.__runtime__', line 0, namely:
[Errno 2] No such file or directory: 'java': 'java'
Aborted
Run Code Online (Sandbox Code Playgroud)
该错误是由需要java的Google编译器minifier引起的吗?有没有办法不用java运行Transcrypt?
我刚刚使用https://github.com/bunkahle/Transcrypt-Examples/blob/master/alerts/alerts2.py 中的简单程序在 Python 3.7 Win7 上尝试了最新的 transcrypt,但出现编译错误:
C:\Python37\Programme\Transcrypt>transcrypt alerts2.py
Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.7.16
Copyright (C) Geatec Engineering. License: Apache 2.0
Saving target code in: C:/Python37/Programme/Transcrypt/__target__/org.transcrypt.__runtime__.js
Saving minified target code in: C:/Python37/Programme/Transcrypt/__target__/org.transcrypt.__runtime__.js
Error while compiling (offending file last):
File 'org.transcrypt.__runtime__', line 0, namely:
[WinError 2] The system cannot find the specified file
Aborted
Run Code Online (Sandbox Code Playgroud)
知道出了什么问题吗?我只是得到一个名为的目录__target__
,其中包含一个被调用的文件,org.transcrypt.__runtime__.pretty.js但没有任何 alerts2.js。顺便说一句,您为什么__javascript__将编译目录的名称从 transcrypt 3.6 更改为__target__in transcrypt 3.7?
python ×8
transcrypt ×8
brython ×2
javascript ×2
async-await ×1
browser ×1
compilation ×1
python-3.7 ×1
rapydscript ×1
skulpt ×1