[Python-ideas] Columns for pprint.PrettyPrinter

Julian Berman julian at grayvines.com
Sat Aug 20 01:23:45 CEST 2011

For the most part I find pprint to be lovely. Simple and does a useful thing that I don't want to have to write every time, especially if I'm using it just to debug.

However, I often find that I'm using it to print out a long-length singly nested structure whose elements' reprs are not really that long, for which its output is cumbersome. Try to pprint.pprint(range(100)). Instead of seeing a long really skinny column, it'd be nice if it could quickly provide a way (combined with the width arg that it already takes) to split up the elements of the structure within the width, so that instead of seeing things like

    >>> pprint.pprint(range(30))

it could be coerced into something like

    >>> pprint.pprint(range(30), columns=5)
    [0, 1, 2, 3, 4
     3, 4, 5, 6, 7, ... ]

or for something nested, which I'm less thrilled with, and haven't thought out how to implement unless you have a somewhat balanced structure, but for posterity:

    {"foo" :
               {"bar" : 1,           {"hello" : 2,               {"other" : 1,
                "baz" : 2,            "world" : 1},              "thing" : 2,
                 "foo" : 3},                                           "here" : 3},


Obviously it's meant to be simple, the comment at the top of the module even says so, and doing something like ^ is easy enough, but for what it's good for (saving me from having to write code that makes my objects easier to debug by displaying them nicely), just making it do a bit more would make life easier.

More information about the Python-ideas mailing list