I am working on a library that implements a data structure that works with any ordered data type - a range. Many operations (such as inversion) become interesting when you allow positive and negative infinity.
One goal is to make datetime objects work with this module and maintain infinity with non-numeric objects, I created INFINITY and NEGATIVE_INFINITY:
class _Indeterminate(object): def __eq__(self, other): return other is self @functools.total_ordering class _Infinity(_Indeterminate): def __lt__(self, other): return False def __gt__(self, other): return True def __str__(self): return 'inf' __repr__ = __str__ @functools.total_ordering class _NegativeInfinity(_Indeterminate): def __lt__(self, other): return True def __gt__(self, other): return False def __str__(self): return '-inf' INFINITY = _Infinity() NEGATIVE_INFINITY = _NegativeInfinity()
Unfortunately, this does not work for datetime objects when on the left side of the cmp () operation:
In [1]: from rangeset import * In [2]: from datetime import datetime In [3]: now = datetime.now() In [4]: cmp(INFINITY, now) Out[4]: 1 In [5]: cmp(now, INFINITY) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /home/axiak/Documents/rangeset/<ipython-input-5-c928d3687d92> in <module>() ----> 1 cmp(now, INFINITY) TypeError: can't compare datetime.datetime to _Infinity
I was hoping I could get around this limitation using the cmp wrapper, which simply ensures that my objects are always called, but I really want to use the .sort()
method, which will call the cmp call between these objects.
Is there a way to create an object that is really smaller than any other object and really larger than any other object?
Home module: https://github.com/axiak/py-rangeset
source share