comparison roundup/hyperdb.py @ 5466:09966de4d3f6

Python 3 built-in types can't be compared against None
author Christof Meerwald <cmeerw@cmeerw.org>
date Sat, 28 Jul 2018 20:34:31 +0100
parents 87f22a5d65ca
children 9a09719b0d8e
comparison
equal deleted inserted replaced
5465:c4f9a152258d 5466:09966de4d3f6
30 from .support import ensureParentsExist, PrioList 30 from .support import ensureParentsExist, PrioList
31 from roundup.i18n import _ 31 from roundup.i18n import _
32 from roundup.cgi.exceptions import DetectorError 32 from roundup.cgi.exceptions import DetectorError
33 33
34 logger = logging.getLogger('roundup.hyperdb') 34 logger = logging.getLogger('roundup.hyperdb')
35
36 try:
37 None < 0
38 def _NoneComparable(v):
39 return v
40 except TypeError:
41 class _NoneComparable(object):
42 def __init__(self, value):
43 self.value = value
44
45 def __cmp__(self, other):
46 if not isinstance(other, self.__class__):
47 raise TypeError('not comparable')
48
49 if self.value is None and other.value is None:
50 return 0
51 elif self.value is None:
52 return -1
53 elif other.value is None:
54 return 1
55 elif type(self.value) == type(()) and type(other.value) == type(()):
56 for lhs, rhs in zip(self.value, other.value):
57 result = _NoneComparable(lhs).__cmp__(_NoneComparable(rhs))
58 if result != 0:
59 return result
60 return len(self.value) - len(other.value)
61 elif self.value < other.value:
62 return -1
63 elif self.value > other.value:
64 return 1
65 else:
66 return 0
67
68 def __eq__(self, other):
69 return self.__cmp__(other) == 0
70 def __ne__(self, other):
71 return self.__cmp__(other) != 0
72 def __lt__(self, other):
73 return self.__cmp__(other) < 0
74 def __le__(self, other):
75 return self.__cmp__(other) <= 0
76 def __ge__(self, other):
77 return self.__cmp__(other) >= 0
78 def __gt__(self, other):
79 return self.__cmp__(other) > 0
35 80
36 # 81 #
37 # Types 82 # Types
38 # 83 #
39 class _Type(object): 84 class _Type(object):
604 idx += 1 649 idx += 1
605 sortattr.append (val) 650 sortattr.append (val)
606 sortattr = zip (*sortattr) 651 sortattr = zip (*sortattr)
607 for dir, i in reversed(list(zip(directions, dir_idx))): 652 for dir, i in reversed(list(zip(directions, dir_idx))):
608 rev = dir == '-' 653 rev = dir == '-'
609 sortattr = sorted (sortattr, key = lambda x:x[i:idx], reverse = rev) 654 sortattr = sorted (sortattr,
655 key = lambda x: _NoneComparable(x[i:idx]),
656 reverse = rev)
610 idx = i 657 idx = i
611 return [x[-1] for x in sortattr] 658 return [x[-1] for x in sortattr]
612 659
613 def _sort_repr(self, sortrep, ids): 660 def _sort_repr(self, sortrep, ids):
614 """Call sortrep for given ids -- possibly a list of list. We 661 """Call sortrep for given ids -- possibly a list of list. We

Roundup Issue Tracker: http://roundup-tracker.org/