Opened 2 years ago

Closed 2 years ago

#25381 closed enhancement (fixed)

py3: adding .items iterator to vectors

Reported by: chapoton Owned by:
Priority: major Milestone: sage-8.3
Component: python3 Keywords:
Cc: embray, jdemeyer Merged in:
Authors: Frédéric Chapoton Reviewers: Erik Bray
Report Upstream: N/A Work issues:
Branch: 2fd6154 (Commits) Commit: 2fd6154ce97b94a5424f94a726ef09c8cd45fea0
Dependencies: Stopgaps:


to help build doc in python3

Change History (8)

comment:1 Changed 2 years ago by chapoton

  • Branch set to u/chapoton/25381
  • Commit set to cbd18885ff45659d9a57c9024dc01567a22bc0df
  • Status changed from new to needs_review

New commits:

cbd1888adding .items method to vectors for py3 sake

comment:2 Changed 2 years ago by embray

+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?

  • src/sage/modules/free_module_element.pyx

    diff --git a/src/sage/modules/free_module_element.pyx b/src/sage/modules/free_module_element.pyx
    index 29b2de1..814afa7 100644
    a b def random_vector(ring, degree=None, *args, **kwds): 
    821821    entries = [ring.random_element(*args, **kwds) for _ in range(degree)]
    822822    return vector(ring, degree, entries, sparse)
    824825cdef class FreeModuleElement(Vector):   # abstract base class
    825826    """
    826827    An element of a generic free module.
    cdef class FreeModuleElement(Vector): # abstract base class 
    15451546        from sage.arith.all import lcm
    15461547        return lcm(v)
     1549    def items(self):
     1550        """
     1551        Return an iterable over self.
     1553        On Python 2 this returns the same as ``list(self.iteritems())``, while
     1554        on Python 3 this is an alias for ``self.items()``.  This is for
     1555        congruence with ``dict.items()``.
     1557        EXAMPLES::
     1559            sage: v = vector([1, 2/3, pi])
     1560            sage: v.items()  # py2
     1561            [(0, 1), (1, 2/3), (2, pi)]
     1562            sage: v.items()  # py3
     1563            <generator object at ...>
     1564        """
     1566        IF PY_MAJOR_VERSION < 3:
     1567            return list(self.iteritems())
     1568        ELSE:
     1569            return self.iteritems()
    15481571    def iteritems(self):
    15491572        """
    15501573        Return iterator over self.
    cdef class FreeModuleElement_generic_sparse(FreeModuleElement): 
    45434566                e = entries
    45444567                entries = {}
    45454568                try:
    4546                     for k, x in e.iteritems():
     4569                    for k, x in e.items():
    45474570                        x = coefficient_ring(x)
    45484571                        if x:
    45494572                            entries[k] = x
    cdef class FreeModuleElement_generic_sparse(FreeModuleElement): 
    47354758            sage: v = vector([1,2/3,pi], sparse=True)
    47364759            sage: v.iteritems()
    4737             <dictionary-itemiterator object at ...>
     4760            <dict...itemiterator object at ...>
    47384761            sage: list(v.iteritems())
    47394762            [(0, 1), (1, 2/3), (2, pi)]
    47404763        """
    4741         return self._entries.iteritems()
     4764        return iter(self._entries.iteritems())
    47434766    def __reduce__(self):
    47444767        """

comment:3 Changed 2 years ago by chapoton

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 Changed 2 years ago by embray

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 FreeModuleElement is not actually a dict subclass I think we have some freedom to pick the latter.

In that case though what you might do instead is actually rename the iteritems() method to just items() and make iteritems() and deprecated (perhaps just informally?) alias for items().

comment:5 Changed 2 years ago by git

  • Commit changed from cbd18885ff45659d9a57c9024dc01567a22bc0df to 2fd6154ce97b94a5424f94a726ef09c8cd45fea0

Branch pushed to git repo; I updated commit sha1. New commits:

2fd6154trac 25381 iteritems as an alias of items for vectors

comment:6 Changed 2 years ago by chapoton


I do not think that one should deprecate formally anything.

comment:7 Changed 2 years ago by embray

  • Reviewers set to Erik Bray
  • Status changed from needs_review to positive_review

I'm inclined to agree in this case, though I can't quite articulate why.

comment:8 Changed 2 years ago by vbraun

  • Branch changed from u/chapoton/25381 to 2fd6154ce97b94a5424f94a726ef09c8cd45fea0
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.