首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python to Jython Gotchas?

Python to Jython Gotchas?
EN

Stack Overflow用户
提问于 2011-04-28 08:44:37
回答 2查看 402关注 0票数 2

我开始了从python到jython的迁移过程。以前有没有人能轻松地做到这一点?有什么问题吗?我应该先在Jython IDE中构建,然后再部署吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-12 23:14:28

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

不带前缀

写入数据(enessw.py)

代码语言:javascript
复制
import struct 
f = file('tmp.dat', 'wb') # binary 
f.write(struct.pack('IIII', 1,2,3,4)) # default endianess

读取数据(enessr.py)

代码语言:javascript
复制
import struct
f = file('tmp.dat', 'rb')
data = f.read()
ints = struct.unpack('IIII', data) # default endianess
print repr(ints)

结果

使用python编写,使用jython读取

代码语言:javascript
复制
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读取

代码语言:javascript
复制
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)

解决之道

在格式字符串中使用<进行打包和解包。这将指示打包/解包数据在格式上特别是小端。

使用“

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2011-04-28 08:52:51

这样做的主要问题是Jython没有任何使用C作为其实现的标准或第三方库模块。或者使用C编译帮助程序模块。有相当多这样的东西,它们可能会以意想不到的方式突然出现。

而且,Jython的速度要慢得多。

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

然而,我预计这样的迁移会有很多问题。据我所知,大多数Jython都是从头开始编写的,用Java类来做特定的事情,主要是为了测试。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5812362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档