[PYTHON] I made a package to create an executable file from Hy source code

Hypo As the title says, I made it so much that it is a promotion.

Introduction

You may not be familiar with it in the first place, but Hy is a ** Lisp dialect that runs on a Python VM **. For example, if you read Squid Lisp: Hy written in Python, you can understand the atmosphere. You can understand it as ** Clojure's Python version ** for the JVM. Or rather, ** it looks like Clojure **, so you can highlight the syntax with markdown or ReST for the fucking minor language.

Hy itself can be executed with a dedicated interpreter, but the overhead of parsing is not good. So it's nice to use the attached command hyc to convert it to a .pyc file (byte-compiled Python script) before using it. It is also possible to convert it to a .py file by using the same attached command hy2py, but ** usually it cannot be operated as it is **. Therefore, it can only be used for debugging purposes. This is because the Python AST generated by Hy frequently has symbol names that cannot normally be used in Python. (For example, the variable name generated by gensym starts with: )

The above is not very good when you want to write a small application that simplifies daily tasks using Hy. It's a story that you only have to write a Makefile as soon as you hyc, but it's not so cool **. Also, for Pythonista, it's kind of unpleasant to see the .pyc file every day **.

Then, ** It's like putting all the Hy source code into an executable format **, so I made this article. ** Main subject **.

Installation

Please from PyPI.

$ sudo pip install hypo

How to use

$ hypo -o Output name Source file 1 Source file 2..

It is used like this. If the -o option is omitted, the name will be ʻa. By the way, the entry point of the application is the source file 1here. Note that input is only accepted for.hy` files. The output is zipapp Regards, it is an archive file, but you can think of it as an execution binary because it has been given execution permission in advance. not.

Example of use

For example, try building an application consisting of the following two files.

main.hy


(import [iota [iota]])

(defmain [&rest args]
  (print (iota 10)))

iota.hy


(defn iota [m &optional [n 0] [step 1]]
  (if (>= n m)
    None
    (cons n (iota m (+ n step) step))))

By the way, these codes are as follows in Python.

main.py


from iota import iota

if __name__ == '__main__':
    print(iota(10))

iota.py


def iota(m, n=0, step=1):
    if n >= m:
        return ()
    return (n, ) + iota(m, n + step, step)

Python doesn't normally use recursion, but functional languages are recursion. However, ** it is not tail recursion **, so I will refuse that it is a code that is not good for Lisp. By the way, when using tail recursion in Hy, [loop / recur](http: // docs. hylang.org/en/latest/contrib/loop.html) Use macros.

So, let's get back to the main subject. Execute the following command from the terminal. This time, we will build with the name ʻexample`.

$ hypo -o example main.hy iota.hy

Then, a file called ʻexample` is created in the current directory, so try executing it.

$ ./example
(0L 1L 2L 3L 4L 5L 6L 7L 8L 9L)

The contents of main.hy are executed. At this time, the file compiled to .pyc is called inside the executable file, so it is a little faster than executing from the Hy interpreter. In addition, it is ** Hy specifications ** that the numerical output is not good.

Summary

I introduced how to use ** Hy source code is put together into an executable format ** with a miso-like feeling. It is not suitable for making very large-scale applications, but I hope you will use it. It is a package that I wrote in a few hours to kill time on holidays, so if you have a bug or request, please do pull request [issue](https: // github.com/koji-kojiro/hylang-hypo/issues) Please throw it. Thank you for reading this far.

Recommended Posts

I made a package to create an executable file from Hy source code
I made a tool to create a word cloud from wikipedia
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
Flow from source code to creating executable
Create a deb file from a python package
Create an executable file in a scripting language
I made a tool to compile Hy natively
I made a tool to generate Markdown from the exported Scrapbox JSON file
I made a code to convert illustration2vec to keras model
I made an action to automatically format python code
How to create a kubernetes pod from python code
I wrote Python code to create a table (view) dependency diagram (PlantUML) from SQL
I made a plug-in from the Japan Meteorological Agency GPV to easily create an animated contour diagram with QGIS.
I made a package to filter time series with python
I want to install a package from requirements.txt with poetry
I made you to execute a command from a web browser
I made a tool to automatically generate a simple ER diagram from the CREATE TABLE statement
How to create a Conda package
How to create a config file
Python> I made a test code for my own external file
I want to make a parameter list from CloudFormation code (yaml)
I made a subtitle file (SRT) from JSON data of AmiVoice
I made a library that adds docstring to a Python stub file.
I came up with a way to create a 3D model from a photo Part 01 Creating an environment
I made a POST script to create an issue on Github and register it in the Project
I tried to create a linebot (preparation)
I made a script to display emoji
How to create a repository from media
I made a tool to convert Jupyter py to ipynb with VS Code
Script to create a Mac dictionary file
I made a program to check the size of a file in Python
I made a configuration file with Python
Use BeautifulSoup to extract a link containing a string from an HTML file
I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)
Aggregate steps by day from iPhone healthcare data to create a CSV file
I made an appdo command to execute a command in the context of the app
I tried to extract a line art from an image with Deep Learning
Edit Excel from Python to create a PivotTable
I made a python dictionary file for Neocomplete
I want to easily create a Noise Model
I made an extenum package that extends an enum
How to create a function object from a string
I want to create a window in Python
How to create a JSON file in Python
I want to create a plug-in type implementation
I made a tool to get new articles
I want to make C ++ code from Python code!
I want to write to a file with Python
Create a Word Cloud from an academic program
I made a QR code image with CuteR
I made a small donation to the non-profit organization "Open Source Robot Foundation" OSRF
Read the Python-Markdown source: How to create a parser
How to create an article from the command line
I tried to create a table only with Django
A tool that makes a standalone jar an executable file
I tried running python etc. from a bat file
I made a script to put a snippet in README.md
I made a Python module to translate comment outs
I want to find a popular package on PyPi
I want to randomly sample a file in Python
How to turn a .py file into an .exe file