Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
.github
docs Move ast definition to stdlib Feb 2, 2018
prettyprinter
tests
.editorconfig
.gitignore
.travis.yml
AUTHORS.rst
HISTORY.rst Update HISTORY and AUTHORS Jul 25, 2018
LICENSE
MANIFEST.in
Makefile
README.rst Move ast definition to stdlib Feb 2, 2018
prettyprinterlightscreenshot.png
prettyprinterscreenshot.png
requirements_dev.txt Test Django prettyprinter definitions Jan 19, 2018
setup.cfg
setup.py Bump version: 0.13.2 → 0.14.0 Jul 25, 2018
tox.ini Configure testing for Python 3.5 Jan 11, 2018

README.rst

PrettyPrinter

Documentation

Syntax-highlighting, declarative and composable pretty printer for Python 3.5+

pip install prettyprinter
  • Drop in replacement for the standard library pprint: just rename pprint to prettyprinter in your imports.
  • Uses a modified Wadler-Leijen layout algorithm for optimal formatting
  • Write pretty printers for your own types with a dead simple, declarative interface

Pretty print common Python values:

>>> from datetime import datetime
>>> from prettyprinter import pprint
>>> pprint({'beautiful output': datetime.now()})
{
    'beautiful output': datetime.datetime(
        year=2017,
        month=12,
        day=12,
        hour=0,
        minute=43,
        second=4,
        microsecond=752094
    )
}

As well as your own, without any manual string formatting:

>>> class MyClass:
...     def __init__(self, one, two):
...         self.one = one
...         self.two = two

>>> from prettyprinter import register_pretty, pretty_call

>>> @register_pretty(MyClass)
... def pretty_myclass(value, ctx):
...     return pretty_call(ctx, MyClass, one=value.one, two=value.two)

>>> pprint(MyClass((1, 2, 3), {'a': 1, 'b': 2}))
MyClass(one=(1, 2, 3), two={'a': 1, 'b': 2})

>>> pprint({'beautiful output': datetime.now(), 'beautiful MyClass instance': MyClass((1, 2, 3), {'a': 1, 'b': 2})})
{
    'beautiful MyClass instance': MyClass(
        one=(1, 2, 3),
        two={'a': 1, 'b': 2}
    ),
    'beautiful output': datetime.datetime(
        year=2017,
        month=12,
        day=12,
        hour=0,
        minute=44,
        second=18,
        microsecond=384219
    )
}

Comes packaged with the following pretty printer definitions:

  • datetime - (installed by default)
  • enum - (installed by default)
  • pytz - (installed by default)
  • dataclasses - any new class you create will be pretty printed automatically
  • attrs - any new class you create will be pretty printed automatically
  • django - your Models and QuerySets will be pretty printed automatically
  • requests - automatically pretty prints Requests, Responses, Sessions, and more from the requests library