我开始了从python到jython的迁移过程。以前有没有人能轻松地做到这一点?有什么问题吗?我应该先在Jython IDE中构建,然后再部署吗?
发布于 2011-05-12 23:14:28
请记住,在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)使用jython编写使用python读取
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)解决之道
在格式字符串中使用<进行打包和解包。这将指示打包/解包数据在格式上特别是小端。
使用“
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
发布于 2011-04-28 08:52:51
这样做的主要问题是Jython没有任何使用C作为其实现的标准或第三方库模块。或者使用C编译帮助程序模块。有相当多这样的东西,它们可能会以意想不到的方式突然出现。
而且,Jython的速度要慢得多。
这实际上取决于您要迁移的内容,以及它对第三方模块的依赖程度,以及它使用了多少“纯”Python。
然而,我预计这样的迁移会有很多问题。据我所知,大多数Jython都是从头开始编写的,用Java类来做特定的事情,主要是为了测试。
https://stackoverflow.com/questions/5812362
复制相似问题