如何使用 Haxe 编码的程序执行外部 Python 脚本?

How to execute external Python script with a program coded in Haxe?

我有一个 Haxe 程序,我需要从 Wordnik API 检索数据。 以下是 Wordnik 支持的平台列表: http://developer.wordnik.com/#!/libraries

我对 Wordnik 支持的所有这些语言都没有经验。但是,我认为 Python 是将 Wordnik API 连接到我的 Haxe 程序中最可行的方法,因为 Python 是一种脚本语言,可以从终端命令执行。

也许,像 Haxe 程序这样的东西会使用一些参数执行 Python。然后 Python 脚本从 Wordnik 检索数据,然后将其编译成 JSON 或 .txt 文件。最后return回到Haxe程序解析JSON或.txt文件。我不确定这个东西是如何工作的,因此我在这里寻找指导:)。

需要注意的一件事是使用 Python 3 version of the library, instead of the Python 2.7 one which is linked to on that overview page. Haxe's Python target only supports version 3 or higher

不需要 Python 程序作为 Haxe 和 Wordnik 之间的接口 API - 你可以写 externs describing the interface to just use it directly from Haxe. An extern for a very simple class, wordnik.models.Label,看起来像这样:

package wordnik.models;

@:pythonImport("wordnik.models.Label", "Label")
extern class Label
{
    public var text:String;
    public var type:String;

    public function new() 
    {
    }
}

这样,您就可以使用 Haxe 的 API:

package;

import python.Lib;
import wordnik.models.Label;

class Main 
{
    static function main() 
    {
        var label = new Label();
        label.text = "Test";
        trace(label.text);
    }
}

你可以在 the Haxe standard library. It also has wrappers for things that are a bit more complex to express, like KwArgs.

中找到很多 Python externs 的例子