[Python] Use pprint module to print arbitrary Python data structures pretty

pprint

The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects such as files, sockets or classes are included, as well as many other objects which are not representable as Python literals.

The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width. Construct PrettyPrinter objects explicitly if you need to adjust the width constraint.

Dictionaries are sorted by key before the display is computed.

Usages

PrettyPrinter

The pprint module defines one class:

class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True)

Construct a PrettyPrinter instance. This constructor understands several keyword parameters.

  • An output stream may be set using the stream keyword; the only method used on the stream object is the file protocol’s write() method. If not specified, the PrettyPrinter adopts sys.stdout.

  • The amount of indentation added for each recursive level is specified by indent; the default is one. Other values can cause output to look a little odd, but can make nesting easier to spot. The number of levels which may be printed is controlled by depth; if the data structure being printed is too deep, the next contained level is replaced by … By default, there is no constraint on the depth of the objects being formatted.

  • The desired output width is constrained using the width parameter; the default is 80 characters. If a structure cannot be formatted within the constrained width, a best effort will be made.

  • If compact is false (the default) each item of a long sequence will be formatted on a separate line. If compact is true, as many items as will fit within the width will be formatted on each output line.

  • If sort_dicts is true (the default), dictionaries will be formatted with their keys sorted, otherwise they will display in insertion order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import pprint

stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)
# [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
# 'spam',
# 'eggs',
# 'lumberjack',
# 'knights',
# 'ni']

pp = pprint.PrettyPrinter(width=41, compact=True)
pp.pprint(stuff)
# [['spam', 'eggs', 'lumberjack',
# 'knights', 'ni'],
# 'spam', 'eggs', 'lumberjack', 'knights',
# 'ni']

tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
('parrot', ('fresh fruit',))))))))
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(tup)
# ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))

Shortcut Functions

The pprint module also provides several shortcut functions:

pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True)

Return the formatted representation of object as a string. indent, width, depth, compact and sort_dicts will be passed to the PrettyPrinter constructor as formatting parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
```

#### pprint.pp(object, *args, sort_dicts=False, **kwargs)

Prints the formatted representation of object followed by a newline. If sort_dicts is false (the default), dictionaries will be displayed with their keys in insertion order, otherwise the dict keys will be sorted. args and kwargs will be passed to pprint() as formatting parameters.

#### pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True)

Prints the formatted representation of object on stream, followed by a newline. If stream is None, sys.stdout is used. This may be used in the interactive interpreter instead of the print() function for inspecting values (you can even reassign print = pprint.pprint for use within a scope). indent, width, depth, compact and sort_dicts will be passed to the PrettyPrinter constructor as formatting parameters.

```python
import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff)
pprint.pprint(stuff)
# [<Recursion on list with id=...>,
# 'spam',
# 'eggs',
# 'lumberjack',
# 'knights',
# 'ni']

pprint.isreadable(object)

Determine if the formatted representation of object is “readable”, or can be used to reconstruct the value using eval(). This always returns False for recursive objects.

1
2
pprint.isreadable(stuff)
# False

pprint.isrecursive(object)

Determine if object requires a recursive representation.

pprint.saferepr(object)

Return a string representation of object, protected against recursive data structures. If the representation of object exposes a recursive entry, the recursive reference will be represented as <Recursion on typename with id=number>. The representation is not otherwise formatted.

1
2
pprint.saferepr(stuff)
# "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"

Example

To demonstrate several uses of the pprint() function and its parameters, let’s fetch information about a project from PyPI:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
import pprint
from urllib.request import urlopen
with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
project_info = json.load(resp)['info']

