Skip to content
Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
Python Makefile
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Pin tox version in CircleCI Feb 26, 2019
.github Initial commit Dec 8, 2017
docs
prettyprinter Bump version: 0.17.0 → 0.18.0 Jun 22, 2019
tests Add support for SimpleNamespace Jun 22, 2019
.editorconfig Initial commit Dec 8, 2017
.gitignore Fix GH Issue #25 Feb 20, 2019
AUTHORS.rst Add entry to changelog Jun 22, 2019
HISTORY.rst
LICENSE Initial commit Dec 8, 2017
MANIFEST.in Initial commit Dec 8, 2017
Makefile Small fixes to docs Dec 31, 2017
README.rst Clarify readme Feb 27, 2019
prettyprinterlightscreenshot.png Update screenshots Dec 14, 2017
prettyprinterscreenshot.png Update screenshots Dec 14, 2017
requirements_dev.txt Pretty-print numpy scalars. Feb 26, 2019
setup.cfg Bump version: 0.17.0 → 0.18.0 Jun 22, 2019
setup.py Bump version: 0.17.0 → 0.18.0 Jun 22, 2019
tox.ini Delete unused/outdated travis config. Mar 12, 2019

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, which you can enable by calling prettyprinter.install_extras():

  • datetime - (installed by default)
  • enum - (installed by default)
  • pytz - (installed by default)
  • dataclasses - any new class you create will be pretty printed automatically
  • attrs - pretty prints any new class you create with attrs
  • django - pretty prints your Models and QuerySets
  • numpy - pretty prints numpy scalars with explicit types
  • requests - pretty prints Requests, Responses, Sessions, and more from the requests library
You can’t perform that action at this time.