New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py3: adding .items iterator to vectors #25381
Comments
New commits:
|
Commit: |
Branch: u/chapoton/25381 |
comment:2
+1 in principle. I have a different approach to this in my python3 branch that I haven't made a ticket for yet. What do you think? diff --git a/src/sage/modules/free_module_element.pyx b/src/sage/modules/free_module_element.pyx
index 29b2de1..814afa7 100644
--- a/src/sage/modules/free_module_element.pyx
+++ b/src/sage/modules/free_module_element.pyx
@@ -821,6 +821,7 @@ def random_vector(ring, degree=None, *args, **kwds):
entries = [ring.random_element(*args, **kwds) for _ in range(degree)]
return vector(ring, degree, entries, sparse)
+
cdef class FreeModuleElement(Vector): # abstract base class
"""
An element of a generic free module.
@@ -1545,6 +1546,28 @@ cdef class FreeModuleElement(Vector): # abstract base class
from sage.arith.all import lcm
return lcm(v)
+ def items(self):
+ """
+ Return an iterable over self.
+
+ On Python 2 this returns the same as ``list(self.iteritems())``, while
+ on Python 3 this is an alias for ``self.items()``. This is for
+ congruence with ``dict.items()``.
+
+ EXAMPLES::
+
+ sage: v = vector([1, 2/3, pi])
+ sage: v.items() # py2
+ [(0, 1), (1, 2/3), (2, pi)]
+ sage: v.items() # py3
+ <generator object at ...>
+ """
+
+ IF PY_MAJOR_VERSION < 3:
+ return list(self.iteritems())
+ ELSE:
+ return self.iteritems()
+
def iteritems(self):
"""
Return iterator over self.
@@ -4543,7 +4566,7 @@ cdef class FreeModuleElement_generic_sparse(FreeModuleElement):
e = entries
entries = {}
try:
- for k, x in e.iteritems():
+ for k, x in e.items():
x = coefficient_ring(x)
if x:
entries[k] = x
@@ -4734,11 +4757,11 @@ cdef class FreeModuleElement_generic_sparse(FreeModuleElement):
sage: v = vector([1,2/3,pi], sparse=True)
sage: v.iteritems()
- <dictionary-itemiterator object at ...>
+ <dict...itemiterator object at ...>
sage: list(v.iteritems())
[(0, 1), (1, 2/3), (2, pi)]
"""
- return self._entries.iteritems()
+ return iter(self._entries.iteritems())
def __reduce__(self):
""" |
comment:3
oh, well. Given that "items" currently does not exist, one can just provide it directly with the python3 behaviour. Otherwise, this opens the possibility that someone will use that "items" in a python3-incompatible way, and that we will have to fix this bad use later.. |
comment:4
That is true--the question is to be compatible with Python 2 semantics on Python 2, or just be immediately forward-compatible with Python 3. Since In that case though what you might do instead is actually rename the |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:6
Done. I do not think that one should deprecate formally anything. |
comment:7
I'm inclined to agree in this case, though I can't quite articulate why. |
Reviewer: Erik Bray |
Changed branch from u/chapoton/25381 to |
to help build doc in python3
CC: @embray @jdemeyer
Component: python3
Author: Frédéric Chapoton
Branch/Commit:
2fd6154
Reviewer: Erik Bray
Issue created by migration from https://trac.sagemath.org/ticket/25381
The text was updated successfully, but these errors were encountered: