def add(self, iterable): if it.is_emply(iterable): self.end = self.end elif it.is_emply(self.end): self.end = iterable else: self.end = iter2(self.end) self.end.add(iterable)
def __init__(self, *args, base): if len(args) == 1: if isinstance(args[0], Family): self.list = args[0].list[:] self.base = args[0].base return if isinstance(args[0], DigitsList): self.list = [args[0]] self.base = args[0].base return if it.is_iterable(args[0]): args = list(args[0]) else: args = [args[0]] self.base = base self.list = [] for i in args: if isinstance(i, int): i = str_base(i, self.base) if isinstance(i, str): try: int(i, base) except: raise TypeError("'%s' isn't a base %s number" % (i, base)) self.list.append(i) elif it.is_iterable(i): if it.is_emply(i): pass if len(i) == 1: self.list.append(Digits(i[0], base=self.base)) else: self.list.append(DigitsList(i, base=self.base))
def __mul__(self, other): if isinstance(other, int): other = str_base(other, base=self.base) if isinstance(other, str): other = DigitsList([other], base=self.base) if it.is_iterable(other): if it.is_emply(other): return self other = DigitsList(other, base=self.base) if self.base != other.base: raise Exception("base must be equal") return DigitsList(list( map(lambda x: x[0] + x[1], itertools.product(self.list, other.list))), base=self.base)
def __str__(self): res = "" try: if not it.is_emply(self.start): res += str(self.start) + "+" except: res += self.start.__class__.__name__ + "+" try: if not it.is_emply(self.iterable): res += str(self.iterable) else: res += "[]" except: res += self.iterable.__class__.__name__ try: if not it.is_emply(self.end): res += "+" + str(self.end) except: res += "+" + self.end.__class__.__name__ return res
def __rmul__(self, other): if it.is_iterable(other): if it.is_emply(other): return self return DigitsList(other, base=self.base) * self