From b6022c7a80b90f9c3bae39a12efa8de7e6707b35 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 11 Jul 2017 00:33:16 +0200 Subject: [PATCH] Add total_ordering for python 2.6. --- test/_compatibility.py | 30 ++++++++++++++++++++++++++++ test/test_normalizer_issues_files.py | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/_compatibility.py diff --git a/test/_compatibility.py b/test/_compatibility.py new file mode 100644 index 0000000..fe81076 --- /dev/null +++ b/test/_compatibility.py @@ -0,0 +1,30 @@ +try: + from functools import total_ordering +except ImportError: + # Python 2.6 + def total_ordering(cls): + """Class decorator that fills in missing ordering methods""" + convert = { + '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), + ('__le__', lambda self, other: self < other or self == other), + ('__ge__', lambda self, other: not self < other)], + '__le__': [('__ge__', lambda self, other: not self <= other or self == other), + ('__lt__', lambda self, other: self <= other and not self == other), + ('__gt__', lambda self, other: not self <= other)], + '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), + ('__ge__', lambda self, other: self > other or self == other), + ('__le__', lambda self, other: not self > other)], + '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), + ('__gt__', lambda self, other: self >= other and not self == other), + ('__lt__', lambda self, other: not self >= other)] + } + roots = set(dir(cls)) & set(convert) + if not roots: + raise ValueError('must define at least one ordering operation: < > <= >=') + root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ + for opname, opfunc in convert[root]: + if opname not in roots: + opfunc.__name__ = opname + opfunc.__doc__ = getattr(int, opname).__doc__ + setattr(cls, opname, opfunc) + return cls diff --git a/test/test_normalizer_issues_files.py b/test/test_normalizer_issues_files.py index a06b86e..8519187 100644 --- a/test/test_normalizer_issues_files.py +++ b/test/test_normalizer_issues_files.py @@ -5,7 +5,7 @@ tests of pydocstyle. import difflib import re -from functools import total_ordering +from _compatibility import total_ordering import parso from parso.utils import source_to_unicode