Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pretty print nested dicts in julia?

Tags:

julia

How to pretty print nested dicts (or other data structures) in julia? For instance this:

xx = Dict(
    "a"=>77, 
    "b"=>55,
    "c"=> Dict(
        44=>"alfa",
        55=>"beta",
        66=>Dict(
            "x"=>999,
            "y"=>888
        )
    )
)
like image 245
tlama Avatar asked Jan 10 '18 20:01

tlama


People also ask

How do you pretty print dictionary values using which modules and function?

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable.

How do you print a dictionary indent in Python?

Pretty Print a Dict in Python with pprint Using this library, we can print out more nicely formatted data structures, including dictionaries. The pprint library allows us to specify a number of different parameters, including the following: indent : the number of spaces to indent each line, where the default value is 1.


2 Answers

You could use JSON.jl and the mostly undocumented indent argument, eg

julia> using JSON

julia> print(json(xx,4))
{
    "c": {
        "55": "beta",
        "66": {
            "x": 999,
            "y": 888
        },
        "44": "alfa"
    },
    "b": 55,
    "a": 77
}
like image 148
gggg Avatar answered Sep 29 '22 22:09

gggg


"Pretty" lies, of course, in the eye of the beholder, but why not simply do something like this? (I am not aware of a preimplemented function)

function pretty_print(d::Dict, pre=1)
    for (k,v) in d
        if typeof(v) <: Dict
            s = "$(repr(k)) => "
            println(join(fill(" ", pre)) * s)
            pretty_print(v, pre+1+length(s))
        else
            println(join(fill(" ", pre)) * "$(repr(k)) => $(repr(v))")
        end
    end
    nothing
end

Your example above would be printed as follows.

julia> pretty_print(xx)
 "c" =>
         55 => "beta"
         66 =>
                "x" => 999
                "y" => 888
         44 => "alfa"
 "b" => 55
 "a" => 77

Alternative version that pretty prints level by level

function pretty_print2(d::Dict, pre=1)
    todo = Vector{Tuple}()
    for (k,v) in d
        if typeof(v) <: Dict
            push!(todo, (k,v))
        else
            println(join(fill(" ", pre)) * "$(repr(k)) => $(repr(v))")
        end
    end

    for (k,d) in todo
        s = "$(repr(k)) => "
        println(join(fill(" ", pre)) * s)
        pretty_print2(d, pre+1+length(s))
    end
    nothing
end

Output:

julia> pretty_print2(xx)
 "b" => 55
 "a" => 77
 "c" =>
         55 => "beta"
         44 => "alfa"
         66 =>
                "x" => 999
                "y" => 888

Maybe not very general and performant but you get the idea.

EDIT: I forgot to print the keys of subdictionaries. I'll leave it to the reader to adjust the function in this regard.

EDIT2: Per request, I adjusted the original version to also print keys of subdictionaries and added a (maybe prettier version) that outputs level by level.

like image 36
carstenbauer Avatar answered Sep 29 '22 23:09

carstenbauer