Try Python output with Haxe 3.2

Python output is available from Haxe 3.2, so let's try it.

Installation

Haxe 3.2 installation

Download and install the latest version from the official Haxe Downloads (http://haxe.org/download/).

If you are using homebrew on your Mac, you can also do the following.

$ brew install haxe --HEAD

Alternatively, refer to Building Haxe and build it yourself.

Install Python 3.x

Since there are many other articles, I will omit it. So far, Haxe seems to be able to output only Python3.

Haxe / Python tutorial

Hello World

Let's try Python output with Official Hello World.

First, create HelloWorld.hx with the following contents.

HelloWorld.hx


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

Compile the above into helloworld.py with haxe.

$ haxe -main HelloWorld -python helloworld.py

The -main option is the main class, and the -python option is the file name to be output by specifying Python. See Compiler Usage for details.

Alternatively, you can also create and compile compile.hxml as shown below.

compile.hxml


-python helloworld.py
-main HelloWorld
$ haxe comple.hxml
#Haxe above-main HelloWorld -python helloworld.hello world like py.py is output

If the compilation is successful, execute the output helloworld.py.

$ python3 helloworld.py
Hello World

Hello World was output safely.

In addition, helloworld.py output at this time was as follows.

helloworld.py


class HelloWorld:

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



HelloWorld.main()

Get command line arguments

You can get the command line arguments from Sys.args.

Main.hx


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

Print a list of command line arguments with Sys.println.

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

At this time, the output main.py had 378 lines. (long!) Probably because I started using Sys, and it contains some extras. However, it would be interesting to read the output Python.

File output

Try to output the file with 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

When outputting a file with codecs

If you use python.lib.Codecs for file output, it will be equivalent to 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

I won't write it here because it's long, but if you read the actual output main.py, you can see that it uses codecs.open.

Of course, python.lib.Codecs can only be used when outputting Python, so be careful. (Sys.io.File can be compiled for other platforms such as C ++, PHP, neko)

Use the library with Haxe / Python

Call the Python standard library

Some of Python's standard libraries are still available, as is the case with the codecs above. From Issue # 2924, it seems that part of sys.net and all of sys.db are not implemented (will be implemented soon). You can check the available ones in the python.lib package.

Python output using Haxe library

There is a Haxe-implemented HTML parser library called HtmlParser. It's a (probably pure) Haxe implementation, so you should be able to output to any platform that Haxe supports. Yes, of course in Python!

That's why I will install and use the Haxe library.

$ 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);
  }
}

When using a library, specify the library to be used with the -lib option at compile time.

$ 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

You can see that you can parse the HTML and get the title.

In other words, any (pure) Haxe-implemented library can be used for Python, and the same library can be used for other platforms. amazing! (Of course, there are already a number of excellent Python libraries for HTML parsers ...)

Issues

Recommended Posts

Try Python output with Haxe 3.2
Try scraping with Python.
Try running Python with Try Jupyter
Try face recognition with Python
Try python
Try scraping with Python + Beautiful Soup
Try singular value decomposition with Python
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
[Note] Hello world output with python
Unit test log output with python
Try face recognition with python + OpenCV
Try frequency control simulation with Python
Try to output audio with M5STACK
FizzBuzz with Python3
Try to reproduce color film with Python
Scraping with Python
Try logging in to qiita with Python
Statistics with python
Scraping with Python
Output color characters to pretty with python
Python with Go
Try working with binary data in Python
python learning output
Output Python log to console with GAE
Twilio with Python
Integrate with Python
Try using Python with Google Cloud Functions
Play with 2016-Python
Python> try: / except:
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
UnicodeEncodeError struggle with standard output of python3
Try HTML scraping with a Python library
Bingo with python
Try calling Python from Ruby with thrift
Zundokokiyoshi with python
Try drawing a map with python + cartopy 0.18.0
[Continued] Try PLC register access with Python
Try assigning or switching with Python: lambda
Excel with Python
[For beginners] Try web scraping with Python
Microcomputer with Python
Cast with python
nginxparser: Try parsing nginx config file with Python
3.14 π day, so try to output in Python
Try it with Word Cloud Japanese Python JupyterLab.
Read JSON with Python and output as CSV
Try running Google Chrome with Python and Selenium
Try to solve the man-machine chart with Python
Python log is not output with docker-compose up
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
I tried to output LLVM IR with Python
Try to automatically generate Python documents with Sphinx
Try working with Mongo in Python on Mac
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Try to make a dihedral group with Python
Try to detect fish with python + OpenCV2.4 (unfinished)