[PATCH 5 of 5] [py3] py2 compliant migration to py3 on sorted

julien tayon julien.tayon at logilab.fr
Fri Jun 7 15:40:07 CEST 2019


# HG changeset patch
# User julien tayon <julien.tayon at logilab.fr>
# Date 1558702395 -7200
#      Fri May 24 14:53:15 2019 +0200
# Node ID 28e9e128daea260ca076951857bbd5108726a774
# Parent  06faf72cf308fb7858a83d25b08d2aa7d64506f3
[py3] py2 compliant migration to py3 on sorted

Py3: py3 sorted does not accept cmp because sorted will now compare the result
"key" return based on the natural ordering of the returned keys.

Found not tests so I experimented by using the schema view and then using
"vu en boite"

It is revealing a bug (see next patch)

diff --git a/cubicweb/web/schemaviewer.py b/cubicweb/web/schemaviewer.py
--- a/cubicweb/web/schemaviewer.py
+++ b/cubicweb/web/schemaviewer.py
@@ -24,9 +24,7 @@ from logilab.common.ureports import Sect
 
 from yams.schema2dot import CARD_MAP
 from yams.schema import RelationDefinitionSchema
-from operator import attrgetter
-
-TYPE_GETTER = attrgetter('type')
+from operator import attrgetter, itemgetter
 
 I18NSTRINGS = [_('read'), _('add'), _('delete'), _('update'), _('order')]
 
@@ -80,16 +78,23 @@ class SchemaViewer(object):
         layout.append(esection)
         eschemas = [eschema for eschema in schema.entities()
                     if not (eschema.final or eschema in skiptypes)]
-        for eschema in sorted(eschemas, key=TYPE_GETTER):
+        for eschema in sorted(eschemas, key=attrgetter('type')):
             esection.append(self.visit_entityschema(eschema, skiptypes))
         if display_relations:
             title = Title(self._('Relations'), klass='titleUnderline')
             rsection = Section(children=(title,))
             layout.append(rsection)
-            relations = [rschema for rschema in sorted(schema.relations(), key=TYPE_GETTER)
-                         if not (rschema.final or rschema.type in skiptypes)]
+
+            def skipped_relation_schema(rschema):
+                return not (rschema.final or rschema.type in skiptypes)
+
+            relations = list(
+                filter(
+                    skipped_relation_schema,
+                    sorted(schema.relations(), key=attrgetter('type'))
+                ))
             keys = [(rschema.type, rschema) for rschema in relations]
-            for key, rschema in sorted(keys, cmp=(lambda x, y: cmp(x[1], y[1]))):
+            for key, rschema in sorted(keys, key=itemgetter(1)):
                 relstr = self.visit_relationschema(rschema)
                 rsection.append(relstr)
         return layout
@@ -97,7 +102,8 @@ class SchemaViewer(object):
     def _entity_attributes_data(self, eschema):
         _ = self._
         data = [_('attribute'), _('type'), _('default'), _('constraints')]
-        attributes = sorted(eschema.attribute_definitions(), cmp=(lambda x, y: cmp(x[0].type, y[0].type)))
+        attributes = sorted(eschema.attribute_definitions(),
+                            key=lambda el: el[0].type)
         for rschema, aschema in attributes:
             rdef = eschema.rdef(rschema)
             if not self.may_read(rdef):
@@ -139,13 +145,15 @@ class SchemaViewer(object):
         rels = []
         first = True
 
-        rel_defs = sorted(eschema.relation_definitions(),
-                          cmp=(lambda x, y: cmp((x[0].type, x[0].cardinality),
-                          (y[0].type, y[0].cardinality))))
+        def get_type_card(el):
+            return (el[0].type, el[0].cardinality)
+
+        rel_defs = sorted(eschema.relation_definitions(), key=get_type_card)
+
         for rschema, targetschemas, role in rel_defs:
             if rschema.type in skiptypes:
                 continue
-            for oeschema in sorted(targetschemas, key=TYPE_GETTER):
+            for oeschema in sorted(targetschemas, key=attrgetter('type')):
                 rdef = rschema.role_rdef(eschema, oeschema, role)
                 if not self.may_read(rdef):
                     continue


More information about the cubicweb-devel mailing list