def testPrintLevelLength(self):
     levelLengths = {
         (0, 1): "#",
         (1, 1): "(if ...)",
         (1, 2): "(if # ...)",
         (1, 3): "(if # # ...)",
         (1, 4): "(if # # #)",
         (2, 1): "(if ...)",
         (2, 2): "(if (member x ...) ...)",
         (2, 3): "(if (member x y) (+ # 3) ...)",
         (3, 2): "(if (member x ...) ...)",
         (3, 3): "(if (member x y) (+ (car x) 3) ...)",
         (3, 4): "(if (member x y) (+ (car x) 3) (foo (a b c d ...)))"
     }
     sexp = ("if", ("member", "x", "y"), ("+", ("car", "x"), 3),
             ("foo", ("a", "b", "c", "d", "Baz")))
     for (level, length) in [(0, 1), (1, 2), (1, 2), (1, 3), (1, 4), (2, 1),
                             (2, 2), (2, 3), (3, 2), (3, 3), (3, 4)]:
         with bindings(printervars,
                       print_pretty=True,
                       print_escape=False,
                       print_level=level,
                       print_length=length):
             s = format(None, "~W", sexp)
             self.assertEqual(levelLengths[(level, length)],
                              s.replace(",", ""))
示例#2
0
    def format(self, stream, args):
        def commafy(s, commachar, comma_interval):
            """Add commachars between groups of comma_interval digits."""
            first = len(s) % comma_interval
            a = [s[0:first]] if first > 0 else []
            for i in range(first, len(s), comma_interval):
                a.append(s[i:i + comma_interval])
            return commachar.join(a)

        i = 0
        if self.radix:
            radix = self.radix
        else:
            radix = int(self.param(0, args, 0)); i += 1
        mincol = int(self.param(i, args, 0)); i += 1
        padchar = str(self.param(i, args, " ")); i += 1
        commachar = str(self.param(i, args, ",")); i += 1
        comma_interval = int(self.param(i, args, 3)); i += 1

        n = args.next()
        s = self.convert(abs(n), radix)
        sign = ("+" if n >= 0 else "-") if self.atsign else \
               ("-" if n < 0 else "")
        if self.colon:
            if padchar == "0" and mincol > len(s) + len(sign):
                # We pad with zeros first so that they can be commafied,
                # too (cf. CLiki Issue FORMAT-RADIX-COMMACHAR).  But in
                # order to figure out how many to add, we need to solve a
                # little constraint problem.
                def col(n):
                    return n + (n-1)/comma_interval + len(sign)
                width = -1
                for i in range(len(s), mincol):
                    if col(i) == mincol:
                        # exact fit
                        width = i
                        break
                    elif col(i) > mincol:
                        # too big
                        width = i - 1
                        break
                assert width > 0, "couldn't find a width"
                s = s.rjust(width, padchar)

                # If we're printing a sign, and the width that we chose
                # above is a multiple of comma_interval, we'll need (at
                # most one) extra space to get up to mincol.
                padchar = " "

            s = commafy(s, commachar, comma_interval)
        with bindings(printervars,
                      print_escape=False,
                      print_radix=False,
                      print_base=radix,
                      print_readably=False):
            stream.write((sign + s).rjust(mincol, padchar))
 def testPrintLength(self):
     lengths = [
         "(...)", "(1, ...)", "(1, 2, ...)", "(1, 2, 3, ...)",
         "(1, 2, 3, 4, ...)", "(1, 2, 3, 4, 5, ...)", "(1, 2, 3, 4, 5, 6)",
         "(1, 2, 3, 4, 5, 6)"
     ]
     a = (1, 2, 3, 4, 5, 6)
     for i in range(7):
         with bindings(printervars, print_length=i):
             self.ppEquals(lengths[i], a)
 def testPrintLevel(self):
     levels = [
         "#", "(1, #)", "(1, (2, #))", "(1, (2, (3, #)))",
         "(1, (2, (3, (4, #))))", "(1, (2, (3, (4, (5, #)))))",
         "(1, (2, (3, (4, (5, (6,))))))", "(1, (2, (3, (4, (5, (6,))))))"
     ]
     a = (1, (2, (3, (4, (5, (6, ))))))
     for i in range(8):
         with bindings(printervars, print_level=i):
             self.ppEquals(levels[i], a)
 def testPrintLevel(self):
     levels = ["#",
               "(1, #)",
               "(1, (2, #))",
               "(1, (2, (3, #)))",
               "(1, (2, (3, (4, #))))",
               "(1, (2, (3, (4, (5, #)))))",
               "(1, (2, (3, (4, (5, (6,))))))",
               "(1, (2, (3, (4, (5, (6,))))))"]
     a = (1, (2, (3, (4, (5, (6,))))))
     for i in range(8):
         with bindings(printervars, print_level=i):
             self.ppEquals(levels[i], a)
 def testPrintLength(self):
     lengths = ["(...)",
                "(1, ...)",
                "(1, 2, ...)",
                "(1, 2, 3, ...)",
                "(1, 2, 3, 4, ...)",
                "(1, 2, 3, 4, 5, ...)",
                "(1, 2, 3, 4, 5, 6)",
                "(1, 2, 3, 4, 5, 6)"]
     a = (1, 2, 3, 4, 5, 6)
     for i in range(7):
         with bindings(printervars, print_length=i):
             self.ppEquals(lengths[i], a)
 def testPrintLevelLength(self):
     levelLengths = {
         (0, 1): "#",
         (1, 1): "(if ...)",
         (1, 2): "(if # ...)",
         (1, 3): "(if # # ...)",
         (1, 4): "(if # # #)",
         (2, 1): "(if ...)",
         (2, 2): "(if (member x ...) ...)",
         (2, 3): "(if (member x y) (+ # 3) ...)",
         (3, 2): "(if (member x ...) ...)",
         (3, 3): "(if (member x y) (+ (car x) 3) ...)",
         (3, 4): "(if (member x y) (+ (car x) 3) (foo (a b c d ...)))"
     }
     sexp = ("if", ("member", "x", "y"), ("+", ("car", "x"), 3),
             ("foo", ("a", "b", "c", "d", "Baz")))
     for (level, length) in [(0, 1), (1, 2), (1, 2), (1, 3), (1, 4),
                             (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4)]:
         with bindings(printervars,
                       print_pretty=True, print_escape=False,
                       print_level=level, print_length=length):
             s = format(None, "~W", sexp)
             self.assertEqual(levelLengths[(level, length)],
                              s.replace(",", ""))
