views:

105

answers:

0

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, eg another module? 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.

edit 1 (2010-07-15 10:48): add {{{

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 the node and return the string representation of that node along with a list of child nodes. And so on.

}}}

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.

edit 2 (2010-07-19 16:50): add {{{

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

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

It's pretty halfass but it got me the info I was looking for. You can also do 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 is still not really an answer for the question; I'm still hoping that there's a readily customizable pretty printer out there somewhere.

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.

}}}