Python 3.x: alternative pprint implementation

There is an improved and maintained Python 2.x/3.x port of "pretty" library in IPython: https://ipython.readthedocs.io/en/stable/api/generated/IPython.lib.pretty.html


for pretty printing you may be looking for __str__ instead of (or as well as) __repr__

e.g.

>>> import datetime
>>> now = datetime.datetime.now()
>>> print now
2013-05-19 13:00:34.085383
>>> print repr(now)
datetime.datetime(2013, 5, 19, 13, 0, 34, 85383)

If the pretty module satisfies your needs, you can make it work with Python 3.

  1. Download and unpack the pretty.py file.
  2. Run 2to3 on it:

    python -m lib2to3 -w pretty.py
    
  3. Comment out the following lines:

    569: types.DictProxyType:        _dict_pprinter_factory('<dictproxy {', '}>'),
    580: xrange:                     _repr_pprint,
    
  4. Put the file near your script.

  5. Import it as usual:

    import pretty