Mercurial > p > roundup > code
comparison roundup/hyperdb.py @ 5481:9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
| author | Christof Meerwald <cmeerw@cmeerw.org> |
|---|---|
| date | Wed, 01 Aug 2018 22:03:50 +0100 |
| parents | 09966de4d3f6 |
| children | bb7865241f8a |
comparison
equal
deleted
inserted
replaced
| 5480:e8f6a1df73e3 | 5481:9a09719b0d8e |
|---|---|
| 28 # roundup modules | 28 # roundup modules |
| 29 from . import date, password | 29 from . import date, password |
| 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 from roundup.anypy.cmp_ import NoneAndDictComparable | |
| 33 | 34 |
| 34 logger = logging.getLogger('roundup.hyperdb') | 35 logger = logging.getLogger('roundup.hyperdb') |
| 35 | 36 |
| 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 | 37 |
| 81 # | 38 # |
| 82 # Types | 39 # Types |
| 83 # | 40 # |
| 84 class _Type(object): | 41 class _Type(object): |
| 650 sortattr.append (val) | 607 sortattr.append (val) |
| 651 sortattr = zip (*sortattr) | 608 sortattr = zip (*sortattr) |
| 652 for dir, i in reversed(list(zip(directions, dir_idx))): | 609 for dir, i in reversed(list(zip(directions, dir_idx))): |
| 653 rev = dir == '-' | 610 rev = dir == '-' |
| 654 sortattr = sorted (sortattr, | 611 sortattr = sorted (sortattr, |
| 655 key = lambda x: _NoneComparable(x[i:idx]), | 612 key = lambda x: NoneAndDictComparable(x[i:idx]), |
| 656 reverse = rev) | 613 reverse = rev) |
| 657 idx = i | 614 idx = i |
| 658 return [x[-1] for x in sortattr] | 615 return [x[-1] for x in sortattr] |
| 659 | 616 |
| 660 def _sort_repr(self, sortrep, ids): | 617 def _sort_repr(self, sortrep, ids): |
