#15981: Python 3 preparation: Adapt dict-methods keys(), items(), values() etc.
--------------------------------+------------------------
Reporter: wluebbe | Owner:
Type: enhancement | Status: new
Priority: major | Milestone: sage-6.2
Component: distribution | Resolution:
Keywords: python3 | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: | Stopgaps:
--------------------------------+------------------------
Comment (by wluebbe):
Here are the (approximate) numbers for the dict-methods in the Sage .py
modules:
|| |||| case: .xxxx() |||| case: .iterxxxx() ||
||xxxx (method) || nbr lines || nbr calls || nbr lines || nbr calls ||
||keys() || 414 || 430 || 19 || 19 ||
||values() || 216 || 216 || 15 || 15 ||
||items() || 284 || 330 || 293 || 295 ||
No .**view**xxxx() were found
The effect of 2to3 fix_dict is:
* {{{d.xxxx()}}} is converted to {{{list(d.xxxx())}}}.
* For Py2.7 the original code gives a list.
[[br]]The converted code gives also a list.
[[br]]//But the converted code performs an EXTRA dictionary copy! //
* For Py3.3 the original code would be a view (not a list) and therefor
**different** from the Py2.7 behavior.
[[br]]The converted code gives a list (as in Py 2.7 for both code
variants).
* {{{d.iterxxxx()}}} is converted to {{{iter(d.xxxx())}}}.
* For Py2.7 the original code gives an iterator.
[[br]]The converted code results in an iterator -
[[br]]//but this iterator is created ON TOP of the list returned by
d.xxxx().//
* For Py3.3 the original code is **not excepted**.
[[br]]The converted code gives an iterator on a view (and is similar
to the iterators in Py 2.7 for both code variants).
In summary
* the original code in Py 2.7 //is equivalent to//
* the converted code in Py 2.7 //is equivalent to//
* the converted code in Py 3.3
In both cases (.xxxx() and .iterxxxx()) the converted code suffers an
overhead in Py2.7!
The {{{six}}} library module offers a solution without this overhead: it
defines 3 new **functions**: {{{iterkeys(d), itervalues(d),
iteritems(d)}}}. In Py2.7 they are implemented by {{{d.iterkeys(),
d.itervalues(), d.iteritems()}}} and in Py3.3 by {{{d.keys(), d.values(),
d.items()}}}.
[[br]]The catch is that the code must be manually modified (use the new
functions in place of the old dictinary methods) - and the module
{{{six}}} must be imported.
What to do?
* The approaches (2to3 and six) could be combined.
* And in some places/contexts the wrapping {{{list(d.xxxx())}}} could be
removed manually when a list is not required.
* Another option is sometimes to use {{{key in d}}} together with
{{{d[key]}}} as appropriate.
This code has the same behavior in Py2.7 and Py3.3. But manual code
change is required
There is no undisputed **best** solution :-(
[[br]]Suggestions are welcome!
--
Ticket URL: <http://trac.sagemath.org/ticket/15981#comment:1>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.