Beispiel #1
0
 def _repr_(self):
     from printing import repr_operator
     approximation = "%s" % self._approximation
     if self.is_exact():
         return approximation
     else:
         precision = "%s" % self._precision
         return repr_operator(" + ", [approximation, precision])
Beispiel #2
0
    def repr(self):
        from printing import repr_parenthesis, repr_operator, repr_exponant, repr_index, repr_function

        name = self.name
        if self.__dict__.has_key("args"):
            args = []
            parenthesis = []
            for arg in self.args:
                level = 0
                if hasattr(arg, "parenthesis_level"):
                    level = arg.parenthesis_level()
                elif hasattr(arg, "is_atomic") and self._parenthesis_level < Infinity:
                    if arg.is_atomic():
                        level = 3
                elif isinstance(arg, Integer):
                    level = 3
                args.append("%s" % arg)
                parenthesis.append(level)
            s = ""
            if name == "add":
                s = repr_operator(" + ", args)
            elif name == "sub" and len(args) == 2:
                if parenthesis[1] < 1:
                    args[1] = repr_parenthesis(args[1])
                s = repr_operator(" - ", args)
            elif name == "neg" and len(args) == 1:
                if parenthesis[0] < 1:
                    args[0] = repr_parenthesis("%s" % args[0])
                s = repr_operator("-", ["", args[0]])
            elif name == "mul":
                for i in range(len(args)):
                    if parenthesis[i] < 1:
                        args[i] = repr_parenthesis(args[i])
                s = repr_operator("*", args)
            elif name == "div" and len(args) == 2:
                if parenthesis[0] < 1:
                    args[0] = repr_parenthesis(args[0])
                if parenthesis[1] < 2:
                    args[1] = repr_parenthesis(args[1])
                s = repr_operator("/", args)
            elif name == "invert" and len(args) == 1:
                if parenthesis[0] < 2:
                    args[0] = repr_parenthesis(args[0])
                s = repr_operator("/", ["1", args[0]])
            elif name == "pow" and len(args) == 2:
                if parenthesis[0] < 3:
                    args[0] = repr_parenthesis(args[0])
                if parenthesis[1] < 3:
                    args[1] = repr_parenthesis(args[1])
                s = repr_exponant(args[0], args[1])
            elif name == "getitem" and len(args) == 2:
                if parenthesis[0] < 2:
                    args[0] = repr_parenthesis("%s" % args[0])
                s = repr_index(args[0], args[1])
            else:
                s = repr_function(name, args)
            return s
        else:
            return "%s(...)" % name