Re: display of dicts?

Thomas Kluyver <[email protected]> Tue, 19 Dec 2017 11:21:53 +0000
Newsgroups gmane.comp.python.ipython.devel
Message-ID <CAOvn4qiCwT1TkUvEDL6HowW7j=NPZtcpn1H1WG-Js--xZ-CbtQ@mail.gmail.com>
--===============2216566572876502283==
Content-Type: multipart/alternative; boundary="001a1140127c5328560560afae97"

--001a1140127c5328560560afae97
Content-Type: text/plain; charset="UTF-8"

I think this is a tricky one, as the discussion on Python-dev is finding.
Removing the sorting makes it easier to explain how dictionaries now work.
But for a lot of real-world use cases, insertion order is not meaningful,
and displaying a sorted dictionary is still going to be more useful.

We'll probably have to make it optional, but I don't think it's at all
obvious which should be the default. I think the sorting is probably
preferable more of the time, but when trying to teach people about
insertion order, you really don't want to have to switch to non-default
behaviour.

On 19 December 2017 at 04:19, Chris Barker <[email protected]> wrote:

>
>
> On Mon, Dec 18, 2017 at 7:04 PM, Nathan Goldbaum <[email protected]>
> wrote:
>
>> Nope, I believe it's this: https://github.com/ipython/ipy
>> thon/blob/dc9d1c679424589480bb83af64aa5ee21031d311/IPython/lib/pretty.py
>>
>
> I see, thanks! Though it does have the same behavior of sorting dicts --
> you can see that in:
>
> ``_dict_pprinter_factory``
>
> However as of cPYthon 3.6, and officially as of 3.7, dicts will maintain
> their insertion order.
>
> So we should probably remove the sorting from the _dict_pprinter_factory.
>
> -CHB
>
>
>
>
>>
>>
>> On Mon, Dec 18, 2017 at 7:56 PM, Chris Barker <[email protected]>
>> wrote:
>>
>>> Thanks Nathan,
>>>
>>> And to confirm, this is the stdlib's pprint module, yes?
>>>
>>> (which does seem to show the same behavior)
>>>
>>> -CHB
>>>
>>>
>>>
>>> On Mon, Dec 18, 2017 at 5:40 PM, Nathan Goldbaum <[email protected]>
>>> wrote:
>>>
>>>> IPython does use pretty-printing by default. You can control it with
>>>> the %pprint magic, in your IPython configuration, with the
>>>> PlainTextFormatter.pprint option, or with the --pprint command line
>>>> argument when starting IPython.
>>>>
>>>> http://ipython.readthedocs.io/en/stable/config/options/termi
>>>> nal.html#configtrait-PlainTextFormatter.pprint
>>>>
>>>>
>>>>
>>>> On Mon, Dec 18, 2017 at 7:08 PM, Chris Barker <[email protected]>
>>>> wrote:
>>>>
>>>>> As Guido has just declared that dicts will now officially preserve
>>>>> order:
>>>>>
>>>>> https://mail.python.org/pipermail/python-dev/2017-December/151283.html
>>>>>
>>>>> I was playing around  them in py3.6 ipython, and found some (to me)
>>>>> odd behavior:
>>>>>
>>>>> In [1]: d = {'one':1, 'two':2, 'three':3}
>>>>>
>>>>> In [2]: d
>>>>> Out[2]: {'one': 1, 'three': 3, 'two': 2}
>>>>>
>>>>> Hmm -- order does not appear to be preserved.
>>>>>
>>>>> But then:
>>>>>
>>>>> In [3]: str(d)
>>>>> Out[3]: "{'one': 1, 'two': 2, 'three': 3}"
>>>>>
>>>>> In [4]: repr(d)
>>>>> Out[4]: "{'one': 1, 'two': 2, 'three': 3}"
>>>>>
>>>>> In [5]: d.values()
>>>>> Out[5]: dict_values([1, 2, 3])
>>>>>
>>>>> In [6]: d.keys()
>>>>> Out[6]: dict_keys(['one', 'two', 'three'])
>>>>>
>>>>> In [7]: d.items()
>>>>> Out[7]: dict_items([('one', 1), ('two', 2), ('three', 3)])
>>>>>
>>>>> Order IS preserved.
>>>>>
>>>>> So presumably iPython is calling sorted() or some such when displaying
>>>>> a dict.
>>>>>
>>>>> Is that that case? Is that documented anywhere?? I can't find it.
>>>>>
>>>>> And with Python >= 3.6, dict order is preserved, so it would probably
>>>>> be better to NOT mess with dict order when displaying them in iPython.
>>>>>
>>>>> SIDE NOTE:
>>>>>
>>>>> I had a bit of trouble finding this mailing list -- google still
>>>>> points to the old ones on scipy.org. -- maybe we can put a note on
>>>>> the home page of those lists that they are been moved??
>>>>>
>>>>> (I only noticed, 'cause the archives of those stop last March)
>>>>>
>>>>> -Chris
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>> Christopher Barker, Ph.D.
>>>>> Oceanographer
>>>>>
>>>>> Emergency Response Division
>>>>> NOAA/NOS/OR&R            (206) 526-6959   voice
>>>>> 7600 Sand Point Way NE   (206) 526-6329   fax
>>>>> Seattle, WA  98115       (206) 526-6317   main reception
>>>>>
>>>>> [email protected]
>>>>>
>>>>> _______________________________________________
>>>>> IPython-dev mailing list
>>>>> [email protected]
>>>>> https://mail.python.org/mailman/listinfo/ipython-dev
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> IPython-dev mailing list
>>>> [email protected]
>>>> https://mail.python.org/mailman/listinfo/ipython-dev
>>>>
>>>>
>>>
>>>
>>> --
>>>
>>> Christopher Barker, Ph.D.
>>> Oceanographer
>>>
>>> Emergency Response Division
>>> NOAA/NOS/OR&R            (206) 526-6959   voice
>>> 7600 Sand Point Way NE   (206) 526-6329   fax
>>> Seattle, WA  98115       (206) 526-6317   main reception
>>>
>>> [email protected]
>>>
>>> _______________________________________________
>>> IPython-dev mailing list
>>> [email protected]
>>> https://mail.python.org/mailman/listinfo/ipython-dev
>>>
>>>
>>
>> _______________________________________________
>> IPython-dev mailing list
>> [email protected]
>> https://mail.python.org/mailman/listinfo/ipython-dev
>>
>>
>
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> [email protected]
>
> _______________________________________________
> IPython-dev mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/ipython-dev
>
>

--001a1140127c5328560560afae97
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I think this is a tricky one, as the discussion on Python-=
dev is finding. Removing the sorting makes it easier to explain how diction=
aries now work. But for a lot of real-world use cases, insertion order is n=
ot meaningful, and displaying a sorted dictionary is still going to be more=
 useful.<br><br>We&#39;ll probably have to make it optional, but I don&#39;=
t think it&#39;s at all obvious which should be the default. I think the so=
rting is probably preferable more of the time, but when trying to teach peo=
ple about insertion order, you really don&#39;t want to have to switch to n=
on-default behaviour.<br></div><div class=3D"gmail_extra"><br><div class=3D=
"gmail_quote">On 19 December 2017 at 04:19, Chris Barker <span dir=3D"ltr">=
&lt;<a href=3D"mailto:[email protected]" target=3D"_blank">chris.barker=
@noaa.gov</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=
=3D"ltr"><br><div class=3D"gmail_extra"><br><div class=3D"gmail_quote"><spa=
n class=3D"">On Mon, Dec 18, 2017 at 7:04 PM, Nathan Goldbaum <span dir=3D"=
ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank">nathan1=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pad=
ding-left:1ex"><div dir=3D"ltr">Nope, I believe it&#39;s this: <a href=3D"h=
ttps://github.com/ipython/ipython/blob/dc9d1c679424589480bb83af64aa5ee21031=
d311/IPython/lib/pretty.py" target=3D"_blank">https://github.com/ipython/ip=
y<wbr>thon/blob/dc9d1c679424589480bb<wbr>83af64aa5ee21031d311/IPython/<wbr>=
lib/pretty.py</a></div></blockquote><div><br></div></span><div>I see, thank=
s! Though it does have the same behavior of sorting dicts -- you can see th=
at in:<br><br><span class=3D"m_4490167575152119102gmail-pl-en">``_dict_ppri=
nter_factory``</span></div><div><br></div><div>However as of cPYthon 3.6, a=
nd officially as of 3.7, dicts will maintain their insertion order.<br><br>=
</div><div>So we should probably remove the sorting from the <span class=3D=
"m_4490167575152119102gmail-pl-en">_dict_pprinter_factory.<br><br></span></=
div><div><div class=3D"h5"><div><span class=3D"m_4490167575152119102gmail-p=
l-en">-CHB<br><br></span></div><div><br>=C2=A0</div><blockquote class=3D"gm=
ail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,=
204,204);padding-left:1ex"><div dir=3D"ltr"><br></div><div class=3D"m_44901=
67575152119102gmail-HOEnZb"><div class=3D"m_4490167575152119102gmail-h5"><d=
iv class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Mon, Dec 18, 201=
7 at 7:56 PM, Chris Barker <span dir=3D"ltr">&lt;<a href=3D"mailto:chris.ba=
[email protected]" target=3D"_blank">[email protected]</a>&gt;</span> wrote=
:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><di=
v><div><div>Thanks Nathan,<br><br></div>And to confirm, this is the stdlib&=
#39;s pprint module, yes?<br><br></div>(which does seem to show the same be=
havior)<br><br></div>-CHB<br><br><div><div><br></div></div></div><div class=
=3D"m_4490167575152119102gmail-m_-3730345828008721064HOEnZb"><div class=3D"=
m_4490167575152119102gmail-m_-3730345828008721064h5"><div class=3D"gmail_ex=
tra"><br><div class=3D"gmail_quote">On Mon, Dec 18, 2017 at 5:40 PM, Nathan=
 Goldbaum <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" ta=
