将库从Java移植到Python [英] Porting library from Java to Python

查看:139
本文介绍了将库从Java移植到Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将一个小型库从Java移植到Python并想要一些建议(小〜几千行代码)。我已经研究了一些Java代码,并注意到了两种语言中常见的一些设计模式。然而,肯定存在一些仅限Java的习语(单身等),这些习惯在Python世界中通常不受欢迎。

I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are generally not-well-received in Python-world.

我至少知道一个工具(j2py)存在,它将通过遍历AST将.java文件转换为.py文件。一些初步的实验产生了不太有利的结果。

I know at least one tool (j2py) exists that will turn a .java file into a .py file by walking the AST. Some initial experimentation yielded less than favorable results.

我是否应该考虑使用自动化工具来生成一些代码,或者语言是否足以使任何工具都足够重新努力从头开始写作是合理的吗?

Should I even be considering using an automated tool to generate some code, or are the languages different enough that any tool would create enough re-work to have justified writing from scratch?

如果工具不是魔鬼,除了j2py之外还有其他任何可以至少处理同一项目导入管理的工具吗?我不希望任何工具将第三方库从一种语言与另一种语言的替代品相匹配。

If tools aren't the devil, are there any besides j2py that can at least handle same-project import management? I don't expect any tool to match 3rd party libraries from one language to a substitute in another.

推荐答案

如果是我的话,我会考虑手工完成这项工作。几千行代码并不是很多代码,通过自己重写(而不是自动翻译),您将能够决定如何恰当地利用Python习语。 (FWIW,我几乎专门从事Java工作9年,现在我正在使用Python,所以我知道你必须要做的翻译。)

If it were me, I'd consider doing the work by hand. A couple thousand lines of code isn't a lot of code, and by rewriting it yourself (rather than translating it automatically), you'll be in a position to decide how to take advantage of Python idioms appropriately. (FWIW, I worked Java almost exclusively for 9 years, and I'm now working in Python, so I know the kind of translation you'd have to do.)

这篇关于将库从Java移植到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