我直截了当吗?PyPy解释器是否真正解释自己然后翻译自己?
所以这是我目前的理解:
如果这是真的,那么这是我见过的最令人头疼的事情之一.
(或者,"可以使用PyPy/RPython将Python编译/转换为C/C++ 而不需要Python运行时吗?")
我试图通过它的RPython及其Python,它的运行和编译以及它的翻译来理解PyPy,并且有些失败.
我有一个假设的Python项目(适用于Windows); 我想保持它的大小,大约100千字节(ONO),而不是使用py2exe所需的几兆字节(在UPX之后).我可以以任何方式使用PyPy 1来生成不依赖于Python26.dll的独立可执行文件吗?如果可以的话,它是否需要遵循RPython限制,例如for只处理内置类型,还是完整的Python语法?
我确实意识到,如果可以做到这一点,我几乎肯定不能直接使用Python的C模块.
1 (自提问时起,情况变得更加清晰,工具链的这一部分更明确地被称为RPython而不是PyPy; 2010年情况并非如此.)
我使用python包pymongo从mongodb数据库中检索数据.
>>> r = collection.find() # returns an object of class 'Cursor'
Run Code Online (Sandbox Code Playgroud)
然后我转换成一个列表
>>> l = list(r) # returns a 'list' of 'dict'
Run Code Online (Sandbox Code Playgroud)
这是print(l)返回的内容:
>>> [{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'_id': 1, u'name': u'name1', u'value': 11},{u'date': datetime.datetime(2013, 11, 10, 10, 45), u'_id': 2, u'name': u'name2', u'value': 22}]
Run Code Online (Sandbox Code Playgroud)
现在我需要转换为JSON,以便我可以操作它.
>>> json.dumps(l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True) …Run Code Online (Sandbox Code Playgroud) 我有一个python脚本,它需要5个参数(一个文件名,3个int值和2个浮点值).我需要从R调用这个python脚本.我该怎么做.我正在尝试使用rPython,但它不允许我传递参数
library("rPython")
python.load("python scriptname")
Run Code Online (Sandbox Code Playgroud)
我不知道如何传递参数
从命令行,我运行我的python脚本,如:
python scriptname filename 10 20 0.1 5000 30
Run Code Online (Sandbox Code Playgroud) 通常声称RPython(Python的一个子集)是静态类型的.(例如在维基百科上.)
最初,我想知道他们如何将它添加到Python并认为他们可能已经添加了添加语句的要求,例如assert isinstance(arg1, ...)在每个函数的开头(但我真的不相信).
然后我查看了一些RPython代码,它看起来并没有真正的静态类型.在许多情况下,可能是编译器可以证明函数参数只能是某些类型,但绝对不是在所有情况下.
例如,这是RPython的实现string.split:
def split(value, by, maxsplit=-1):
bylen = len(by)
if bylen == 0:
raise ValueError("empty separator")
res = []
start = 0
while maxsplit != 0:
next = value.find(by, start)
if next < 0:
break
res.append(value[start:next])
start = next + bylen
maxsplit -= 1 # NB. if it's already < 0, it stays < 0
res.append(value[start:len(value)])
return res
Run Code Online (Sandbox Code Playgroud)
在关于RPython的PyPy文档中,有人说:" 变量应该包含最多一种类型的值 ".
那么,函数参数也算作变量吗?或者在什么意义上RPython静态输入?或者这实际上是错误的?
PyPy GIL是RPython中PyPy解释器实现的一部分,还是translate.py自动添加的东西?也就是说,如果我要在RPython中编写我自己的新语言解释器并通过translate.py运行它,它是否会先于GIL,或者是由我的解释器代码决定的?
RPython是否支持生成器,因为我只是在PyPy的文档中读到了一些内容,但它们并没有
它们似乎很容易被翻译成像C这样的静态类型语言,因为每个生成步骤都是在函数调用中生成的.
有人可以解释原因吗?或者对这个问题有更多的了解.我目前正在尝试学习编写RPython安全代码的基础知识.
我有以下代码:
import sys
def entry_point(argv):
sys.exit(1)
return 0
def target(*args):
return entry_point, None
Run Code Online (Sandbox Code Playgroud)
但是,当我运行时,python ./pypy/pypy/translator/goal/translate.py t.py我收到以下错误:
...
[translation:ERROR] Exception: unexpected prebuilt constant: <built-in function exit>
[translation:ERROR] Processing block:
[translation:ERROR] block@9 is a <class 'pypy.objspace.flow.flowcontext.SpamBlock'>
[translation:ERROR] in (t:3)entry_point
[translation:ERROR] containing the following operations:
[translation:ERROR] v0 = simple_call((builtin_function_or_method exit), (1))
[translation:ERROR] --end--
Run Code Online (Sandbox Code Playgroud)
实际上有更多的错误,但我认为只有最后一部分是相关的.如果你认为它可能会有所帮助,请发表评论,然后我会编辑.
实际上,当我将sys.exit替换为更简单的类似sys.stdout.write时,我会遇到另一个错误.
import sys
def entry_point(argv):
sys.stdout.write('some mesg\n')
return 0
def target(*args):
return entry_point, None
Run Code Online (Sandbox Code Playgroud)
给我:
...
[translation:ERROR] AnnotatorError: annotation of v0 degenerated to SomeObject()
[translation:ERROR] v0 = …Run Code Online (Sandbox Code Playgroud) 我有一些意外的行为,从Bash和RStudio中运行相同的脚本.
请考虑以下事项.我有一个"~/rpython"包含两个脚本的文件夹:
# test1.R
library(rPython)
setwd("~/rpython")
python.load("test1.py")
number <- python.get("number")
string <- python.get("string")
print(sqrt(number))
print(string)
Run Code Online (Sandbox Code Playgroud)
和
# test1.py
import random, nltk
number = random.randint(1, 1000)
string = nltk.word_tokenize('home sweet home')
Run Code Online (Sandbox Code Playgroud)
我可以从Bash调用我的R脚本Rscript test1.R,它按预期返回
>> Loading required package: RJSONIO
>> [1] 13.0384
>> [1] "home" "sweet" "home"
Run Code Online (Sandbox Code Playgroud)
如果我再次调用它将产生一个不同的随机数
>> Loading required package: RJSONIO
>> [1] 7.211103
>> [1] "home" "sweet" "home"
Run Code Online (Sandbox Code Playgroud)
但是当我test1.R从RStudio 运行相同的脚本()时,事情变得奇怪了.这里输出
# test1.R
>
> library(rPython)
Loading required package: RJSONIO
>
> setwd("~/rpython")
> …Run Code Online (Sandbox Code Playgroud) 我对PyPy项目非常感兴趣,但对于下面列出的第一个(但不太知名)的目的:
在以下博文中,http://morepypy.blogspot.com/2011/04/tutorial-writing-interpreter-with-pypy.html和http://morepypy.blogspot.com/2011/04/tutorial-part -2-adding-jit.html有关于如何使用RPython实现脑力叉解释器的详细教程,并添加了一个JIT.
但是我在其他地方读过RPython可能很麻烦 - 为动态类型创建的语法突然限制为推断的静态类型导致难以理解的编译错误.
所以我的问题是,是否有任何其他项目可以让你像上面的教程一样编写脑力翻译/ JIT?或者PyPy是这么简单的唯一选择吗?
(旁白):如果存在,一般来说RPython有什么意义?是否只是为了表明Python的子集可以是类型安全的,并且在该子集中实现了Python?在现有的翻译创建工具中做"PyPy"会更有意义吗?