From 510e6dc9c775eaec9088e17bd0f7c4943784af39 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 9 Jul 2021 23:19:30 +0200 Subject: [PATCH] sort: removed cmp argument --- app/scodoc/notes_table.py | 51 +++++++---------------- app/scodoc/sco_entreprises.py | 10 ++--- app/scodoc/sco_etud.py | 5 ++- app/scodoc/sco_formsemestre.py | 9 ++-- app/scodoc/sco_formulas.py | 10 +++++ app/scodoc/sco_inscr_passage.py | 6 +-- app/scodoc/sco_liste_notes.py | 3 +- app/scodoc/sco_lycee.py | 8 +--- app/scodoc/sco_moduleimpl.py | 17 ++++---- app/scodoc/sco_moduleimpl_inscriptions.py | 6 +-- app/scodoc/sco_news.py | 5 ++- app/scodoc/sco_prepajury.py | 10 ++--- app/scodoc/sco_pvjury.py | 3 +- app/scodoc/sco_report.py | 7 ++-- app/scodoc/sco_synchro_etuds.py | 3 +- app/views/notes.py | 3 +- 16 files changed, 71 insertions(+), 85 deletions(-) diff --git a/app/scodoc/notes_table.py b/app/scodoc/notes_table.py index 7efa9fdb4..3ab5aa606 100644 --- a/app/scodoc/notes_table.py +++ b/app/scodoc/notes_table.py @@ -31,6 +31,7 @@ from types import StringType, FloatType import time import pdb import inspect +from operator import itemgetter import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb @@ -207,7 +208,7 @@ class NotesTable: x["nomp"] = (i["nom_usuel"] or i["nom"]) + i["prenom"] # pour tri # Tri les etudids par NOM - self.inscrlist.sort(lambda x, y: cmp(x["nomp"], y["nomp"])) + self.inscrlist.sort(key=itemgetter("nomp")) # { etudid : rang dans l'ordre alphabetique } rangalpha = {} @@ -306,33 +307,18 @@ class NotesTable: T.append(tuple(t)) # tri par moyennes décroissantes, # en laissant les demissionnaires a la fin, par ordre alphabetique - def cmprows(x, y): + def row_key(x): + """clé de tri par moyennes décroissantes, + en laissant les demissionnaires a la fin, par ordre alphabetique. + (moy_gen, rang_alpha) + """ try: - return cmp(float(y[0]), float(x[0])) # moy. gen. - except: - vx, vy = x[0], y[0] - try: - vx = float(vx) - except: - pass - try: - vy = float(vy) - except: - pass + moy = -float(x[0]) + except (ValueError, TypeError): + moy = 1000.0 + return (moy, rangalpha[x[-1]]) - if type(vx) == type(vy): # and type(vx) == StringType: - # rang alphabetique par nom - return rangalpha[x[-1]] - rangalpha[y[-1]] - else: - # Laisse les chaines a la fin de la liste - return cmp(str(type(vx)), str(type(vy))) # A revoir ! - # fallback *** should not occur *** - # txt = '\nkey missing in cmprows !!!\nx=%s\ny=%s\n' % (str(x),str(y)) - # txt += '\nrangalpha=%s' % str(rangalpha) + '\n\nT=%s' % str(T) - # send_debug_alert(txt, REQUEST=None) - # return cmp(x,y) - - T.sort(cmprows) + T.sort(key=row_key) self.T = T if len(valid_moy): @@ -363,7 +349,7 @@ class NotesTable: ue_eff = len( [x for x in val_ids if type(x[0]) == FloatType] ) # nombre d'étudiants avec une note dans l'UE - val_ids.sort(cmprows) + val_ids.sort(key=row_key) ue_rangs[ue_id] = ( comp_ranks(val_ids), ue_eff, @@ -374,7 +360,7 @@ class NotesTable: for modimpl in self._modimpls: vals = self._modmoys[modimpl["moduleimpl_id"]] val_ids = [(vals[etudid], etudid) for etudid in vals.keys()] - val_ids.sort(cmprows) + val_ids.sort(key=row_key) self.mod_rangs[modimpl["moduleimpl_id"]] = (comp_ranks(val_ids), len(vals)) # self.compute_moy_moy() @@ -483,14 +469,7 @@ class NotesTable: r = [m for m in self._modimpls if m["ue"]["ue_id"] == ue_id] # trie la liste par ue.numero puis mat.numero puis mod.numero r.sort( - lambda x, y: cmp( - x["ue"]["numero"] * 1000000 - + x["mat"]["numero"] * 1000 - + x["module"]["numero"], - y["ue"]["numero"] * 1000000 - + y["mat"]["numero"] * 1000 - + y["module"]["numero"], - ) + key=lambda x: (x["ue"]["numero"], x["mat"]["numero"], x["module"]["numero"]) ) return r diff --git a/app/scodoc/sco_entreprises.py b/app/scodoc/sco_entreprises.py index cf78f6608..00c19a4f8 100644 --- a/app/scodoc/sco_entreprises.py +++ b/app/scodoc/sco_entreprises.py @@ -29,6 +29,7 @@ """ # codes anciens déplacés de ZEntreprise import datetime +from operator import itemgetter import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb @@ -94,7 +95,7 @@ class EntreprisesEditor(EditableTable): else: r["date"] = datetime.date.min # sort - R.sort(lambda r1, r2: cmp(r2["date"], r1["date"])) + R.sort(key=itemgetter("date")) for r in R: r["date"] = DateISOtoDMY(r["date"]) return R @@ -122,11 +123,8 @@ class EntreprisesEditor(EditableTable): R.append(d) # sort if sort_on_contact: - R.sort( - lambda r1, r2: cmp( - r2["date"] or datetime.date.min, r1["date"] or datetime.date.min - ) - ) + R.sort(key=lambda x: (x["date"] or datetime.date.min)) + for r in R: r["date"] = DateISOtoDMY(r["date"] or datetime.date.min) return R diff --git a/app/scodoc/sco_etud.py b/app/scodoc/sco_etud.py index 7bcea4bd6..d42bb6124 100644 --- a/app/scodoc/sco_etud.py +++ b/app/scodoc/sco_etud.py @@ -36,6 +36,7 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header from email.mime.base import MIMEBase +from operator import itemgetter from app.scodoc import sco_emails import app.scodoc.sco_utils as scu @@ -605,7 +606,7 @@ class EtudIdentEditor: } res[-1].update(void_adm) # tri par nom - res.sort(lambda x, y: cmp(x["nom"] + x["prenom"], y["nom"] + y["prenom"])) + res.sort(key=itemgetter("nom", "prenom")) return res def edit(self, cnx, args, context=None, REQUEST=None): @@ -937,7 +938,7 @@ def fill_etuds_info(etuds): sems.append(sem) # trie les semestres par date de debut, le plus recent d'abord # (important, ne pas changer (suivi cohortes)) - sems.sort(lambda x, y: cmp(y["dateord"], x["dateord"])) + sems.sort(key=itemgetter("dateord"), reverse=True) etud["sems"] = sems etud["cursem"] = cursem if cursem: diff --git a/app/scodoc/sco_formsemestre.py b/app/scodoc/sco_formsemestre.py index 50655617e..e11287655 100644 --- a/app/scodoc/sco_formsemestre.py +++ b/app/scodoc/sco_formsemestre.py @@ -28,6 +28,7 @@ """Operations de base sur les formsemestres """ import time +from operator import itemgetter from scodoc_manager import sco_mgr from app.scodoc import sco_codes_parcours @@ -118,12 +119,8 @@ def do_formsemestre_list(context, *a, **kw): for sem in sems: formsemestre_enrich(context, sem) - # tri par date - sems.sort( - lambda x, y: cmp( - (y["dateord"], y["semestre_id"]), (x["dateord"], x["semestre_id"]) - ) - ) + # tri par date, le plus récent d'abord + sems.sort(key=itemgetter("dateord", "semestre_id"), reverse=True) return sems diff --git a/app/scodoc/sco_formulas.py b/app/scodoc/sco_formulas.py index 2a4e49ecc..e559074c6 100644 --- a/app/scodoc/sco_formulas.py +++ b/app/scodoc/sco_formulas.py @@ -33,6 +33,16 @@ from types import FloatType, IntType, LongType, StringType from functools import reduce +def cmp(x, y): + """ + Replacement for built-in function cmp that was removed in Python 3 + Compare the two objects x and y and return an integer according to + the outcome. The return value is negative if x < y, zero if x == y + and strictly positive if x > y. + """ + return (x > y) - (x < y) + + class NoteVector(object): """Vecteur de notes (ou coefficients) utilisé pour les formules définies par l'utilisateur. Les éléments sont accessibles soit par index v[i], soit par leur nom v['nom'] s'il en ont un. diff --git a/app/scodoc/sco_inscr_passage.py b/app/scodoc/sco_inscr_passage.py index 2aa52226e..5c8c8db59 100644 --- a/app/scodoc/sco_inscr_passage.py +++ b/app/scodoc/sco_inscr_passage.py @@ -29,6 +29,7 @@ Utilise les autorisations d'inscription délivrées en jury. """ import datetime +from operator import itemgetter import app.scodoc.notesdb as ndb import app.scodoc.sco_utils as scu @@ -301,7 +302,7 @@ def formsemestre_inscr_passage( def set_to_sorted_etud_list(etudset): etuds = [candidats[etudid] for etudid in etudset] - etuds.sort(lambda x, y: cmp(x["nom"], y["nom"])) + etuds.sort(key=itemgetter("nom")) return etuds if submitted: @@ -522,8 +523,7 @@ def etuds_select_boxes( infos["comment"] = infos.get("comment", "") # commentaire dans sous-titre boite help = infos.get("help", "") etuds = auth_etuds_by_cat[src_cat]["etuds"] - etuds.sort(lambda x, y: cmp(x["nom"], y["nom"])) - + etuds.sort(key=itemgetter("nom")) with_checkbox = (not read_only) and auth_etuds_by_cat[src_cat]["infos"].get( "with_checkbox", True ) diff --git a/app/scodoc/sco_liste_notes.py b/app/scodoc/sco_liste_notes.py index 5e083890f..85cf9c402 100644 --- a/app/scodoc/sco_liste_notes.py +++ b/app/scodoc/sco_liste_notes.py @@ -29,6 +29,7 @@ """ import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error from types import StringType +from operator import itemgetter import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb @@ -512,7 +513,7 @@ def _make_table_notes( '

