Skip to content

Instantly share code, notes, and snippets.

@Garmelon
Created January 14, 2024 22:51
Show Gist options
  • Save Garmelon/2f0285c91774f4511905ffd669da7e12 to your computer and use it in GitHub Desktop.
Save Garmelon/2f0285c91774f4511905ffd669da7e12 to your computer and use it in GitHub Desktop.
class CursedInt:
def __init__(self, expr):
self.__expr = str(expr)
# "Not exactly like numbers"
# Most other functions depend on this function being available, since
# CursedInt should behave like int in most cases.
def __int__(self):
return eval(self.__expr)
def __repr__(self):
return f"CursedInt({self.__expr!r})"
# "Like numbers"
def __str__(self):
return str(int(self))
def __bytes__(self):
return bytes(int(self))
def __format__(self, format_spec):
return int(self).__format__(format_spec)
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __gt__(self, other):
return int(self) > int(other)
def __ge__(self, other):
return int(self) >= int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __hash__(self):
return hash(int(self))
def __bool__(self):
return bool(int(self))
def __complex__(self):
return complex(int(self))
def __float__(self):
return float(int(self))
def __index__(self):
return int(self).__index__()
# "Like numbers, just without the parentheses"
@classmethod
def __cthulu(cls, n):
if isinstance(n, cls):
return n.__expr
if isinstance(n, int):
return n
return n
@classmethod
def __fhtagn(cls, *args):
expr = " ".join(cls.__cthulu(arg) for arg in args)
return cls(expr)
def __add__(self, other):
return self.__fhtagn(self, "+", other)
def __sub__(self, other):
return self.__fhtagn(self, "-", other)
def __mul__(self, other):
return self.__fhtagn(self, "*", other)
def __truediv__(self, other):
return self.__fhtagn(self, "/", other)
def __floordiv__(self, other):
return self.__fhtagn(self, "//", other)
def __mod__(self, other):
return self.__fhtagn(self, "%", other)
def __pow__(self, other):
return self.__fhtagn(self, "**", other)
def __lshift__(self, other):
return self.__fhtagn(self, "<<", other)
def __rshift__(self, other):
return self.__fhtagn(self, ">>", other)
def __and__(self, other):
return self.__fhtagn(self, "&", other)
def __xor__(self, other):
return self.__fhtagn(self, "^", other)
def __or__(self, other):
return self.__fhtagn(self, "|", other)
def __radd__(self, other):
return self.__fhtagn(other, "+", self)
def __rsub__(self, other):
return self.__fhtagn(other, "-", self)
def __rmul__(self, other):
return self.__fhtagn(other, "*", self)
def __rtruediv__(self, other):
return self.__fhtagn(other, "/", self)
def __rfloordiv__(self, other):
return self.__fhtagn(other, "//", self)
def __rmod__(self, other):
return self.__fhtagn(other, "%", self)
def __rpow__(self, other):
return self.__fhtagn(other, "**", self)
def __rlshift__(self, other):
return self.__fhtagn(other, "<<", self)
def __rrshift__(self, other):
return self.__fhtagn(other, ">>", self)
def __rand__(self, other):
return self.__fhtagn(other, "&", self)
def __rxor__(self, other):
return self.__fhtagn(other, "^", self)
def __ror__(self, other):
return self.__fhtagn(other, "|", self)
def __neg__(self):
return self.__fhtagn("-", self)
def __pos__(self):
return self.__fhtagn("+", self)
def __invert__(self):
return self.__fhtagn("~", self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment