summaryrefslogtreecommitdiff
path: root/lib/_range.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/_range.py')
-rw-r--r--lib/_range.py39
1 files changed, 35 insertions, 4 deletions
diff --git a/lib/_range.py b/lib/_range.py
index 4586639..47b8208 100644
--- a/lib/_range.py
+++ b/lib/_range.py
@@ -133,12 +133,43 @@ class Range(object):
def __hash__(self):
return hash((self._lower, self._upper, self._bounds))
+ # as the postgres docs describe for the server-side stuff,
+ # ordering is rather arbitrary, but will remain stable
+ # and consistent.
+
def __lt__(self, other):
- raise TypeError(
- 'Range objects cannot be ordered; please refer to the PostgreSQL'
- ' documentation to perform this operation in the database')
+ if not isinstance(other, Range):
+ return NotImplemented
+ for attr in ('_lower', '_upper', '_bounds'):
+ self_value = getattr(self, attr)
+ other_value = getattr(other, attr)
+ if self_value == other_value:
+ pass
+ elif self_value is None:
+ return True
+ elif other_value is None:
+ return False
+ else:
+ return self_value < other_value
+ return False
- __le__ = __gt__ = __ge__ = __lt__
+ def __le__(self, other):
+ if self == other:
+ return True
+ else:
+ return self.__lt__(other)
+
+ def __gt__(self, other):
+ if isinstance(other, Range):
+ return other.__lt__(self)
+ else:
+ return NotImplemented
+
+ def __ge__(self, other):
+ if self == other:
+ return True
+ else:
+ return self.__gt__(other)
def register_range(pgrange, pyrange, conn_or_curs, globally=False):