diff --git a/glances/compat.py b/glances/compat.py index c30e019e..a85d433a 100644 --- a/glances/compat.py +++ b/glances/compat.py @@ -25,27 +25,16 @@ 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: - 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 @@ -135,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)""" 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..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 PY3, PY_PYPY from glances.thresholds import GlancesThresholdOk from glances.thresholds import GlancesThresholdCareful from glances.thresholds import GlancesThresholdWarning @@ -213,8 +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(PY3, True) - @unittest.skipIf(PY_PYPY, True) def test_094_thresholds(self): """Test thresholds classes""" print('INFO: [TEST_094] Thresholds') @@ -226,11 +223,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"""