[IPython-dev] An extensible pretty-printing system

Robert Kern robert.kern at gmail.com
Fri Jan 23 02:06:33 EST 2009


I ran into a project from the sandbox of the Pocoo team (particularly Armin Ronacher) for 
an extensible pretty-printing system.

   http://sandbox.pocoo.org/
   http://dev.pocoo.org/hg/sandbox/file/tip/pretty

The nice part is that you can add pretty-printers for new types. It has nice features like 
automatic wordwrapping and indentation (and unlike the standard pprint, will correctly 
indent custom __repr__s). I thought it would be good to integrate into IPython as the 
usual result_display. For example, I've implemented a pretty-printer to print dtypes with 
many fields:

def dtype_pprinter(obj, p, cycle):
     """ Pretty-printer for numpy dtypes.
     """
     if cycle:
         return p.text('dtype(...)')
     if obj.fields is None:
         p.text(repr(obj))
     else:
         p.begin_group(7, 'dtype([')
         for i, field in enumerate(obj.descr):
             if i > 0:
                 p.text(',')
                 p.breakable()
             p.pretty(field)
         p.end_group(7, '])')


In [3]: dtype([(x, float) for x in 'abcdefghijklm'])
Out[3]:
dtype([('a', '<f8'),
        ('b', '<f8'),
        ('c', '<f8'),
        ('d', '<f8'),
        ('e', '<f8'),
        ('f', '<f8'),
        ('g', '<f8'),
        ('h', '<f8'),
        ('i', '<f8'),
        ('j', '<f8'),
        ('k', '<f8'),
        ('l', '<f8'),
        ('m', '<f8')])


Is there any interest in integrating this as the default result_display?

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the IPython-dev mailing list