summaryrefslogtreecommitdiff
path: root/lib/_range.py
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2014-02-22 23:02:19 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2014-02-22 23:02:19 +0000
commit283dbccf56d1c3fb14eadc66bdd4e2a4bb56974e (patch)
tree2e691b9fe6ba34336a5e8002037ad2c73c2231f7 /lib/_range.py
parentdd9e476353b0fd0f10da40204327a951941bc2e4 (diff)
parent8d2744c550fef74c518ef14bd304e0bc880c84c7 (diff)
downloadpsycopg2-283dbccf56d1c3fb14eadc66bdd4e2a4bb56974e.tar.gz
Merge branch 'range_sort'
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):