pprint.pprint(project_info)
# {'author': 'The Python Packaging Authority',
# 'author_email': '[email protected]',
# 'bugtrack_url': None,
# 'classifiers': ['Development Status :: 3 - Alpha',
# 'Intended Audience :: Developers',
# 'License :: OSI Approved :: MIT License',
# 'Programming Language :: Python :: 2',
# 'Programming Language :: Python :: 2.6',
# 'Programming Language :: Python :: 2.7',
# 'Programming Language :: Python :: 3',
# 'Programming Language :: Python :: 3.2',
# 'Programming Language :: Python :: 3.3',
# 'Programming Language :: Python :: 3.4',
# 'Topic :: Software Development :: Build Tools'],
# 'description': 'A sample Python project\n'
# '=======================\n'
# '\n'
# 'This is the description file for the project.\n'
# '\n'
# 'The file should use UTF-8 encoding and be written using '
# 'ReStructured Text. It\n'
# 'will be used to generate the project webpage on PyPI, and '
# 'should be written for\n'
# 'that purpose.\n'
# '\n'
# 'Typical contents for this file would include an overview of '
# 'the project, basic\n'
# 'usage examples, etc. Generally, including the project '
# 'changelog in here is not\n'
# 'a good idea, although a simple "What\'s New" section for the '
# 'most recent version\n'
# 'may be appropriate.',
# 'description_content_type': None,
# 'docs_url': None,
# 'download_url': 'UNKNOWN',
# 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
# 'home_page': 'https://github.com/pypa/sampleproject',
# 'keywords': 'sample setuptools development',
# 'license': 'MIT',
# 'maintainer': None,
# 'maintainer_email': None,
# 'name': 'sampleproject',
# 'package_url': 'https://pypi.org/project/sampleproject/',
# 'platform': 'UNKNOWN',
# 'project_url': 'https://pypi.org/project/sampleproject/',
# 'project_urls': {'Download': 'UNKNOWN',
# 'Homepage': 'https://github.com/pypa/sampleproject'},
# 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
# 'requires_dist': None,
# 'requires_python': None,
# 'summary': 'A sample Python project',
# 'version': '1.2.0'}

pprint.pp(f'project_info = {pprint.pformat(project_info)}')
# project_info = {'author': 'The Python Packaging Authority',
# 'author_email': '[email protected]',
# 'bugtrack_url': None,
# 'classifiers': ['Development Status :: 3 - Alpha',
# 'Intended Audience :: Developers',
# 'License :: OSI Approved :: MIT License',
# 'Programming Language :: Python :: 2',
# 'Programming Language :: Python :: 2.6',
# 'Programming Language :: Python :: 2.7',
# 'Programming Language :: Python :: 3',
# 'Programming Language :: Python :: 3.2',
# 'Programming Language :: Python :: 3.3',
# 'Programming Language :: Python :: 3.4',
# 'Topic :: Software Development :: Build Tools'],
# 'description': 'A sample Python project\n'
# '=======================\n'
# '\n'
# 'This is the description file for the project.\n'
# '\n'
# 'The file should use UTF-8 encoding and be written using '
# 'ReStructured Text. It\n'
# 'will be used to generate the project webpage on PyPI, and '
# 'should be written for\n'
# 'that purpose.\n'
# '\n'
# 'Typical contents for this file would include an overview of '
# 'the project, basic\n'
# 'usage examples, etc. Generally, including the project '
# 'changelog in here is not\n'
# 'a good idea, although a simple "What\'s New" section for the '
# 'most recent version\n'
# 'may be appropriate.',
# 'description_content_type': None,
# 'docs_url': None,
# 'download_url': 'UNKNOWN',
# 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
# 'home_page': 'https://github.com/pypa/sampleproject',
# 'keywords': 'sample setuptools development',
# 'license': 'MIT',
# 'maintainer': None,
# 'maintainer_email': None,
# 'name': 'sampleproject',
# 'package_url': 'https://pypi.org/project/sampleproject/',
# 'platform': 'UNKNOWN',
# 'project_url': 'https://pypi.org/project/sampleproject/',
# 'project_urls': {'Download': 'UNKNOWN',
# 'Homepage': 'https://github.com/pypa/sampleproject'},
# 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
# 'requires_dist': None,
# 'requires_python': None,
# 'summary': 'A sample Python project',
# 'version': '1.2.0'}

References

[1] pprint — Data pretty printer — Python 3.9.5 documentation - https://docs.python.org/3/library/pprint.html