示例#8
0
def pprint(obj, *args, **kwargs):
    pp = PrettyPrinter(*args, **kwargs)
    with bindings(printervars, print_pretty=True):
        pp.pprint(obj)
    pp.terpri()
    pp.close()
示例#9
0
 def format_with_bindings(self, stream, args):
     with bindings(printervars, **self.bindings):
         stream.pprint(args.next())
示例#10
0
 def format(self, stream, args):
     with bindings(printervars, print_escape=True):
         super(Standard, self).format(stream, args)
示例#11
0
 def format(self, stream, args):
     with bindings(printervars, print_escape=None):
         super(Aesthetic, self).format(stream, args)
示例#12
0
    def format(self, stream, args):
        def commafy(s, commachar, comma_interval):
            """Add commachars between groups of comma_interval digits."""
            first = len(s) % comma_interval
            a = [s[0:first]] if first > 0 else []
            for i in range(first, len(s), comma_interval):
                a.append(s[i:i + comma_interval])
            return commachar.join(a)

        i = 0
        if self.radix:
            radix = self.radix
        else:
            radix = int(self.param(0, args, 0))
            i += 1
        mincol = int(self.param(i, args, 0))
        i += 1
        padchar = str(self.param(i, args, " "))
        i += 1
        commachar = str(self.param(i, args, ","))
        i += 1
        comma_interval = int(self.param(i, args, 3))
        i += 1

        n = args.next()
        s = self.convert(abs(n), radix)
        sign = ("+" if n >= 0 else "-") if self.atsign else \
               ("-" if n < 0 else "")
        if self.colon:
            if padchar == "0" and mincol > len(s) + len(sign):
                # We pad with zeros first so that they can be commafied,
                # too (cf. CLiki Issue FORMAT-RADIX-COMMACHAR).  But in
                # order to figure out how many to add, we need to solve a
                # little constraint problem.
                def col(n):
                    return n + (n - 1) / comma_interval + len(sign)

                width = -1
                for i in range(len(s), mincol):
                    if col(i) == mincol:
                        # exact fit
                        width = i
                        break
                    elif col(i) > mincol:
                        # too big
                        width = i - 1
                        break
                assert width > 0, "couldn't find a width"
                s = s.rjust(width, padchar)

                # If we're printing a sign, and the width that we chose
                # above is a multiple of comma_interval, we'll need (at
                # most one) extra space to get up to mincol.
                padchar = " "

            s = commafy(s, commachar, comma_interval)
        with bindings(printervars,
                      print_escape=False,
                      print_radix=False,
                      print_base=radix,
                      print_readably=False):
            stream.write((sign + s).rjust(mincol, padchar))
示例#13
0
def pprint(obj, *args, **kwargs):
    pp = PrettyPrinter(*args, **kwargs)
    with bindings(printervars, print_pretty=True):
        pp.pprint(obj)
    pp.terpri()
    pp.close()
示例#14
0
 def format_with_bindings(self, stream, args):
     with bindings(printervars, **self.bindings):
         stream.pprint(args.next())
示例#15
0
 def format(self, stream, args):
     with bindings(printervars, print_escape=True):
         super(Standard, self).format(stream, args)
示例#16
0
 def format(self, stream, args):
     with bindings(printervars, print_escape=None):
         super(Aesthetic, self).format(stream, args)