LoginSignup
2
2

More than 5 years have passed since last update.

Haxe3.2でPython出力を試す

Last updated at Posted at 2015-05-21

Haxe 3.2からPython出力が可能になったので、試してみます。

インストール

Haxe 3.2のインストール

Haxe公式のDownloadsから最新版をダウンロードしてインストールします。

Macでhomebrewを使っているなら下記でもOKです。

$ brew install haxe --HEAD

もしくは、Building Haxeを参考に自分でビルドします。

Python3.xのインストール

他にたくさん記事があるので省略。
なお、いまのところHaxeが出力可能なのはPython3だけのようです。

Haxe/Pythonチュートリアル

Hello World

公式のHello WorldでPython出力をしてみます。

まずはHelloWorld.hxを下記の内容で作成。

HelloWorld.hx
class HelloWorld {
    static public function main() {
        trace("Hello World");
    }
}

上記をhaxeでhelloworld.pyにコンパイル。

$ haxe -main HelloWorld -python helloworld.py

-mainオプションがメインクラス、-pythonオプションがPython指定で出力するファイル名です。詳細はCompiler Usage参照。

もしくは、下記のようにcompile.hxmlを作成しコンパイルする方法もあります。

compile.hxml
-python helloworld.py
-main HelloWorld
$ haxe comple.hxml
# 上記の haxe -main HelloWorld -python helloworld.py と同じようにhelloworld.pyが出力される

コンパイルが成功したら、出力されたhelloworld.pyを実行します。

$ python3 helloworld.py
Hello World

無事、Hello Worldが出力されました。

なお、このとき出力されているhelloworld.pyは下記のようになっていました。

helloworld.py
class HelloWorld:

    @staticmethod
    def main():
        print("Hello World")



HelloWorld.main()

コマンドライン引数の取得

Sys.argsからコマンドライン引数が取得できます。

Main.hx
class Main {
  static public function main():Void {
    var args = Sys.args();
    for (arg in args) {
      Sys.println(arg);
    }
  }
}

コマンドライン引数のリストをSys.printlnで出力します。

$ haxe -main Main -python main.py
$ python3 main.py foo
foo
$ python3 main.py foo bar
foo
bar

このとき出力されたmain.pyは378行ありました。(長い!)
おそらく、Sysを使用するようになったためで、若干余分なものも含まれています。
とはいえ出力されたPythonを読んでみるのもおもしろいでしょう。

ファイル出力

sys.io.Fileでファイル出力をしてみます

Main.hx
import sys.io.File;

class Main {
  static public function main():Void {
    var output = File.write("output.txt");
    output.writeString("test\n");
    output.close();
  }
}
$ haxe -main Main -python main.py
$ python3 main.py
$ cat output.txt
test

ファイル出力をcodecsで行う場合

ファイル出力にpython.lib.Codecsを使用すると、Pythonのcodecs相当になります。

Main.hx
import python.lib.Codecs;

class Main {
  static public function main():Void {
    var file = Codecs.open("output.txt", "w");
    file.write("test\n");
  }
}
$ haxe -main Main -python main.py
$ python3 main.py
$ cat output.txt
test

長いのでここには書きませんが、実際に出力された main.pyを読んでみると、codecs.open を使用していることがわかります。

当然ながら、python.lib.Codecs はPython出力時にしか使用できませんので、注意が必要です。
sys.io.FileはC++, PHP, nekoなどの他のプラットフォーム向けコンパイルも可能)

Haxe/Pythonでライブラリを使用する

Pythonの標準ライブラリを呼び出す

上記 codecs を利用する場合のように、Pythonの標準ライブラリのいくつかはそのまま利用可能です。
Issue#2924より、現時点ではsys.netの一部とsys.dbの全ては未実装(いずれ実装予定)のようです。利用可能なものはpython.lib パッケージで確認できます。

Haxeのライブラリを使用してPython出力する

HtmlParserというHaxe実装のHTMLパーサライブラリがあります。(たぶんpureな)Haxe実装なので、Haxeが対応しているプラットフォームならいずれにも出力することが出来るはずです。そう、もちろんPythonにも!

というわけでHaxeのライブラリをインストールして利用してみます。

$ haxelib install HtmlParser
Main.hx
import sys.io.File;

import htmlparser.HtmlDocument;

class Main {
  static public function main():Void {
    var html = new HtmlDocument(File.getContent("example.html"));
    var titles = html.find(">html>head>title");
    trace(titles[0].innerHTML);
  }
}

ライブラリ使用時はコンパイル時に-libオプションで使用するライブラリを指定します。

$ haxe -main Main -python main.py -lib HtmlParser
example.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Title of Example</title>
  </head>
  <body>
    <h1>Example</h1>
  </body>
</html>
$ python3 main.py
Title of Example

HTMLをパースして、タイトルを取得できていることがわかります。

つまり、(pureな)Haxe実装のライブラリであればPython向けに利用することもできるし、その他のプラットフォームにも同じライブラリを利用することが出来ます。すごいね!
(もちろん、HTMLパーサぐらいは既にPythonで優秀なライブラリがいくつもありますが…)

課題とか

  • Pythonの外部ライブラリを使用したいときはどうするの?(externして@:pythonImport?externがめんどくさそう…)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2