Opened 8 years ago
Closed 3 years ago
#15981 closed enhancement (fixed)
Python 3 preparation: Adapt dict-methods keys(), items(), values() etc.
Reported by: | wluebbe | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | sage-duplicate/invalid/wontfix |
Component: | python3 | Keywords: | python3 |
Cc: | embray, jdemeyer, fbissey, tscrim, jhpalmieri, vklein | Merged in: | |
Authors: | Reviewers: | ||
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
Python 3 introduces new dict-methods and changes the behavior of existing.
Changes according to lib2to3/fixes/fix_dict.py
:
d.keys() -> list(d.keys()) d.items() -> list(d.items()) d.values() -> list(d.values()) d.iterkeys() -> iter(d.keys()) d.iteritems() -> iter(d.items()) d.itervalues() -> iter(d.values()) d.viewkeys() -> d.keys() d.viewitems() -> d.items() d.viewvalues() -> d.values()
TODO: Clarify whether some generated changes need modifications to avoid unnecessary wrappings with list().
This ticket is tracked as a dependency of meta-ticket ticket:16052.
Change History (26)
comment:1 Changed 8 years ago by
comment:2 Changed 8 years ago by
If you have been wondering (like me) why 2to3 maps d.iteritems()
to iter(d.items())
and what is means in Py3, then you may benefit from this entry in stackoverflow.
comment:3 Changed 8 years ago by
I would personally move this to stage 2, since most likely we will want to use some sort of compatibility library for at least some instances (and I don't want to make a choice now of which library we will use).
comment:4 Changed 8 years ago by
- Description modified (diff)
comment:5 Changed 8 years ago by
- Milestone changed from sage-6.2 to sage-6.3
comment:6 Changed 8 years ago by
- Milestone changed from sage-6.3 to sage-6.4
comment:7 Changed 6 years ago by
- Component changed from distribution to python3
comment:8 Changed 6 years ago by
comment:9 Changed 6 years ago by
comment:10 Changed 6 years ago by
see #22298 for some work on iteritems
comment:11 Changed 5 years ago by
after #22485, there should remain no .view* and no .iter*.
comment:12 Changed 5 years ago by
- Milestone changed from sage-6.4 to sage-8.0
comment:13 Changed 5 years ago by
- Milestone changed from sage-8.0 to sage-8.1
Now we face the issue that .keys()[0]
, .items()[0]
and so on are not allowed.
First ticket for .items in #23824
comment:14 Changed 5 years ago by
next step in #23831
comment:15 Changed 5 years ago by
next in #24068 for .values()[something]
comment:16 Changed 5 years ago by
next in #24126 for some remaining .keys()[something]
comment:17 follow-up: ↓ 18 Changed 5 years ago by
- Cc jdemeyer added
Sigh.. it seems that we will also need to get rid of iteritems and the like in pyx files (python3-built-sage traceback):
sage: v=vector([1,0,2]) sage: list(v.iteritems()) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-29-e4acb1d548e2> in <module>() ----> 1 list(v.iteritems()) /home/chapoton/sage3/src/sage/modules/free_module_element.pyx in sage.modules.free_module_element.FreeModuleElement.iteritems (build/cythonized/sage/modules/free_module_element.c:12200)() AttributeError: 'dict' object has no attribute 'iteritems'
comment:18 in reply to: ↑ 17 Changed 5 years ago by
Follow-up: #24134
comment:19 Changed 5 years ago by
next step in #24181
comment:20 Changed 5 years ago by
next step in #24224
comment:21 Changed 5 years ago by
next in #24407
comment:22 Changed 4 years ago by
next in #25259
comment:23 Changed 3 years ago by
- Cc embray fbissey tscrim jhpalmieri vklein added
- Milestone changed from sage-8.1 to sage-duplicate/invalid/wontfix
- Status changed from new to needs_review
I propose to close this one also. Agreed ?
comment:24 Changed 3 years ago by
Yes i agree. I don't think this meta-ticket is needed anymore.
comment:25 Changed 3 years ago by
- Status changed from needs_review to positive_review
comment:26 Changed 3 years ago by
- Resolution set to fixed
- Status changed from positive_review to closed
Here are the (approximate) numbers for the dict-methods in the Sage .py modules:
No .viewxxxx() were found
The effect of 2to3 fix_dict is:
d.xxxx()
is converted tolist(d.xxxx())
.The converted code gives also a list.
But the converted code performs an EXTRA dictionary copy!
The converted code gives a list (as in Py 2.7 for both code variants).
d.iterxxxx()
is converted toiter(d.xxxx())
.The converted code results in an iterator -
but this iterator is created ON TOP of the list returned by d.xxxx().
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
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 byd.iterkeys(), d.itervalues(), d.iteritems()
and in Py3.3 byd.keys(), d.values(), d.items()
.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?
list(d.xxxx())
could be removed manually when a list is not required.key in d
together withd[key]
as appropriate. This code has the same behavior in Py2.7 and Py3.3. But manual code change is requiredThere is no undisputed best solution :-(
Suggestions are welcome!