Python output is available from Haxe 3.2, so let's try it.
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.
Since there are many other articles, I will omit it. So far, Haxe seems to be able to output only Python3.
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()
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.
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
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)
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.
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 ...)
Recommended Posts