|
33 | 33 |
|
34 | 34 | logger = logging.getLogger('roundup.hyperdb') |
35 | 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 |
| 80 | + |
36 | 81 | # |
37 | 82 | # Types |
38 | 83 | # |
@@ -606,7 +651,9 @@ def _sort(self, val): |
606 | 651 | sortattr = zip (*sortattr) |
607 | 652 | for dir, i in reversed(list(zip(directions, dir_idx))): |
608 | 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 | 657 | idx = i |
611 | 658 | return [x[-1] for x in sortattr] |
612 | 659 |
|
|
0 commit comments