Implement rich comparisons rather than relying on __cmp__()

The __cmp__() special method is gone in Python 3 in favor of rich
comparison methods.
This commit is contained in:
Alessio Sergi 2018-01-29 19:19:19 +01:00
parent 7a0b9bcbc9
commit 32425eb205
2 changed files with 11 additions and 8 deletions

View File

@ -22,6 +22,7 @@ Thresholds classes: OK, CAREFUL, WARNING, CRITICAL
"""
import sys
from functools import total_ordering
class GlancesThresholds(object):
@ -63,6 +64,7 @@ class GlancesThresholds(object):
glances_thresholds = GlancesThresholds()
@total_ordering
class _GlancesThreshold(object):
"""Father class for all other Thresholds"""
@ -79,9 +81,11 @@ class _GlancesThreshold(object):
def __str__(self):
return self.description()
def __cmp__(self, other):
"""Override the default comparaison behavior"""
return self.value().__cmp__(other.value())
def __lt__(self, other):
return self.value() < other.value()
def __eq__(self, other):
return self.value() == other.value()
class GlancesThresholdOk(_GlancesThreshold):

View File

@ -28,7 +28,7 @@ from glances.stats import GlancesStats
from glances import __version__
from glances.globals import WINDOWS, LINUX
from glances.outputs.glances_bars import Bar
from glances.compat import PY3, PY_PYPY
from glances.compat import PY_PYPY
from glances.thresholds import GlancesThresholdOk
from glances.thresholds import GlancesThresholdCareful
from glances.thresholds import GlancesThresholdWarning
@ -213,7 +213,6 @@ class TestGlances(unittest.TestCase):
self.assertTrue(type(stats_grab) is list, msg='GPU stats is not a list')
print('INFO: GPU stats: %s' % stats_grab)
@unittest.skipIf(PY3, True)
@unittest.skipIf(PY_PYPY, True)
def test_094_thresholds(self):
"""Test thresholds classes"""
@ -226,11 +225,11 @@ class TestGlances(unittest.TestCase):
self.assertTrue(careful < warning)
self.assertTrue(warning < critical)
self.assertFalse(ok > careful)
self.assertTrue(ok == ok)
self.assertTrue(str(ok) == 'OK')
self.assertEqual(ok, ok)
self.assertEqual(str(ok), 'OK')
thresholds = GlancesThresholds()
thresholds.add('cpu_percent', 'OK')
self.assertTrue(thresholds.get(stat_name='cpu_percent').description() == 'OK')
self.assertEqual(thresholds.get(stat_name='cpu_percent').description(), 'OK')
def test_095_methods(self):
"""Test mandatories methods"""