On Sep 1, 11:00 am, Simon King <simon.k...@nuigalway.ie> wrote:

> But then, what can one do? I.e., how can one safely doctest the
> contents of a dictionary D?

If the default choice of "sorted" is not portable, make sure to tell
the system to sort on something that is portable, like you suggest:

> At least in this example,
>   sage: sorted( [(repr(a),b) for a,b in D.iteritems()] )
> should work. But it doesn't seem very elegant to me.

which you can do this much more elegantly by:

sage: sorted(L,key=lambda l: str(l[0]))
[(2520, {0: 'X'}), (360, {}), ('prime', 3)]

It depends on '0'..'9' having lower "ord()" values than 'a'..'z' and
if you have two distinct keys that have the same string
representation, results will be undefined (the sort is guaranteed to
be stable, but the order in which python iterates through the
dictionary may be implementation dependent).

Note the subtle difference:
sage: sorted(L,key=lambda l: repr(l[0]))
[('prime', 3), (2520, {0: 'X'}), (360, {})]

Explained by:

sage: [repr('prime'),repr(2520)]
["'prime'", '2520']
sage: [ord("'"),ord("2")]
[39, 50]

If you want something sorted, it's really good to tell what you want
it sorted on. In Python 3, you'll have to make an explicit choice in
many cases.

The printing of that example made me wonder to how much trouble python
goes to print quotes around a string. This one tells it:

sage: "'"+'"'
'\'"'

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to