26

Customizing pprint.PrettyPrinter

The documentation for the pprint module mentions that the method PrettyPrinter.format is intended to make it possible to customize formatting.

I gather that it's possible to override this method in a subclass, but this doesn't seem to provide a way to have the base class methods apply line wrapping and indentation.

  • Am I missing something here?
  • Is there a better way to do this (e.g. another module)?

Alternatives?

I've checked out the pretty module, which looks interesting, but doesn't seem to provide a way to customize formatting of classes from other modules without modifying those modules.

I think what I'm looking for is something that would allow me to provide a mapping of types (or maybe functions) that identify types to routines that process a node. The routines that process a node would take a node and return the string representation it, along with a list of child nodes. And so on.

Why I’m looking into pretty-printing

My end goal is to compactly print custom-formatted sections of a DocBook-formatted xml.etree.ElementTree.

(I was surprised to not find more Python support for DocBook. Maybe I missed something there.)

I built some basic functionality into a client called xmlearn that uses lxml. For example, to dump a Docbook file, you could:

xmlearn -i docbook_file.xml dump -f docbook -r book

It's pretty half-ass, but it got me the info I was looking for.

xmlearn has other features too, like the ability to build a graph image and do dumps showing the relationships between tags in an XML document. These are pretty much totally unrelated to this question.

You can also perform a dump to an arbitrary depth, or specify an XPath as a set of starting points. The XPath stuff sort of obsoleted the docbook-specific format, so that isn't really well-developed.

This still isn't really an answer for the question. I'm still hoping that there's a readily customizable pretty printer out there somewhere.

Zearin
  • 1,474
  • 2
  • 17
  • 36
intuited
  • 23,174
  • 7
  • 66
  • 88

4 Answers4

5

My solution was to replace pprint.PrettyPrinter with a simple wrapper that formats any floats it finds before calling the original printer.

from __future__ import division
import pprint
if not hasattr(pprint,'old_printer'):
    pprint.old_printer=pprint.PrettyPrinter

class MyPrettyPrinter(pprint.old_printer):
    def _format(self,obj,*args,**kwargs):
        if isinstance(obj,float):
            obj=round(obj,4)
        return pprint.old_printer._format(self,obj,*args,**kwargs)
pprint.PrettyPrinter=MyPrettyPrinter

def pp(obj):
    pprint.pprint(obj)

if __name__=='__main__':
    x=[1,2,4,6,457,3,8,3,4]
    x=[_/17 for _ in x]
    pp(x)
Josh
  • 173
  • 3
  • 7
2

If you would like to modify the default pretty printer without subclassing, you can use the internal _dispatch table on the pprint.PrettyPrinter class. You can see how examples of how dispatching is added for internal types like dictionaries and lists in the source.

Here is how I added a custom pretty printer for MatchPy's Operation type:

import pprint
import matchpy

def _pprint_operation(self, object, stream, indent, allowance, context, level):
    """
    Modified from pprint dict https://github.com/python/cpython/blob/3.7/Lib/pprint.py#L194
    """
    operands = object.operands
    if not operands:
        stream.write(repr(object))
        return
    cls = object.__class__
    stream.write(cls.__name__ + "(")
    self._format_items(
        operands, stream, indent + len(cls.__name__), allowance + 1, context, level
    )
    stream.write(")")


pprint.PrettyPrinter._dispatch[matchpy.Operation.__repr__] = _pprint_operation

Now if I use pprint.pprint on any object that has the same __repr__ as matchpy.Operation, it will use this method to pretty print it. This works on subclasses as well, as long as they don't override the __repr__, which makes some sense! If you have the same __repr__ you have the same pretty printing behavior.

Here is an example of the pretty printing some MatchPy operations now:

ReshapeVector(Vector(Scalar('1')),
              Vector(Index(Vector(Scalar('0')),
                           If(Scalar('True'),
                              Scalar("ReshapeVector(Vector(Scalar('2'), Scalar('2')), Iota(Scalar('10')))"),
                              Scalar("ReshapeVector(Vector(Scalar('2'), Scalar('2')), Ravel(Iota(Scalar('10'))))")))))
saul.shanabrook
  • 3,068
  • 3
  • 31
  • 49
2

This question may be a duplicate of:


Using pprint.PrettyPrinter

I looked through the source of pprint. It seems to suggest that, in order to enhance pprint(), you’d need to:

  • subclass PrettyPrinter
  • override _format()
  • test for issubclass(),
  • and (if it's not your class), pass back to _format()

Alternative

I think a better approach would be just to have your own pprint(), which defers to pprint.pformat when it doesn't know what's up.

For example:

'''Extending pprint'''

from pprint import pformat

class CrazyClass: pass

def prettyformat(obj):
    if isinstance(obj, CrazyClass):
        return "^CrazyFoSho^"
    else:
        return pformat(obj)

def prettyp(obj):
    print(prettyformat(obj))

# test
prettyp([1]*100)
prettyp(CrazyClass())

The big upside here is that you don't depend on pprint internals. It’s explicit and concise.

The downside is that you’ll have to take care of indentation manually.

Community
  • 1
  • 1
Mr Temp
  • 41
  • 5
  • 3
    Another downside of **Alternative** is that `prettyp([CrazyClass()])` won't work. – mic_e Dec 01 '16 at 10:18
  • 1
    See this patch, which was applied to Python quite some time ago: http://bugs.python.org/issue1373762 This allows you to subclass PrettyPrinter and override `format`. An example is given in the text. Note that the example is flawed because `True` and `False` will be printed as `0x1` and `0x0`, which hides the fact that they are `bool`. – mic_e Dec 01 '16 at 10:23
0

Consider using the pretty module:

Carl Smith
  • 3,025
  • 24
  • 36
miracle2k
  • 29,597
  • 21
  • 65
  • 64
  • 1
    an improved and maintained version that supports Python 3.x can be found in IPython (as IPython.lib.pretty): http://ipython.org/ipython-doc/stable/api/generated/IPython.lib.pretty.html – Mikhail Korobov Sep 08 '13 at 12:33
  • This answer is link-only, you should expand the answer to explain how should the module be used. – user202729 Jan 29 '21 at 08:54