Skip to main content

A Hy pretty-printer / code formatter / beautifier.

Project description

🦑 Beautifhy

A Hy beautifier / code formatter / pretty-printer.

Probably compatible with Hy 1.0.0 and later.

Install

$ pip install -U beautifhy

If you want syntax highlighting available (which requires pygments), do instead

$ pip install -U beautifhy[hylight]

Usage

From the command line, to pretty-print the file core.hy:

$ beautifhy core.hy

gives the output

(import toolz [first second last])

 ;; * Utility things
 ;; -----------------------------------------

(defmacro defmethod [#* args]
  "Define a multimethod (using multimethod.multimethod).
  For example, the Hy code

  `(defmethod f [#^ int x #^ float y]
    (// x (int y)))`

  is equivalent to the following Python code:

  `@multimethod
  def f(x: int, y: float):
      return await x // int(y)`

  You can also define an asynchronous multimethod:

  `(defmethod :async f [#* args #** kwargs]
    (await some-async-function #* args #** kwargs))`
  "
  (if (= :async (first args))
    (let [f (second args) body (cut args 2 None)]
      `(defn :async [hy.I.multimethod.multimethod] ~f ~@body))
    (let [f (first args) body (cut args 1 None)]
      `(defn [hy.I.multimethod.multimethod] ~f ~@body))))


(defn slurp [fname #** kwargs]
  "Read a file and return as a string.
  kwargs can include mode, encoding and buffering, and will be passed
  to open()."
  (let [f (if (:encoding kwargs None) hy.I.codecs.open open)]
    (with [o (f fname #** kwargs)]
      (o.read))))


(defmacro rest [xs]
  "A slice of all but the first element of a sequence."
  `(cut ~xs 1 None))

To apply syntax highlighting (no pretty-printing), do

$ hylight core.hy

You can use stdin and pipe by replacing the filename with -:

$ beautifhy core.hy | hylight -

which will pretty-print core.hy and then syntax highlight the output.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

beautifhy-1.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beautifhy-1.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file beautifhy-1.1.0.tar.gz.

File metadata

  • Download URL: beautifhy-1.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for beautifhy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ec0439c0b1f44f4dfbaf5cb013462959cb89be8926ec673afd85d5cf746133f3
MD5 c16efba0e94ba39dbbbb5f7a7f784757
BLAKE2b-256 5100b05c0a69fdae4a22baffd29a39f62152ed7a2f7233e0dd56ffd095773c5d

See more details on using hashes here.

File details

Details for the file beautifhy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: beautifhy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for beautifhy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d98f767844e7c4128f11bccf9d0c7cf8bada1fdc4b76413c3e0cefae095e7f7
MD5 3d7ef58334664b3f8931fb8674828ce5
BLAKE2b-256 fff5868cd0aa67188ac6edf0c1b22e79307989c7a848353372783ecf51d30d0c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page