A drop-in replacement for pprint that's actually pretty
Project description
Now with Python 3 support!
Installation
pprint++ can be installed with Python 2 or Python 3 using pip or easy_install:
$ pip install pprintpp - OR - $ easy_install pprintpp
Usage
pprint++ can be used in three ways:
Through the separate pp package:
$ pip install pp-ez $ python ... >>> import pp >>> pp(["Hello", "world"]) ["Hello", "world"]
For more, see https://pypi.python.org/pypi/pp-ez
As a command-line program, which will read Python literals from standard in and pretty-print them:
$ echo "{'hello': 'world'}" | pypprint {'hello': 'world'}
As an ipython extension:
In [1]: %load_ext pprintpp
This will use pprintpp for ipython’s output.
To load this extension when ipython starts, put the previous line in your startup file.
You can change the indentation level like so:
In [2]: %config PPrintPP.indentation = 4
To monkeypatch pprint:
>>> import pprintpp >>> pprintpp.monkeypatch() >>> import pprint >>> pprint.pprint(...)
Note: the original pprint module will be available with import pprint_original. Additionally, a warning will be issued if pprint has already been imported. This can be suppressed by passing quiet=True.
And, if you really want, it can even be imported as a regular module:
>>> import pprintpp >>> pprintpp.pprint(...)
Usability Protips
pp
For bonus code aesthetics, pprintpp.pprint can be imported as pp:
>>> from pprintpp import pprint as pp
>>> pp(...)
And if that is just too many letters, the pp-ez package can be installed from PyPI, ensuring that pretty-printing is never more than an import pp away:
$ pip install pp-ez $ python ... >>> import pp >>> pp(["Hello", "world"]) ["Hello", "world"]
For more, see https://pypi.python.org/pypi/pp-ez
Why is it prettier?
Unlike pprint, pprint++ strives to emit a readable, largely PEP8-compliant, representation of its input.
It also has explicit support for: the collections module (defaultdict and Counter) and numpy arrays:
>>> import numpy as np
>>> from collections import defaultdict, Counter
>>> pprint([np.array([[1,2],[3,4]]), defaultdict(int, {"foo": 1}), Counter("aaabbc")])
[
array([[1, 2],
[3, 4]]),
defaultdict(<type 'int'>, {'foo': 1}),
Counter({'a': 3, 'b': 2, 'c': 1}),
]
Unicode characters, when possible, will be printed un-escaped. This is done by checking both the output stream’s encoding (defaulting to utf-8) and the character’s Unicode category. An effort is made to print only characters which will be visually unambiguous: letters and numbers will be printed un-escaped, spaces, combining characters, and control characters will be escaped:
>>> unistr = u"\xe9e\u0301"
>>> print unistr
éé
>>> pprint(unistr)
u'ée\u0301'
The output stream’s encoding will be considered too:
>>> import io
>>> stream = io.BytesIO()
>>> stream.encoding = "ascii"
>>> pprint(unistr, stream=stream)
>>> print stream.getvalue()
u'\xe9e\u0301'
Subclassess of built-in collection types which don’t define a new __repr__ will have their class name explicitly added to their repr. For example:
>>> class MyList(list):
... pass
...
>>> pprint(MyList())
MyList()
>>> pprint(MyList([1, 2, 3]))
MyList([1, 2, 3])
Note that, as you might expect, custom __repr__ methods will be respected:
>>> class MyList(list):
... def __repr__(self):
... return "custom repr!"
...
>>> pprint(MyList())
custom repr!
Note: pprint++ is still under development, so the format will change and improve over time.
Example
With printpp:
>>> import pprintpp
>>> pprintpp.pprint(["Hello", np.array([[1,2],[3,4]])])
[
'Hello',
array([[1, 2],
[3, 4]]),
]
>>> pprintpp.pprint(tweet)
{
'coordinates': None,
'created_at': 'Mon Jun 27 19:32:19 +0000 2011',
'entities': {
'hashtags': [],
'urls': [
{
'display_url': 'tumblr.com/xnr37hf0yz',
'expanded_url': 'http://tumblr.com/xnr37hf0yz',
'indices': [107, 126],
'url': 'http://t.co/cCIWIwg',
},
],
'user_mentions': [],
},
'place': None,
'source': '<a href="http://www.tumblr.com/" rel="nofollow">Tumblr</a>',
'truncated': False,
'user': {
'contributors_enabled': True,
'default_profile': False,
'entities': {'hashtags': [], 'urls': [], 'user_mentions': []},
'favourites_count': 20,
'id_str': '6253282',
'profile_link_color': '0094C2',
},
}
Without printpp:
>>> import pprint >>> import numpy as np >>> pprint.pprint(["Hello", np.array([[1,2],[3,4]])]) ['Hello', array([[1, 2], [3, 4]])] >>> tweet = {'coordinates': None, 'created_at': 'Mon Jun 27 19:32:19 +0000 2011', 'entities': {'hashtags': [], 'urls': [{'display_url': 'tumblr.com/xnr37hf0yz', 'expanded_url': 'http://tumblr.com/xnr37hf0yz', 'indices': [107, 126], 'url': 'http://t.co/cCIWIwg'}], 'user_mentions': []}, 'place': None, 'source': '<a href="http://www.tumblr.com/" rel="nofollow">Tumblr</a>', 'truncated': False, 'user': {'contributors_enabled': True, 'default_profile': False, 'entities': {'hashtags': [], 'urls': [], 'user_mentions': []}, 'favourites_count': 20, 'id_str': '6253282', 'profile_link_color': '0094C2'}} >>> pprint.pprint(tweet) {'coordinates': None, 'created_at': 'Mon Jun 27 19:32:19 +0000 2011', 'entities': {'hashtags': [], 'urls': [{'display_url': 'tumblr.com/xnr37hf0yz', 'expanded_url': 'http://tumblr.com/xnr37hf0yz', 'indices': [107, 126], 'url': 'http://t.co/cCIWIwg'}], 'user_mentions': []}, 'place': None, 'source': '<a href="http://www.tumblr.com/" rel="nofollow">Tumblr</a>', 'truncated': False, 'user': {'contributors_enabled': True, 'default_profile': False, 'entities': {'hashtags': [], 'urls': [], 'user_mentions': []}, 'favourites_count': 20, 'id_str': '6253282', 'profile_link_color': '0094C2'}}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pprintpp-0.4.0.tar.gz
.
File metadata
- Download URL: pprintpp-0.4.0.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea826108e2c7f49dc6d66c752973c3fc9749142a798d6b254e1e301cfdbc6403 |
|
MD5 | b5b6a310c80986e38eaab9b9a978b966 |
|
BLAKE2b-256 | 061a7737e7a0774da3c3824d654993cf57adc915cb04660212f03406334d8c0b |
File details
Details for the file pprintpp-0.4.0-py2.py3-none-any.whl
.
File metadata
- Download URL: pprintpp-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6b4dcdd0c0c0d75e4d7b2f21a9e933e5b2ce62b26e1a54537f9651ae5a5c01d |
|
MD5 | 6c96b17612b15f7bd78fdb8cc498a6f9 |
|
BLAKE2b-256 | 4ed1e4ed95fdd3ef13b78630280d9e9e240aeb65cc7c544ec57106149c3942fb |