``pprint++``: a drop-in replacement for ``pprint`` that's actually pretty
=========================================================================
.. image:: https://travis-ci.org/wolever/pprintpp.svg?branch=master
:target: https://travis-ci.org/wolever/pprintpp
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:
1. 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
2. As a command-line program, which will read Python literals from standard in
and pretty-print them::
$ echo "{'hello': 'world'}" | pypprint
{'hello': 'world'}
3. As an `ipython <https://github.com/ipython/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 <https://ipython.org/ipython-doc/1/config/overview.html#startup-files>`_.
You can change the indentation level like so::
In [2]: %config PPrintPP.indentation = 4
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``.
5. 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``:
.. code:: pycon
>>> 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:
.. code:: pycon
>>> 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:
.. code:: pycon
>>> unistr = u"\xe9e\u0301"
>>> print unistr
éé
>>> pprint(unistr)
u'ée\u0301'
The output stream's encoding will be considered too:
.. code:: pycon
>>> 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:
.. code:: pycon
>>> 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:
.. code:: pycon
>>> 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``:
.. code:: pycon
>>> 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'}}
Raw data
{
"_id": null,
"home_page": "https://github.com/wolever/pprintpp",
"name": "pprintpp",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "David Wolever",
"author_email": "david@wolever.net",
"download_url": "https://files.pythonhosted.org/packages/06/1a/7737e7a0774da3c3824d654993cf57adc915cb04660212f03406334d8c0b/pprintpp-0.4.0.tar.gz",
"platform": "",
"description": "``pprint++``: a drop-in replacement for ``pprint`` that's actually pretty\n=========================================================================\n\n.. image:: https://travis-ci.org/wolever/pprintpp.svg?branch=master\n :target: https://travis-ci.org/wolever/pprintpp\n\nNow with Python 3 support!\n\nInstallation\n------------\n\n\n``pprint++`` can be installed with Python 2 or Python 3 using ``pip`` or\n``easy_install``::\n\n $ pip install pprintpp\n - OR -\n $ easy_install pprintpp\n\nUsage\n-----\n\n``pprint++`` can be used in three ways:\n\n1. Through the separate ``pp`` package::\n\n $ pip install pp-ez\n $ python\n ...\n >>> import pp\n >>> pp([\"Hello\", \"world\"])\n [\"Hello\", \"world\"]\n\n For more, see https://pypi.python.org/pypi/pp-ez\n\n2. As a command-line program, which will read Python literals from standard in\n and pretty-print them::\n\n $ echo \"{'hello': 'world'}\" | pypprint\n {'hello': 'world'}\n\n3. As an `ipython <https://github.com/ipython/ipython>`_ extension::\n\n In [1]: %load_ext pprintpp\n \n This will use pprintpp for ipython's output.\n \n To load this extension when ipython starts, put the previous line in your `startup file <https://ipython.org/ipython-doc/1/config/overview.html#startup-files>`_.\n \n You can change the indentation level like so::\n \n In [2]: %config PPrintPP.indentation = 4 \n\n4. To monkeypatch ``pprint``::\n\n >>> import pprintpp\n >>> pprintpp.monkeypatch()\n >>> import pprint\n >>> pprint.pprint(...)\n\n Note: the original ``pprint`` module will be available with ``import\n pprint_original``. Additionally, a warning will be issued if ``pprint`` has\n already been imported. This can be suppressed by passing ``quiet=True``.\n\n5. And, if you *really* want, it can even be imported as a regular module:\n\n >>> import pprintpp\n >>> pprintpp.pprint(...)\n\n\nUsability Protips\n-----------------\n\n``pp``\n~~~~~~\n\nFor bonus code aesthetics, ``pprintpp.pprint`` can be imported as ``pp``:\n\n.. code:: pycon\n\n >>> from pprintpp import pprint as pp\n >>> pp(...)\n\nAnd if that is just too many letters, the ``pp-ez`` package can be installed\nfrom PyPI, ensuring that pretty-printing is never more than an ``import pp``\naway::\n\n $ pip install pp-ez\n $ python\n ...\n >>> import pp\n >>> pp([\"Hello\", \"world\"])\n [\"Hello\", \"world\"]\n\nFor more, see https://pypi.python.org/pypi/pp-ez\n\n\nWhy is it prettier?\n-------------------\n\nUnlike ``pprint``, ``pprint++`` strives to emit a readable, largely\nPEP8-compliant, representation of its input.\n\nIt also has explicit support for: the ``collections`` module (``defaultdict``\nand ``Counter``) and ``numpy`` arrays:\n\n.. code:: pycon\n\n >>> import numpy as np\n >>> from collections import defaultdict, Counter\n >>> pprint([np.array([[1,2],[3,4]]), defaultdict(int, {\"foo\": 1}), Counter(\"aaabbc\")])\n [\n array([[1, 2],\n [3, 4]]),\n defaultdict(<type 'int'>, {'foo': 1}),\n Counter({'a': 3, 'b': 2, 'c': 1}),\n ]\n\nUnicode characters, when possible, will be printed un-escaped. This is done by\nchecking both the output stream's encoding (defaulting to ``utf-8``) and the\ncharacter's Unicode category. An effort is made to print only characters which\nwill be visually unambiguous: letters and numbers will be printed un-escaped,\nspaces, combining characters, and control characters will be escaped:\n\n.. code:: pycon\n\n >>> unistr = u\"\\xe9e\\u0301\"\n >>> print unistr\n \u00e9\u00e9\n >>> pprint(unistr)\n u'\u00e9e\\u0301'\n\nThe output stream's encoding will be considered too:\n\n.. code:: pycon\n\n >>> import io\n >>> stream = io.BytesIO()\n >>> stream.encoding = \"ascii\"\n >>> pprint(unistr, stream=stream)\n >>> print stream.getvalue()\n u'\\xe9e\\u0301'\n\nSubclassess of built-in collection types which don't define a new ``__repr__``\nwill have their class name explicitly added to their repr. For example:\n\n.. code:: pycon\n\n >>> class MyList(list):\n ... pass\n ...\n >>> pprint(MyList())\n MyList()\n >>> pprint(MyList([1, 2, 3]))\n MyList([1, 2, 3])\n\nNote that, as you might expect, custom ``__repr__`` methods will be respected:\n\n.. code:: pycon\n\n >>> class MyList(list):\n ... def __repr__(self):\n ... return \"custom repr!\"\n ...\n >>> pprint(MyList())\n custom repr!\n\n**Note**: ``pprint++`` is still under development, so the format *will* change\nand improve over time.\n\nExample\n~~~~~~~\n\nWith ``printpp``:\n\n.. code:: pycon\n\n >>> import pprintpp\n >>> pprintpp.pprint([\"Hello\", np.array([[1,2],[3,4]])])\n [\n 'Hello',\n array([[1, 2],\n [3, 4]]),\n ]\n >>> pprintpp.pprint(tweet)\n {\n 'coordinates': None,\n 'created_at': 'Mon Jun 27 19:32:19 +0000 2011',\n 'entities': {\n 'hashtags': [],\n 'urls': [\n {\n 'display_url': 'tumblr.com/xnr37hf0yz',\n 'expanded_url': 'http://tumblr.com/xnr37hf0yz',\n 'indices': [107, 126],\n 'url': 'http://t.co/cCIWIwg',\n },\n ],\n 'user_mentions': [],\n },\n 'place': None,\n 'source': '<a href=\"http://www.tumblr.com/\" rel=\"nofollow\">Tumblr</a>',\n 'truncated': False,\n 'user': {\n 'contributors_enabled': True,\n 'default_profile': False,\n 'entities': {'hashtags': [], 'urls': [], 'user_mentions': []},\n 'favourites_count': 20,\n 'id_str': '6253282',\n 'profile_link_color': '0094C2',\n },\n }\n\nWithout ``printpp``::\n\n >>> import pprint\n >>> import numpy as np\n >>> pprint.pprint([\"Hello\", np.array([[1,2],[3,4]])])\n ['Hello', array([[1, 2],\n [3, 4]])]\n >>> 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'}} \n >>> pprint.pprint(tweet)\n {'coordinates': None,\n 'created_at': 'Mon Jun 27 19:32:19 +0000 2011',\n 'entities': {'hashtags': [],\n 'urls': [{'display_url': 'tumblr.com/xnr37hf0yz',\n 'expanded_url': 'http://tumblr.com/xnr37hf0yz',\n 'indices': [107, 126],\n 'url': 'http://t.co/cCIWIwg'}],\n 'user_mentions': []},\n 'place': None,\n 'source': '<a href=\"http://www.tumblr.com/\" rel=\"nofollow\">Tumblr</a>',\n 'truncated': False,\n 'user': {'contributors_enabled': True,\n 'default_profile': False,\n 'entities': {'hashtags': [], 'urls': [], 'user_mentions': []},\n 'favourites_count': 20,\n 'id_str': '6253282',\n 'profile_link_color': '0094C2'}}\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "A drop-in replacement for pprint that's actually pretty",
"version": "0.4.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "6c96b17612b15f7bd78fdb8cc498a6f9",
"sha256": "b6b4dcdd0c0c0d75e4d7b2f21a9e933e5b2ce62b26e1a54537f9651ae5a5c01d"
},
"downloads": -1,
"filename": "pprintpp-0.4.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "6c96b17612b15f7bd78fdb8cc498a6f9",
"packagetype": "bdist_wheel",
"python_version": "2.7",
"requires_python": null,
"size": 16952,
"upload_time": "2018-07-01T01:42:36",
"upload_time_iso_8601": "2018-07-01T01:42:36.496104Z",
"url": "https://files.pythonhosted.org/packages/4e/d1/e4ed95fdd3ef13b78630280d9e9e240aeb65cc7c544ec57106149c3942fb/pprintpp-0.4.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "b5b6a310c80986e38eaab9b9a978b966",
"sha256": "ea826108e2c7f49dc6d66c752973c3fc9749142a798d6b254e1e301cfdbc6403"
},
"downloads": -1,
"filename": "pprintpp-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "b5b6a310c80986e38eaab9b9a978b966",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 17995,
"upload_time": "2018-07-01T01:42:34",
"upload_time_iso_8601": "2018-07-01T01:42:34.870060Z",
"url": "https://files.pythonhosted.org/packages/06/1a/7737e7a0774da3c3824d654993cf57adc915cb04660212f03406334d8c0b/pprintpp-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2018-07-01 01:42:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "wolever",
"github_project": "pprintpp",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "pprintpp"
}