[Python-Dev] Re: [bug?] UserLong.py - pow(5, UserLong(3), 26) fails
Luke Kenneth Casson Leighton
lkcl@samba-tng.org
Wed, 1 May 2002 19:16:11 +0000
"""A more or less complete user-defined wrapper around long objects."""
class UserLong:
def __init__(self, initlong=None):
self.data = 0L
if initlong is not None:
# XXX should this accept an arbitrary sequence?
if type(initlong) == type(self.data):
self.data = initlong
elif isinstance(initlong, UserLong):
self.data = initlong.data
else:
self.data = long(initlong)
def __repr__(self): return repr(self.data)
def __lt__(self, other): return self.data < self.__cast(other)
def __le__(self, other): return self.data <= self.__cast(other)
def __eq__(self, other): return self.data == self.__cast(other)
def __ne__(self, other): return self.data != self.__cast(other)
def __gt__(self, other): return self.data > self.__cast(other)
def __ge__(self, other): return self.data >= self.__cast(other)
def __cast(self, other):
if isinstance(other, UserLong): return other.data
else: return other
def __long__(self):
return self.data
def __int__(self):
return int(self.data)
def __cmp__(self, other):
return cmp(self.data, self.__cast(other))
def __sub__(self, other):
if isinstance(other, UserLong):
return self.__class__(self.data - other.data)
elif isinstance(other, type(self.data)):
return self.__class__(self.data - other)
else:
return self.__class__(self.data - long(other))
def __add__(self, other):
if isinstance(other, UserLong):
return self.__class__(self.data + other.data)
elif isinstance(other, type(self.data)):
return self.__class__(self.data + other)
else:
return self.__class__(self.data + long(other))
def __rsub__(self, other):
if isinstance(other, UserLong):
return self.__class__(other.data - self.data)
elif isinstance(other, type(self.data)):
return self.__class__(other - self.data)
else:
return self.__class__(long(other) - self.data)
def __radd__(self, other):
if isinstance(other, UserLong):
return self.__class__(other.data + self.data)
elif isinstance(other, type(self.data)):
return self.__class__(other + self.data)
else:
return self.__class__(long(other) + self.data)
def __iadd__(self, other):
if isinstance(other, UserLong):
self.data += other.data
elif isinstance(other, type(self.data)):
self.data += other
else:
self.data += long(other)
return self
def __isub__(self, other):
if isinstance(other, UserLong):
self.data -= other.data
elif isinstance(other, type(self.data)):
self.data -= other
else:
self.data -= long(other)
return self
def __pow__(self, *args):
print args
if len(args) == 1:
(other,) = args
if isinstance(other, UserLong):
return self.__class__(pow(self.data, other.data))
elif isinstance(other, type(self.data)):
return self.__class__(pow(self.data, other))
else:
return self.__class__(pow(self.data, long(other)))
elif len(args) == 2:
(other, other2) = args
if isinstance(other, UserLong):
x = other.data
elif isinstance(other, type(self.data)):
x = other
else:
x2 = long(other)
if isinstance(other2, UserLong):
x2 = other2.data
elif isinstance(other2, type(self.data)):
x2 = other2
else:
x2 = long(other2)
return self.__class__(pow(self.data, x, x2))
def __rpow__(self, *args):
print "rpow:", args
return self.data
def __mod__(self, other):
if isinstance(other, UserLong):
return self.__class__(self.data % other.data)
elif isinstance(other, type(self.data)):
return self.__class__(self.data % other)
else:
return self.__class__(self.data % long(other))
def __rmod__(self, other):
if isinstance(other, UserLong):
return self.__class__(other.data % self.data)
elif isinstance(other, type(self.data)):
return self.__class__(other % self.data)
else:
return self.__class__(long(other) % self.data)
def __divmod__(self, m):
return self.__class__(divmod(self.data, m))
def __div__(self, n):
return self.__class__(self.data/n)
def __rdiv__(self, n):
return self.__class__(n/self.data)
def __mul__(self, n):
return self.__class__(self.data*n)
__rmul__ = __mul__
def __imul__(self, n):
self.data *= n
return self
def __and__(self, n):
return self.__class__(self.data&n)
__rand__ = __and__
def __iand__(self, n):
self.data &= n
return self
x = UserLong(5L)
y = UserLong(3L)
print "int", int(y)
print "long", long(y)
print "5**int(y)", 5 ** int(y)
print "5**y", 5 ** y
print "x^y%26", pow(x, y, 26)
print "5^y%26", pow(5, y, 26)
print "x^3%26", pow(x, 3, 26)
print x % y
print 5 % y
print x % 3
print "x-y", x - y
print "5-y", 5 - y
print "x-3", x - 3
print "x/y", x / y
print "5/y", 5 / y
print "x/3", x / 3