1 Replies - 1754 Views - Last Post: 29 May 2012 - 08:57 AM

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,257
  • Joined: 23-December 08

Integer of any base

Posted 29 May 2012 - 08:56 AM

Description: To create a value of a different base, the syntax is baseN(value, base) for example, to create an object that represent the number 10 in base 2 would be baseN(10,2) Bases must be positive integers, values are automatically converted to integers.This class allows you to create an integer of any positive integer base.
__author__ = "atraub"
__date__= "5/22/2012"

class baseN:
    """Class that defines a number and a base for that number"""
    bases = ('0', '1', '2', '3', '4', '5',
             '6', '7', '8', '9',
             'A', 'B', 'C', 'D', 'E', 'F',
             'G', 'H', 'I', 'J', 'K', 'L',
             'M', 'N', 'O', 'P', 'Q', 'R',
             'S', 'T', 'U', 'V', 'W', 'X',
             'Y', 'Z', 'a', 'b', 'c', 'd',
             'e', 'f', 'g', 'h', 'i', 'j',
             'k', 'l', 'm', 'n', 'o', 'p',
             'q', 'r', 's', 't', 'u', 'v',
             'w', 'x', 'y', 'z',)

    def __init__(self, value=1, base=10):
        """Creates an integer of a given base from a given value and base"""
        self.__validateBase(base)
        self.value = int(value)
        self.__base = base

    def changeBase(self, base):
        """change the number's base"""
        self.__validateBase(base)
        self.__base = base
    
    def __validateBase(self, base):
        """checks if the value uses a valid base and throws an error accordingly"""
        if "." in str(base) or base <= 0:
            raise Exception("Bases must be positive integers")

    def __str__(self):
        return str(self.__changeBase(self.value, self.__base))

    def __repr__(self):
        return str(self.__changeBase(self.value, self.__base))

    def __int__(self):
        return self.value

    def __pos__(self):
        return self.__changeBase(+self.value, self.__base)

    def __neg__(self):
        return self.__changeBase(-self.value, self.__base)

    def __abs__(self):
        return self.__changeBase(abs(self.value), self.__base)

    def __invert__(self):
        return self.__changeBase(~self.value, self.__base)
        
    def __add__(self, value):
        return self.__changeBase(self.value + int(value), self.__base)

    def __sub__(self, value):
        return self.__changeBase(self.value - int(value), self.__base)

    def __mul__(self,value):
        return self.__changeBase(self.value * int(value), self.__base)

    def __div__(self,value):
        return self.__changeBase(self.value / int(value), self.__base)

    def __truediv__(self, value):
        return self.__changeBase(self.value / int(value), self.__base)

    def __floordiv__(self,value):
        return self.__changeBase(self.value // int(value), self.__base)

    def __mod__(self, value):
        return self.__changeBase(self.value % int(value), self.__base)

    def __pow__(self, value):
        return self.__changeBase(self.value ** int(value), self.__base)

    def __lshift__(self, value):
        return self.__changeBase(self.value << int(value), self.__base)

    def __rshift__(self, value):
        return self.__changeBase(self.value >> int(value), self.__base)

    def __and__(self, value):
        return self.__changeBase(self.value & int(value), self.__base)

    def __or__(self, value):
        return self.__changeBase(self.value | int(value), self.__base)

    def __xor__(self, value):
        return self.__changeBase(self.value ^ int(value), self.__base)

    def __radd__(self, value):
        return self.__changeBase(int(value) + self.value, self.__base)

    def __rsub__(self, value):
        return self.__changeBase(int(value) - self.value, self.__base)

    def __rmul__(self,value):
        return self.__changeBase(int(value) * self.value, self.__base)

    def __rdiv__(self,value):
        return self.__changeBase(int(value) / self.value, self.__base)

    def __rtruediv__(self,value):
        return self.__changeBase(int(value) / self.value, self.__base)

    def __rfloordiv__(self,value):
        return self.__changeBase(int(value) // self.value, self.__base)

    def __rmod__(self, value):
        return self.__changeBase(int(value) % self.value, self.__base)

    def __rpow__(self, value):
        return self.__changeBase(int(value) ** self.value, self.__base)

    def __rlshift__(self, value):
        return self.__changeBase(int(value) << self.value, self.__base)

    def __rshift__(self, value):
        return self.__changeBase(int(value) >> self.value, self.__base)

    def __rand__(self, value):
        return self.__changeBase(int(value) & self.value, self.__base)

    def __ror__(self, value):
        return self.__changeBase(int(value) | self.value, self.__base)

    def __rxor__(self, value):
        return self.__changeBase(int(value) ^ self.value, self.__base)

    def __iadd__(self, other):
        self.value += other
        return self
        
    def __isub__(self, other):
        self.value -= other
        return self

    def __imul__(self, other):
        self.value *= other
        return self

    def __ifloordiv__(self, other):
        self.value //= other
        return self

    def __idiv__(self, other):
        self.value /= other
        return self

    def __itruediv__(self, other):
        self.value /= other
        return self

    def __imod__(self, other):
        self.__valie %= other
        return self
        
    def __ipow__(self, other):
        self.value **= other
        return self

    def __ilshift__(self, other):
        self.value <<= other
        return self

    def __irshift__(self, other):
        self.value >>= other
        return self

    def __iand__(self, other):
        self.value &= other
        return self

    def __ior__(self, other):
        self.value |= other
        return self

    def __ixor__(self, other):
        self.value ^= other
        return self
    
    def __changeBase(self, value, newBase):
        """changes a base 10 integer to any other base, used for display purposes"""
        output = ""
        newValue = abs(int(value))#forces the input to be a positive integer
        
        #base 1 is just ticks
        if newBase == 1:
            output = "1"*newValue

        #handles the general case
        else:
            while True:
                if newValue == 0:
                    break
                output = baseN.bases[(newValue%newBase)] + output
                newValue //= newBase
            
        if value < 0: #Add negative back in if necesary
            output = "-"+output
        return output



Is This A Good Question/Topic? 0
  • +

Replies To: Integer of any base

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,257
  • Joined: 23-December 08

Re: Integer of any base

Posted 29 May 2012 - 08:57 AM

Compatible with both Python2 and Python3!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1