Last active
March 19, 2019 23:00
-
-
Save mathrick/b909e353c40484d8c9c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comparable(object): | |
def __init__(self, name): | |
self.name = name | |
self.is_neg = False | |
def __neg__(self): | |
self.is_neg = not self.is_neg | |
return self | |
def op(self, op, value): | |
try: | |
float(value) | |
return "{}{}.{}:{}".format( | |
"not " if self.is_neg else "", | |
self.name, | |
op, | |
value | |
) | |
except: | |
raise ValueError(value) | |
def __eq__(self, value): | |
return self.op("eq", value) | |
def __gt__(self, value): | |
return self.op("gt", value) | |
def __lt__(self, value): | |
return self.op("lt", value) | |
def __ge__(self, value): | |
return self.op("gte", value) | |
def __le__(self, value): | |
return self.op("lte", value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment