PrettyPrinting.jl

Julia library for optimal formatting of composite data structures
Popularity
73 Stars
Updated Last
5 Months Ago
Started In
November 2018

PrettyPrinting.jl

Build Status Code Coverage Status Open Issues Documentation MIT License

PrettyPrinting is a Julia library for optimal formatting of composite data structures. It works by generating all possible layouts of the data, and then selecting the best layout that fits the screen width. The algorithm for finding the optimal layout is based upon Phillip Yelland, A New Approach to Optimal Code Formatting, 2016.

Out of the box, PrettyPrinting can format Julia code and standard Julia containers. It can be easily extended to format custom data structures.

To learn more about PrettyPrinting, check the Quick Start below, read the Documentation, or watch the Presentation at JuliaCon 2021 (slides).

PrettyPrinting | JuliaCon 2021

Quick Start

If you work with nested data structures in Julia REPL, you may find the way they are displayed unsatisfactory. For example:

julia> data = [(name = "POLICE",
                employees = [(name = "JEFFERY A", position = "SERGEANT", salary = 101442, rate = missing),
                             (name = "NANCY A", position = "POLICE OFFICER", salary = 80016, rate = missing)]),
               (name = "OEMC",
                employees = [(name = "LAKENYA A", position = "CROSSING GUARD", salary = missing, rate = 17.68),
                             (name = "DORIS A", position = "CROSSING GUARD", salary = missing, rate = 19.38)])]
2-element Vector{NamedTuple{(:name, :employees), T} where T<:Tuple}:
 (name = "POLICE", employees = NamedTuple{(:name, :position, :salary, :rate), Tuple{String, String, Int64, Missing}}[(name = "JEFFERY A", position = "SERGEANT", salary = 101442, rate = missing), (name = "NANCY A", position = "POLICE OFFICER", salary = 80016, rate = missing)])
 (name = "OEMC", employees = NamedTuple{(:name, :position, :salary, :rate), Tuple{String, String, Missing, Float64}}[(name = "LAKENYA A", position = "CROSSING GUARD", salary = missing, rate = 17.68), (name = "DORIS A", position = "CROSSING GUARD", salary = missing, rate = 19.38)])

If this is the case, consider using PrettyPrinting. First, install it with the Julia package manager:

julia> using Pkg
julia> Pkg.add("PrettyPrinting")

Import the package:

julia> using PrettyPrinting

Now you can use pprint() to display your complex data. For example:

julia> pprint(data)
[(name = "POLICE",
  employees = [(name = "JEFFERY A",
                position = "SERGEANT",
                salary = 101442,
                rate = missing),
               (name = "NANCY A",
                position = "POLICE OFFICER",
                salary = 80016,
                rate = missing)]),
 (name = "OEMC",
  employees = [(name = "LAKENYA A",
                position = "CROSSING GUARD",
                salary = missing,
                rate = 17.68),
               (name = "DORIS A",
                position = "CROSSING GUARD",
                salary = missing,
                rate = 19.38)])]

PrettyPrinting knows how to format tuples, named tuples, vectors, sets, and dictionaries. It can also format Julia code represented as an Expr object. To format custom data types, implement either PrettyPrinting.quoteof() or PrettyPrinting.tile(), as described in the Documentation.

Required Packages

No packages found.