Python 到 Jython 的陷阱?

标签 python migration jython

我正在开始从 python 到 jython 的迁移过程。以前有人轻松做到过吗? 有哪些陷阱?我应该先在 Jython IDE 中构建然后部署还是什么?

最佳答案

请记住,在 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

读取数据(enesr.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

关于Python 到 Jython 的陷阱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812362/

相关文章:

java - 使用 Jython 的标准 Python 模块的 ImportError(在 JAR 文件中)

python - 在 Django 中将 Django-Hstore 用于 PostgreSQL HSTORE 时出错

python - 在2个pandas数据框之间匹配数据并在Python中提取另一列的匹配值

java - 从 jdk 8 迁移到 jdk 11 cxf 生成源未知导入

java - 现有数据库上的飞路迁移导致 "wrong column type encountered in column"

python - Jython 和 SAX 解析器 : No more than 64000 entities allowed?

Python/Boto + AWS S3 版本控制 : how do I replace the current key with older version?

python - 如何使用字符串定义函数名

java - 需要迁移 hibernate 3 到 5 指南吗?

java - 独立 Jython : Import Error (Apache-POI)