def op_first(x, y): try: method = getattr(y, rmethod) except AttributeError: raise_unordered(op, args=(x, y)) else: return method(x)
def op(x, y): out = getattr(x, method, lambda y: NotImplemented)(y) if out is NotImplemented: try: return getattr(y, rmethod)(x) except AttributeError: raise_unordered(op, args=(x, y)) raise_no_methods(op, args=(x, y))
def gt(x, y): # Python 2 may implement relations using the __cmp__ method. This is true # for builtins, for instance. We have to check all failure points to see # if a __cmp__ function is implemented. try: out = x.__gt__(y) except AttributeError: if hasattr(x, "__cmp__"): return x.__cmp__(y) == 1 raise RuntimeError("TotalOrdered object does not implement a __gt__ relation") if out is NotImplemented: try: out = y.__lt__(x) except AttributeError: if hasattr(x, "__cmp__"): return y.__cmp__(x) == -1 out = NotImplemented if out is NotImplemented: raise_unordered(x, y) return out
def gt(x, y): # Python 2 may implement relations using the __cmp__ method. This is true # for builtins, for instance. We have to check all failure points to see # if a __cmp__ function is implemented. try: out = x.__gt__(y) except AttributeError: if hasattr(x, '__cmp__'): return x.__cmp__(y) == 1 raise RuntimeError( 'TotalOrdered object does not implement a __gt__ relation') if out is NotImplemented: try: out = y.__lt__(x) except AttributeError: if hasattr(x, '__cmp__'): return y.__cmp__(x) == -1 out = NotImplemented if out is NotImplemented: raise_unordered(x, y) return out
def op_second(x, y): out = getattr(x, method, lambda y: NotImplemented)(y) if out is NotImplemented: raise_unordered(x, y) return out