PrettyPrinter
PrettyPrinter is a Python library (version 0.18.0) that offers syntax-highlighting, declarative, and composable pretty printing for Python 3.5+ data structures. It aims to be a feature-rich alternative to Python's standard `pprint` module, providing improved readability, customizability, and integrations with other libraries. Its release cadence has been somewhat irregular, but the 0.18.0 version released in 2019 has been stable, suggesting a mature library.
Warnings
- breaking The default behavior for dictionary key sorting changed in version 0.8.0. Previously, keys were sorted like in the standard library's `pprint` module. Now, they default to insertion order (for CPython 3.6+). To revert to sorted keys, use the `sort_dict_keys` argument in `pprint` or configure it in `PrettyContext` for custom printers.
- gotcha There are two distinct Python libraries with similar names: the built-in `pprint` module and the external `prettyprinter` library. Users often confuse the two. `prettyprinter` offers enhanced features like syntax highlighting, a more flexible API, and extensibility, which the standard library module does not.
- gotcha For `prettyprinter` to automatically format output in Python shells (like IPython or the default Python REPL) or for it to pretty-print types from other popular libraries (e.g., `dataclasses`, `attrs`, `django`), you must explicitly call `install_extras()`. Without this, only basic types will be pretty-printed, and shell integration will not occur.
- deprecated When defining custom pretty printers, the `PrettyContext.set` method was deprecated in favor of `PrettyContext.assoc`. While `set` might still work in older versions, `assoc` is the recommended and clearer method for modifying context values.
- gotcha When trying to pretty-print custom objects, simply defining a `__repr__` method on your class might not fully leverage `prettyprinter`'s capabilities or might even be overridden by `prettyprinter`'s default handling for certain base types. The library's declarative approach provides a more robust way.
Install
-
pip install prettyprinter
Imports
- pprint
from prettyprinter import pprint
- install_extras
from prettyprinter import install_extras
- register_pretty
from prettyprinter import register_pretty, pretty_call
Quickstart
from prettyprinter import pprint, register_pretty, pretty_call
# Basic pretty-printing of a nested dictionary
data = {
'name': 'PrettyPrinter Example',
'details': {
'id': 12345,
'items': [
{'item_id': 'A1', 'value': 100.5, 'tags': ['new', 'sale']},
{'item_id': 'B2', 'value': 200, 'tags': ['old']},
{'item_id': 'C3', 'value': 50.75, 'tags': []}
],
'status': 'active'
},
'configs': [
{'key': 'timeout', 'value': 30},
{'key': 'retries', 'value': 5}
]
}
print('--- Standard print ---')
print(data)
print('\n--- PrettyPrinter output ---')
pprint(data)
# Custom pretty-printing for a user-defined class
class MyObject:
def __init__(self, name, values):
self.name = name
self.values = values
@register_pretty(MyObject)
def pretty_my_object(value, ctx):
return pretty_call(
ctx,
MyObject,
name=value.name,
values=value.values
)
my_instance = MyObject('CustomItem', [{'x': 1, 'y': 2}, {'z': 3}])
print('\n--- Custom object pretty-printing ---')
pprint(my_instance)
# To enable shell integration (e.g., for `ipython` or default python shell):
# from prettyprinter import install_extras
# install_extras(['ipython']) # or ['python']
# {'a': 1, 'b': 2} # will now be pretty-printed in the shell