views:

127

answers:

4

I have the pretty print module, which I prepared because I was not happy the pprint module produced zillion lines for list of numbers which had one list of list. Here is example use of my module.

    >>> a=range(10)
    >>> a.insert(5,[range(i) for i in range(10)])
    >>> a
    [0, 1, 2, 3, 4, [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]], 5, 6, 7, 8, 9]
    >>> import pretty
    >>> pretty.ppr(a,indent=6)

    [0, 1, 2, 3, 4, 
          [
            [], 
            [0], 
            [0, 1], 
            [0, 1, 2], 
            [0, 1, 2, 3], 
            [0, 1, 2, 3, 4], 
            [0, 1, 2, 3, 4, 5], 
            [0, 1, 2, 3, 4, 5, 6], 
            [0, 1, 2, 3, 4, 5, 6, 7], 
            [0, 1, 2, 3, 4, 5, 6, 7, 8]], 5, 6, 7, 8, 9]

Code is like this:

""" pretty.py prettyprint module version alpha 0.2
    mypr: pretty string function
    ppr:  print of the pretty string
    ONLY list and tuple prettying implemented!
"""
def mypr(w, i = 0, indent = 2, nl = '\n') :
    """ w = datastructure, i = indent level, indent = step size for indention """
    startend = {list : '[]', tuple : '()'}
    if type(w) in (list, tuple) :
        start, end = startend[type(w)]
        pr = [mypr(j, i + indent, indent, nl) for j in w]
        return nl + ' ' * i + start + ', '.join(pr) + end
    else :  return repr(w)

def ppr(w, i = 0, indent = 2, nl = '\n') :
    """ see mypr, this is only print of mypr with same parameters """
    print mypr(w, i, indent, nl)

Here is one fixed text for table printing in my pretty print module:

## let's do it "manually"
width = len(str(10+10))
widthformat = '%'+str(width)+'i'
for i in range(10):
    for j in range(10):
        print widthformat % (i+j),
    print

Have you better alternative for this code to be generalized enough for the pretty printing module?

What I found for this kind of regular cases after posting the question is this module: prettytable A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

+7  A: 

If you're looking for nice formatting for matrices, numpy's output looks great right out of the box:

from numpy import *
print array([[i + j for i in range(10)] for j in range(10)])

Output:

[[ 0  1  2  3  4  5  6  7  8  9]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 2  3  4  5  6  7  8  9 10 11]
 [ 3  4  5  6  7  8  9 10 11 12]
 [ 4  5  6  7  8  9 10 11 12 13]
 [ 5  6  7  8  9 10 11 12 13 14]
 [ 6  7  8  9 10 11 12 13 14 15]
 [ 7  8  9 10 11 12 13 14 15 16]
 [ 8  9 10 11 12 13 14 15 16 17]
 [ 9 10 11 12 13 14 15 16 17 18]]
John Kugelman
This solution is nice, but it is not general enough to put in pretty print module. Good response for my first request of doing this as automatically as possible. From the layout of your code which is str not repr, I desided to change the others line to else : return str(w)
Tony Veijalainen
+3  A: 

You can write:

'\n'.join(  # join the lines with '\n'
       ' '.join(  # join one line with ' '
              "%2d" % (i + j) # format each item
        for i in range(10))
    for j in range(10))
THC4k
Not so bad at all, but every odd day I still think it ugly to do this kind of tricks for concatenating of strings, every even day I think it a super-Pythonic and smile to myself while writing such a Clever Pythonic code ('\n'.join instead of multiple prints).
Tony Veijalainen
+1  A: 

Using George Sakkis' table indention recipe:

print(indent(((i + j for i in range(10)) for j in range(10)),
             delim=' ', justify='right'))

yields:

0  1  2  3  4  5  6  7  8  9
1  2  3  4  5  6  7  8  9 10
2  3  4  5  6  7  8  9 10 11
3  4  5  6  7  8  9 10 11 12
4  5  6  7  8  9 10 11 12 13
5  6  7  8  9 10 11 12 13 14
6  7  8  9 10 11 12 13 14 15
7  8  9 10 11 12 13 14 15 16
8  9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18

PS. To get the above to work, I made one minor change to the recipe. I changed wrapfunc(item) to wrapfunc(str(item)):

def rowWrapper(row):
    newRows = [wrapfunc(str(item)).split('\n') for item in row]
unutbu
Looks simple until you look the amount of code in behind the linked recipe. Compared to for example the 9 non-comment lines of my pretty functions.
Tony Veijalainen
A: 

My answer to this kind of regular cases would be to use this module: prettytable A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

Tony Veijalainen