-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A more nicely formatted output for the display()
function with NamedTuple
s
#50063
base: master
Are you sure you want to change the base?
Conversation
Oooh - interesting. One of the reasons for the way it currently is, is that it mimics actual sytax - it is kind of like One point in favour is one thing I love to do with named tuples is use them in a |
A possible style here would be: julia> nt = (a=3, b=5, g=5, d=9,)
4-element NamedTuple(
a = 3,
b = 5,
g = 5,
d = 9,
)
julia> nt1 = (a=3,)
1-element NamedTuple(a = 3) At present Such printing could also be recursive, while remaining valid syntax. (I wrote something like this for Flux.jl, which in fact recurses into almost arbitrary structs.) It's possible that very simple NamedTuples should not trigger the multi-line printing. Above I put a 1-element case on one line, that would be a simple rule... but it could also go by whether the 1-line printout fits into a terminal line, or something. |
You could also do:
which is readable but also gives you something copy-pasteable and tuple-looking. |
Thanks for the input everyone! And sorry for the delay in answering. Life is busy 😬
I personally don't see a problem with the way |
I think I like this better because it looks like the actual syntax used to create the object and, like @stevengj points out, it's copy-pasteable and very readable. |
For now I implemented something very close to @stevengj's suggestion (but with an extra colon). Short NTs are displayed like: julia> NamedTuple( Symbol(:El, el)=>el for el in 1:4 )
4-element NamedTuple:
(El1 = 1
El2 = 2
El3 = 3
El4 = 4) And longer ones (that don't fit in the REPL's rows) are displayed like: julia> NamedTuple( Symbol(:El, el)=>el for el in 1:11 )
11-element NamedTuple:
(El1 = 1
El2 = 2
El3 = 3
El4 = 4
El5 = 5
El6 = 6
El7 = 7
El8 = 8
El9 = 9
⋮ = ⋮ ) (Note that the last line (the one with Thoughts? |
Trying this out: julia> (a=1,) # needs the trailing comma
1-element NamedTuple:
(a = 1)
julia> (a=1, bb=(x=10, yy=20), ccc_ccc=3) # should this be recursive?
3-element NamedTuple:
(a = 1
bb = (x = 10, yy = 20)
ccc_ccc = 3)
julia> NamedTuple(Symbol(i) => i^2 for i in 1:100) # symbols broken
100-element NamedTuple:
(ymbol("1") = 1
ymbol("2") = 4
ymbol("3") = 9
⋮ = ⋮ )
|
A lot of those questions seem better suited to reasons to use the PrettyPrinting package, though perhaps that should be what the REPL does, it is different direction to a solution. |
This is a good point. There could be various (e.g. performance, egonomics) issues with riduclously large named tuples; it seems pretty-printing a few too many lines to the REPL then would be the least of your issues. (OTOH it is trivial to type something like |
I agree with these points. I think for nice recursiveness/unfolding and non-truncating printing, PrettyPrinting is the obvious choice. In my mind, regarding this specific PR, the question is whether it's worth to implement a more simplified solution (i.e. one that does not unfold inner NamedTuples and that truncates) so that a user can still get a reasonable human-readable output in the REPL without installing an additional packages. For me, personally, the answer is yes, it's worth it. Especially since afak PrettyPrinting isn't an official Julia package (i.e. it's not hosted by JuliaLang, or JuliaMath, etc.) and since (like @andyferris mentioned). Thoughts? PS.: obviously some of the points highlighted by @mcabbott still need to be resolved first. |
This is an attempt to create an output for
display()
withNamedTuple
s that's more human-readable.My first attempt is an output that almost exactly matches that of
Dict
s:imo that could be good enough, and given that's virtually a copy-paste from
Dict
users should be familiar with it. But I'm hoping to have a discussion about it.Note that in the output above I'm purposefully omiting the colon (
:
) from theNamedTuple
's keys' symbols to mimic the syntax of howNamedTuple
s are defined (i.e. we define it asnt = (a=3, b=5)
, not asnt = (:a=3, :b=5)
).Closes #50004
CC @gbaraldi