sorting output dicts in doctests

55 views
Skip to first unread message

John Cremona

unread,
Feb 4, 2021, 12:06:52 PM2/4/21
to SAGE devel
Can someone remind me the preferred way to get doctests to pass when
the output -- which we do want to include in the docstring -- includes
a dict whose keys come out in random order?

John

Travis Scrimshaw

unread,
Feb 4, 2021, 6:34:36 PM2/4/21
to sage-devel
Hi John,
    IIRC, a dict has its keys automatically sorted in the output. However, if the dict is inside of another object, then no sorting is done. So if it is a simple dict with a total ordering on the keys, then you can simply put the output. You can also run sorted() on the items if they dicts are inside of other objects. Otherwise you might be better off testing the individual aspects you want, such as specific values are correct. Could you point to which ticket and/or test you want?

Best,
Travis

Reimundo Heluani

unread,
Feb 4, 2021, 6:49:49 PM2/4/21
to 'Travis Scrimshaw' via sage-devel
In addition to that I've been using the idiom

MyOutput == {this : dict}
True

And that works for tests and renders well on documents

R
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

John Cremona

unread,
Feb 5, 2021, 4:20:30 AM2/5/21
to sage-devel
Thanks for the suggestions (which I did not see right away as I had my group preference set to daily digest -- it's annoying that google does not make ex exception for replies to one's own posts!)

Travis, this relates to ticket #30000 which I should be uploading code for today, so there is nothing to see there yet.  The output to one function will be a pair, True/False and a dict, so this fits with what you said.  What I will do is assign the two parts to variables and display each separately.  I prefer that to the second suggestion, though that would certainly work too.

John

Samuel Lelievre

unread,
Feb 7, 2021, 12:02:23 PM2/7/21
to sage-devel
Another option might be to use an OrderedDict.

If I understand correctly, this becomes unnecessary
in Python >= 3.7 where dict behaves like OrderedDict.

If I understand correctly, Sage 9.3 will still support
Python 3.6 but Sage 9.4 probably won't, after

- Sage Trac ticket 30551
  Drop Python 3.6 support


Travis Scrimshaw

unread,
Feb 7, 2021, 6:17:58 PM2/7/21
to sage-devel
Hi John,

Travis, this relates to ticket #30000 which I should be uploading code for today, so there is nothing to see there yet.  The output to one function will be a pair, True/False and a dict, so this fits with what you said.  What I will do is assign the two parts to variables and display each separately.  I prefer that to the second suggestion, though that would certainly work too.

In this case, it might also be good to instead (or also) verify the certificate through tests. That way to get to further illustrate your code and it is a less brittle doctest.

Best,
Travis

John Cremona

unread,
Feb 8, 2021, 3:44:24 AM2/8/21
to SAGE devel
#30000 is now under review, so if any one thinks that the tests I put
in there need changing they can say so on trac.

John

On Sun, 7 Feb 2021 at 23:18, 'Travis Scrimshaw' via sage-devel
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/f27b35cf-66f7-48d9-a9bc-e1fd486556afn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages