From aea498fa86743a725923ec373ceb7338ba460746 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 9 Jul 2021 22:09:12 +0200 Subject: [PATCH] Rich Comparisons for py3 --- app/scodoc/sco_abs.py | 20 +++++++++++++++++++- app/scodoc/sco_formsemestre.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/scodoc/sco_abs.py b/app/scodoc/sco_abs.py index 8b9afa01..7c8b42e7 100644 --- a/app/scodoc/sco_abs.py +++ b/app/scodoc/sco_abs.py @@ -188,11 +188,29 @@ class ddmmyyyy: else: return self.prev(self.weekday) - def __cmp__(self, other): + def __cmp__(self, other): # #py3 TODO à supprimer """return a negative integer if self < other, zero if self == other, a positive integer if self > other""" return int(self.time - other.time) + def __eq__(self, other): + return self.time == other.time + + def __ne__(self, other): + return self.time != other.time + + def __lt__(self, other): + return self.time < other.time + + def __le__(self, other): + return self.time <= other.time + + def __gt__(self, other): + return self.time > other.time + + def __ge__(self, other): + return self.time >= other.time + def __hash__(self): "we are immutable !" return hash(self.time) ^ hash(str(self)) diff --git a/app/scodoc/sco_formsemestre.py b/app/scodoc/sco_formsemestre.py index 8fbe9c22..50655617 100644 --- a/app/scodoc/sco_formsemestre.py +++ b/app/scodoc/sco_formsemestre.py @@ -411,10 +411,14 @@ class ApoEtapeVDI: def __str__(self): return self.etape_vdi - def __cmp__(self, other): + def _cmp(self, other): """Test égalité de deux codes étapes. Si le VDI des deux est spécifié, on l'utilise. Sinon, seul le code étape est pris en compte. Donc V1RT == V1RT!111, V1RT!110 == V1RT, V1RT!77 != V1RT!78, ... + + Compare the two objects x (=self) 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. """ if other is None: return -1 @@ -422,9 +426,31 @@ class ApoEtapeVDI: other = ApoEtapeVDI(other) if self.vdi and other.vdi: - return cmp((self.etape, self.vdi), (other.etape, other.vdi)) + x = (self.etape, self.vdi) + y = (other.etape, other.vdi) else: - return cmp(self.etape, other.etape) + x = self.etape + y = other.etape + + return (x > y) - (x < y) + + def __eq__(self, other): + return self._cmp(other) == 0 + + def __ne__(self, other): + return self._cmp(other) != 0 + + def __lt__(self, other): + return self._cmp(other) < 0 + + def __le__(self, other): + return self._cmp(other) <= 0 + + def __gt__(self, other): + return self._cmp(other) > 0 + + def __ge__(self, other): + return self._cmp(other) >= 0 def split_etape_vdi(self, etape_vdi): """Etape Apogee can be stored as 'V1RT' or, including the VDI version,