From 32425eb205eb7ad83b838a020423b0fda80fb8e2 Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Mon, 29 Jan 2018 19:19:19 +0100 Subject: [PATCH 1/3] Implement rich comparisons rather than relying on __cmp__() The __cmp__() special method is gone in Python 3 in favor of rich comparison methods. --- glances/thresholds.py | 10 +++++++--- unitest.py | 9 ++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/glances/thresholds.py b/glances/thresholds.py index 845e19cb..f56ce2ae 100644 --- a/glances/thresholds.py +++ b/glances/thresholds.py @@ -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): diff --git a/unitest.py b/unitest.py index 015bc46b..3ea2170a 100755 --- a/unitest.py +++ b/unitest.py @@ -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""" From 7e70b36f91644f7f1f0e460e1f4343b67ff3541e Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Mon, 29 Jan 2018 19:59:17 +0100 Subject: [PATCH 2/3] Take advantage of rich comparisons for PyPy too --- glances/compat.py | 5 ----- unitest.py | 2 -- 2 files changed, 7 deletions(-) diff --git a/glances/compat.py b/glances/compat.py index c30e019e..be0706c6 100644 --- a/glances/compat.py +++ b/glances/compat.py @@ -25,15 +25,10 @@ import operator import sys import unicodedata import types -import platform import subprocess from glances.logger import logger -PY_CYTHON = platform.python_implementation() == 'CPython' -PY_PYPY = platform.python_implementation() == 'PyPy' -PY_JYTHON = platform.python_implementation() == 'Jython' -PY_IRON = platform.python_implementation() == 'IronPython' PY3 = sys.version_info[0] == 3 try: diff --git a/unitest.py b/unitest.py index 3ea2170a..84a8e4c7 100755 --- a/unitest.py +++ b/unitest.py @@ -28,7 +28,6 @@ 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 PY_PYPY from glances.thresholds import GlancesThresholdOk from glances.thresholds import GlancesThresholdCareful from glances.thresholds import GlancesThresholdWarning @@ -213,7 +212,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(PY_PYPY, True) def test_094_thresholds(self): """Test thresholds classes""" print('INFO: [TEST_094] Thresholds') From d137d3bd268246ec214cf2abc721f137f6ae6948 Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Mon, 29 Jan 2018 20:42:29 +0100 Subject: [PATCH 3/3] Unconditionally use statistics module since we support Python 3.4+ now --- glances/compat.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/glances/compat.py b/glances/compat.py index be0706c6..a85d433a 100644 --- a/glances/compat.py +++ b/glances/compat.py @@ -31,16 +31,10 @@ from glances.logger import logger PY3 = sys.version_info[0] == 3 -try: - from statistics import mean -except ImportError: - # Statistics is only available for Python 3.4 or higher - def mean(numbers): - return float(sum(numbers)) / max(len(numbers), 1) - if PY3: import queue from configparser import ConfigParser, NoOptionError, NoSectionError + from statistics import mean from xmlrpc.client import Fault, ProtocolError, ServerProxy, Transport, Server from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer from urllib.request import urlopen @@ -130,6 +124,9 @@ else: viewvalues = operator.methodcaller('viewvalues') viewitems = operator.methodcaller('viewitems') + def mean(numbers): + return float(sum(numbers)) / max(len(numbers), 1) + def to_ascii(s): """Convert the unicode 's' to a ASCII string Usefull to remove accent (diacritics)"""