notalentgeek
notalentgeek

Reputation: 5779

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

I have a Haxe program and I need to retrieve data from Wordnik API. Here are the list of supported platform in Wordnik: http://developer.wordnik.com/#!/libraries

I have no experience in all of these languages supported by Wordnik. However, I think Python is the most feasible way to connect Wordnik API into my Haxe program because Python is a scripting language and can be executed from terminal command.

Perhaps, something like Haxe program execute the Python WITH some parameters. Then the Python script retrieve data from Wordnik and then compile it into a JSON or .txt file. Finally return back to Haxe program to parse the JSON or .txt file. I am not sure how this thing can work, hence I am looking for guidance here :).

Upvotes: 3

Views: 1003

Answers (1)

Gama11
Gama11

Reputation: 34138

One thing to look out for is using the 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.

There shouldn't be a need for a Python program serving as an interface between Haxe and the Wordnik API - you could write externs describing the interface to just use it directly from Haxe. An extern for a very simple class, wordnik.models.Label, could look like this:

package wordnik.models;

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

    public function new() 
    {
    }
}

With that, you can then use the API from Haxe:

package;

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

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

You can find a lot of examples of Python externs in the Haxe standard library. It also has wrappers for things that are a bit more complex to express, like KwArgs.

Upvotes: 5

Related Questions