Human-readable but precise printing of data

question

#1

How should I print data in Julia so that

  1. the output contains all the information (full precision for floats, type information for literals, etc),
  2. the output is plain text.

Eg with show, type information and precision is lost:

julia> d = Dict(:a => √2, :b => fill(1/3, 3), :c => 1f0)
Dict{Symbol,Any} with 3 entries:
  :c => 1.0
  :a => 1.41421
  :b => [0.333333,0.333333,0.333333]

while serialize does preserve everything, but is not plain text. I would be satisfied with something that handles everything but functions/closures.


#2

Accidently I think I have an answer for you… just messed up with it a little bit myself.
So digging through the julia base I think this is what you are looking for:

sprintf_exact(x) = sprint(0,print,x; env = (:compact => false))

for your simple example it works.
lets validate it:

julia> d = Dict(:a => √2, :b => fill(1/3, 3), :c => 1f0);

julia> f = eval(parse(sprintf_exact(d)));

julia> f === d
false

julia> f == d
true

#3

Thanks. I think that adding this as print_parseable or something similar would be worthwhile, but I need to understand Julia’s printing system in depth before opening an issue.


#4

print is already parsable … I think it is by design.
to get full precision in print use:

print(IOContext(STDOUT;compact = false) , 1/3)

if you are not worried about precision then sprintf_exact boils down to
sprint(print,x)


#5

I believe repr(x) (equivalent to sprint(showall, x)) and showall(x) are closer to what you want.


#6

To revive this topic: I have been using

showfull(io, x) = show(IOContext(io; compact = false, limit = false), x)
showfull(x) = showfull(STDOUT, x)

for a while. Would inclusion in Base make sense?