rget=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><blockquote =
class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px sol=
id rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">IPython does use pre=
tty-printing by default. You can control it with the %pprint magic, in your=
 IPython configuration, with the PlainTextFormatter.pprint option, or with =
the --pprint command line argument when starting IPython.<br><br><a href=3D=
"http://ipython.readthedocs.io/en/stable/config/options/terminal.html#confi=
gtrait-PlainTextFormatter.pprint" target=3D"_blank">http://ipython.readthed=
ocs.io/<wbr>en/stable/config/options/termi<wbr>nal.html#configtrait-PlainTe=
xt<wbr>Formatter.pprint</a><br><br><br></div><div class=3D"gmail_extra"><br=
><div class=3D"gmail_quote"><div><div class=3D"m_4490167575152119102gmail-m=
_-3730345828008721064m_-6421650856597578850h5">On Mon, Dec 18, 2017 at 7:08=
 PM, Chris Barker <span dir=3D"ltr">&lt;<a href=3D"mailto:chris.barker@noaa=
.gov" target=3D"_blank">[email protected]</a>&gt;</span> wrote:<br></di=
v></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex=
;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class=
=3D"m_4490167575152119102gmail-m_-3730345828008721064m_-6421650856597578850=
h5"><div dir=3D"ltr"><div>As Guido has just declared that dicts will now of=
ficially preserve order:<br clear=3D"all"><div><br><a href=3D"https://mail.=
python.org/pipermail/python-dev/2017-December/151283.html" target=3D"_blank=
">https://mail.python.org/piperm<wbr>ail/python-dev/2017-December/1<wbr>512=
83.html</a><br><br></div><div>I was playing around=C2=A0 them in py3.6 ipyt=
hon, and found some (to me) odd behavior:<br><br><span style=3D"font-family=
:monospace,monospace">In [1]: d =3D {&#39;one&#39;:1, &#39;two&#39;:2, &#39=
;three&#39;:3}<br><br>In [2]: d<br>Out[2]: {&#39;one&#39;: 1, &#39;three&#3=
9;: 3, &#39;two&#39;: 2}<br><br></span></div>Hmm -- order does not appear t=
o be preserved.<br><br></div>But then:<span style=3D"font-family:monospace,=
monospace"></span><br><div><span style=3D"font-family:monospace,monospace">=
</span><div><span style=3D"font-family:monospace,monospace"><br>In [3]: str=
(d)<br>Out[3]: &quot;{&#39;one&#39;: 1, &#39;two&#39;: 2, &#39;three&#39;: =
3}&quot;<br><br>In [4]: repr(d)<br>Out[4]: &quot;{&#39;one&#39;: 1, &#39;tw=
o&#39;: 2, &#39;three&#39;: 3}&quot;<br><br>In [5]: d.values()<br>Out[5]: d=
ict_values([1, 2, 3])<br><br>In [6]: d.keys()<br>Out[6]: dict_keys([&#39;on=
e&#39;, &#39;two&#39;, &#39;three&#39;])<br><br>In [7]: d.items()<br>Out[7]=
: dict_items([(&#39;one&#39;, 1), (&#39;two&#39;, 2), (&#39;three&#39;, 3)]=
)<br></span><br></div><div>Order IS preserved.<br><br></div><div>So presuma=
bly iPython is calling sorted() or some such when displaying a dict. <br><b=
r></div><div>Is that that case? Is that documented anywhere?? I can&#39;t f=
ind it.<br><br></div><div>And with Python &gt;=3D 3.6, dict order is preser=
ved, so it would probably be better to NOT mess with dict order when displa=
ying them in iPython.<br><br></div><div>SIDE NOTE:<br></div><div><br>I had =
a bit of trouble finding this mailing list -- google still points to the ol=
d ones on <a href=3D"http://scipy.org" target=3D"_blank">scipy.org</a>. -- =
maybe we can put a note on the home page of those lists that they are been =
moved??<br><br></div><div>(I only noticed, &#39;cause the archives of those=
 stop last March)<span class=3D"m_4490167575152119102gmail-m_-3730345828008=
721064m_-6421650856597578850m_3404512757170262343HOEnZb"><font color=3D"#88=
8888"><br><br></font></span></div><span class=3D"m_4490167575152119102gmail=
-m_-3730345828008721064m_-6421650856597578850m_3404512757170262343HOEnZb"><=
font color=3D"#888888"><div>-Chris<br></div><div><br></div><div><br></div><=
div><br><br></div><div><br>-- <br><div class=3D"m_4490167575152119102gmail-=
m_-3730345828008721064m_-6421650856597578850m_3404512757170262343m_-4512783=
311611731130gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer=
<br><br>Emergency Response Division<br>NOAA/NOS/OR&amp;R =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"tel:%28206%29%20526-6959" value=3D"+1206=
5266959" target=3D"_blank">(206) 526-6959</a>=C2=A0=C2=A0 voice<br>7600 San=
d Point Way NE =C2=A0=C2=A0<a href=3D"tel:%28206%29%20526-6329" value=3D"+1=
2065266329" target=3D"_blank">(206) 526-6329</a>=C2=A0=C2=A0 fax<br>Seattle=
, WA =C2=A098115 =C2=A0 =C2=A0 =C2=A0=C2=A0<a href=3D"tel:%28206%29%20526-6=
317" value=3D"+12065266317" target=3D"_blank">(206) 526-6317</a>=C2=A0=C2=
=A0 main reception<br><br><a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a></div>
</div></font></span></div></div>
<br></div></div>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">IPython-dev@pyt=
hon.org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/mailma<wbr>n/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">IPython-dev@pyt=
hon.org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/mailma<wbr>n/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div><br><br clear=3D"all"><br>-- <br><div class=3D"m_449=
0167575152119102gmail-m_-3730345828008721064m_-6421650856597578850gmail_sig=
nature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Res=
ponse Division<br>NOAA/NOS/OR&amp;R =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0<a href=3D"tel:%28206%29%20526-6959" value=3D"+12065266959" target=3D"_b=
lank">(206) 526-6959</a>=C2=A0=C2=A0 voice<br>7600 Sand Point Way NE =C2=A0=
=C2=A0<a href=3D"tel:%28206%29%20526-6329" value=3D"+12065266329" target=3D=
"_blank">(206) 526-6329</a>=C2=A0=C2=A0 fax<br>Seattle, WA =C2=A098115 =C2=
=A0 =C2=A0 =C2=A0=C2=A0<a href=3D"tel:%28206%29%20526-6317" value=3D"+12065=
266317" target=3D"_blank">(206) 526-6317</a>=C2=A0=C2=A0 main reception<br>=
<br><a href=3D"mailto:[email protected]" target=3D"_blank">Chris.Barker=
@noaa.gov</a></div>
</div>
</div></div><br>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">IPython-dev@pyt=
hon.org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/mailma<wbr>n/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">IPython-dev@pyt=
hon.org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/mailma<wbr>n/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div></div></div><div><div class=3D"h5"><br><br clear=3D"=
all"><br>-- <br><div class=3D"m_4490167575152119102gmail_signature"><br>Chr=
istopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<=
br>NOAA/NOS/OR&amp;R =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(206) 526-695=
9=C2=A0=C2=A0 voice<br>7600 Sand Point Way NE =C2=A0=C2=A0(206) 526-6329=C2=
=A0=C2=A0 fax<br>Seattle, WA =C2=A098115 =C2=A0 =C2=A0 =C2=A0=C2=A0(206) 52=
6-6317=C2=A0=C2=A0 main reception<br><br><a href=3D"mailto:Chris.Barker@noa=
a.gov" target=3D"_blank">[email protected]</a></div>
</div></div></div></div>
<br>______________________________<wbr>_________________<br>
IPython-dev mailing list<br>
<a href=3D"mailto:[email protected]">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/ipython-dev" rel=3D"nor=
eferrer" target=3D"_blank">https://mail.python.org/<wbr>mailman/listinfo/ip=
ython-dev</a><br>
<br></blockquote></div><br></div>

--001a1140127c5328560560afae97--

--===============2216566572876502283==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
IPython-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/ipython-dev

--===============2216566572876502283==--