Python到Jython Gotchas?

时间:2011-04-28 00:44:37

标签: python migration jython

我正在开始从python到jython的迁移过程。有没有人以前轻松完成这件事? 有什么问题?我应该首先在Jython IDE中构建然后部署还是什么?

2 个答案:

答案 0 :(得分:4)

请记住,在jython中,在Java下运行时,无论你使用什么平台,所有东西都是'Big-Endian',而在PC / Linux / Mac(x86)平台上,python是Little Endian。确保在使用struct.pack和struct.unpack时使用适当的前缀

没有前缀

写入数据(enessw.py)

import struct 
f = file('tmp.dat', 'wb') # binary 
f.write(struct.pack('IIII', 1,2,3,4)) # default endianess

读取数据(enessr.py)

import struct
f = file('tmp.dat', 'rb')
data = f.read()
ints = struct.unpack('IIII', data) # default endianess
print repr(ints)

结果

用python编写,用jython读取

C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py

C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)

C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(16777216L, 33554432L, 50331648L, 67108864L)

使用python

编写jython
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessw.py

C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)

C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(16777216, 33554432, 50331648, 67108864)

修复

使用<在pack和unpack的格式字符串中。这将指示打包/解压缩数据格式为特定的小端。

将'< IIII'作为打包/解包格式

C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py

C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)

C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)

C:\Documents and Settings\mat99856\My Documents\tmp>jython enessw.py

C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)

C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py

C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)

C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)

参考

struct.pack

答案 1 :(得分:2)

主要问题是Jython没有使用C作为其实现的任何标准或第三方库模块。或者有C编译的帮助模块。有很多这些,他们可能会以意想不到的方式出现。

此外,Jython要慢得多。

这实际上取决于您正在迁移的内容,以及它对第三方模块的依赖程度以及它使用的“纯”Python的程度。

但是,我希望这种迁移存在很多问题。我知道的大多数Jython是从头开始编写的,用Java类做特定的事情,主要用于测试。