All Projects → MechanicalRabbit → PrettyPrinting.jl

MechanicalRabbit / PrettyPrinting.jl

Licence: MIT license
Julia library for optimal formatting of composite data structures

Programming Languages

julia
2034 projects

Projects that are alternatives of or similar to PrettyPrinting.jl

errata
Source code error pretty printing
Stars: ✭ 41 (-29.31%)
Mutual labels:  pretty-printing
pretty-d-array
Pretty printing multidimensional D arrays.
Stars: ✭ 16 (-72.41%)
Mutual labels:  pretty-printing
Dbg Macro
A dbg(…) macro for C++
Stars: ✭ 1,825 (+3046.55%)
Mutual labels:  pretty-printing

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].