Skip to content

Instantly share code, notes, and snippets.

@mathrick
Last active March 19, 2019 23:00
Show Gist options
  • Save mathrick/b909e353c40484d8c9c8 to your computer and use it in GitHub Desktop.
Save mathrick/b909e353c40484d8c9c8 to your computer and use it in GitHub Desktop.
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