Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to implement custom pretty-printers

Tags:

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.

like image 508
intuited Avatar asked Jul 15 '10 17:07

intuited


People also ask

Which module is required in pretty printing?

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.

What is pretty print format?

Prettyprint is the process of converting and presenting source code or other objects in a legible and attractive way. A prettyprinter takes blocks of code and prints them in an aesthetically pleasing fashion, presenting the characters with line breaks and indentations to make the code comprehensible.

How do you print a structure in Python?

The pprint is a Python module that pleasingly prints the data structure. It's been around for a long time as included in the Python standard library; therefore, installing it as a separate program is not required. All we have to do is install its function pprint() function.


1 Answers

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) 
like image 170
Josh Avatar answered Sep 29 '22 18:09

Josh