', ] commentkeys = list(K.items()) # [ (comment, key), ... ] - commentkeys.sort(lambda x, y: cmp(int(x[1]), int(y[1]))) + commentkeys.sort(key=lambda x: int(x[1])) for (comment, key) in commentkeys: C.append( '(%s) %s
' % (key, comment) diff --git a/app/scodoc/sco_lycee.py b/app/scodoc/sco_lycee.py index 5275937d1..a955a5f11 100644 --- a/app/scodoc/sco_lycee.py +++ b/app/scodoc/sco_lycee.py @@ -29,6 +29,7 @@ - statistiques decisions - suivi cohortes """ +from operator import itemgetter import app.scodoc.sco_utils as scu from app.scodoc import html_sco_header @@ -112,12 +113,7 @@ def _table_etuds_lycees( for l in L: l["nbetuds"] = len(etuds_by_lycee[l["codelycee"]]) # L.sort( key=operator.itemgetter('codepostallycee', 'nomlycee') ) argh, only python 2.5+ !!! - L.sort( - cmp=lambda x, y: cmp( - (x["codepostallycee"], x["nomlycee"]), - (y["codepostallycee"], y["nomlycee"]), - ) - ) + L.sort(key=itemgetter("codepostallycee", "nomlycee")) columns_ids = ( "nbetuds", "codelycee", diff --git a/app/scodoc/sco_moduleimpl.py b/app/scodoc/sco_moduleimpl.py index 76ee15aa5..31eed42b0 100644 --- a/app/scodoc/sco_moduleimpl.py +++ b/app/scodoc/sco_moduleimpl.py @@ -153,15 +153,16 @@ def do_moduleimpl_withmodule_list( )[0] # tri par semestre/UE/numero_matiere/numero_module - extr = lambda x: ( - x["ue"]["numero"], - x["ue"]["ue_id"], - x["matiere"]["numero"], - x["matiere"]["matiere_id"], - x["module"]["numero"], - x["module"]["code"], + modimpls.sort( + key=lambda x: ( + x["ue"]["numero"], + x["ue"]["ue_id"], + x["matiere"]["numero"], + x["matiere"]["matiere_id"], + x["module"]["numero"], + x["module"]["code"], + ) ) - modimpls.sort(lambda x, y: cmp(extr(x), extr(y))) return scu.return_text_if_published(modimpls, REQUEST) diff --git a/app/scodoc/sco_moduleimpl_inscriptions.py b/app/scodoc/sco_moduleimpl_inscriptions.py index 2761da24a..afa944f6f 100644 --- a/app/scodoc/sco_moduleimpl_inscriptions.py +++ b/app/scodoc/sco_moduleimpl_inscriptions.py @@ -27,7 +27,7 @@ """Opérations d'inscriptions aux modules (interface pour gérer options ou parcours) """ - +from operator import itemgetter import app.scodoc.notesdb as ndb import app.scodoc.sco_utils as scu @@ -105,7 +105,7 @@ def moduleimpl_inscriptions_edit( "Etudiant %s inscrit mais inconnu dans la base !!!!!" % ins["etudid"] ) ins["etud"] = etuds_info[0] - inscrits.sort(lambda x, y: cmp(x["etud"]["nom"], y["etud"]["nom"])) + inscrits.sort(key=lambda x: x["etud"]["nom"]) in_m = sco_moduleimpl.do_moduleimpl_inscription_list( context, moduleimpl_id=M["moduleimpl_id"] ) @@ -458,7 +458,7 @@ def _fmt_etud_set(context, ins, max_list_size=7): etuds = [] for etudid in ins: etuds.append(sco_etud.get_etud_info(etudid=etudid, filled=True)[0]) - etuds.sort(lambda x, y: cmp(x["nom"], y["nom"])) + etuds.sort(itemgetter("nom")) return ", ".join( [ '%(nomprenom)s' diff --git a/app/scodoc/sco_news.py b/app/scodoc/sco_news.py index dcbecf770..88c98a95d 100644 --- a/app/scodoc/sco_news.py +++ b/app/scodoc/sco_news.py @@ -25,7 +25,7 @@ # ############################################################################## -"""Gestions des "nouvelles" +"""Gestion des "nouvelles" """ import datetime import re @@ -34,6 +34,7 @@ from cStringIO import StringIO from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header +from operator import itemgetter from stripogram import html2text import PyRSS2Gen # pylint: disable=import-error @@ -129,7 +130,7 @@ def scolar_news_summary(context, n=5): news = list(selected_news.values()) # sort by date, descending - news.sort(lambda x, y: cmp(y["date"], x["date"])) + news.sort(itemgetter("date"), reverse=True) news = news[:n] # mimic EditableTable.list output formatting: for n in news: diff --git a/app/scodoc/sco_prepajury.py b/app/scodoc/sco_prepajury.py index 74de7ad46..c6928e05e 100644 --- a/app/scodoc/sco_prepajury.py +++ b/app/scodoc/sco_prepajury.py @@ -137,16 +137,14 @@ def feuille_preparation_jury(context, formsemestre_id, REQUEST): # Codes des UE "semestre précédent": ue_prev_codes = list(prev_moy_ue.keys()) ue_prev_codes.sort( - lambda x, y, prev_ue_acro=prev_ue_acro: cmp( # pylint: disable=undefined-variable - prev_ue_acro[x], prev_ue_acro[y] - ) + key=lambda x, prev_ue_acro=prev_ue_acro: prev_ue_acro[ # pylint: disable=undefined-variable + x + ] ) # Codes des UE "semestre courant": ue_codes = list(moy_ue.keys()) ue_codes.sort( - lambda x, y, ue_acro=ue_acro: cmp( # pylint: disable=undefined-variable - ue_acro[x], ue_acro[y] - ) + key=lambda x, ue_acro=ue_acro: ue_acro[x] # pylint: disable=undefined-variable ) sid = sem["semestre_id"] diff --git a/app/scodoc/sco_pvjury.py b/app/scodoc/sco_pvjury.py index 727a12ae4..218931ee4 100644 --- a/app/scodoc/sco_pvjury.py +++ b/app/scodoc/sco_pvjury.py @@ -47,6 +47,7 @@ Jury de semestre n """ import time +from operator import itemgetter from reportlab.platypus import Paragraph from reportlab.lib import styles @@ -97,7 +98,7 @@ def _descr_decisions_ues(context, nt, etudid, decisions_ue, decision_sem): uelist.append(nt.get_etud_ue_status(etudid, ue["ue_id"])["ue"]) except KeyError: pass - uelist.sort(lambda x, y: cmp(x["numero"], y["numero"])) + uelist.sort(itemgetter("numero")) return uelist diff --git a/app/scodoc/sco_report.py b/app/scodoc/sco_report.py index f78c6e373..02a97da8c 100644 --- a/app/scodoc/sco_report.py +++ b/app/scodoc/sco_report.py @@ -35,6 +35,7 @@ import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error import re import time import datetime +from operator import itemgetter import app.scodoc.sco_utils as scu from app.scodoc import notesdb as ndb @@ -453,7 +454,7 @@ def table_suivi_cohorte( for s in sems: d, m, y = [int(x) for x in s["date_debut"].split("/")] s["date_debut_dt"] = datetime.datetime(y, m, d) - sems.sort(lambda x, y: cmp(x["date_debut_dt"], y["date_debut_dt"])) + sems.sort(key=itemgetter("date_debut_dt")) # 2-- Pour chaque semestre, trouve l'ensemble des etudiants venant de sem logt("B: etuds sets") @@ -910,7 +911,7 @@ def _descr_etud_set(context, etudids): for etudid in etudids: etuds.append(sco_etud.get_etud_info(etudid=etudid, filled=True)[0]) # sort by name - etuds.sort(lambda x, y: cmp(x["nom"], y["nom"])) + etuds.sort(itemgetter("nom")) return ", ".join([e["nomprenom"] for e in etuds]) @@ -1077,7 +1078,7 @@ def tsp_grouped_list(context, codes_etuds): ) L.append(l) # tri par effectifs décroissants - L.sort(lambda x, y: cmp(y["nb"], x["nb"])) + L.sort(key=itemgetter("nb")) return L diff --git a/app/scodoc/sco_synchro_etuds.py b/app/scodoc/sco_synchro_etuds.py index cd85a0ff9..ef4c4ea8d 100644 --- a/app/scodoc/sco_synchro_etuds.py +++ b/app/scodoc/sco_synchro_etuds.py @@ -30,6 +30,7 @@ import time import pprint +from operator import itemgetter import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb @@ -423,7 +424,7 @@ def list_synch(context, sem, anneeapogee=None): return etud etuds = [key2etud(x, etud_apo) for x in etudset] - etuds.sort(lambda x, y: cmp(x["nom"], y["nom"])) + etuds.sort(itemgetter("nom")) return etuds # diff --git a/app/views/notes.py b/app/views/notes.py index a6fdc14e5..7f41c03dc 100644 --- a/app/views/notes.py +++ b/app/views/notes.py @@ -35,6 +35,7 @@ import time import datetime import jaxml import pprint +from operator import itemgetter from flask import url_for, g from flask import current_app @@ -1207,7 +1208,7 @@ def formsemestre_enseignants_list(context, REQUEST, formsemestre_id, format="htm sem_ens[ens]["_email_target"] = "mailto:%s" % sem_ens[ens]["email"] sem_ens_list = list(sem_ens.values()) - sem_ens_list.sort(lambda x, y: cmp(x["nomprenom"], y["nomprenom"])) + sem_ens_list.sort(itemgetter("nomprenom")) # --- Generate page with table title = "Enseignants de " + sem["titremois"]