summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fpprint.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fpprint.html
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fpprint.html')
-rw-r--r--devdocs/python~3.12/library%2Fpprint.html223
1 files changed, 223 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fpprint.html b/devdocs/python~3.12/library%2Fpprint.html
new file mode 100644
index 00000000..5d90f5d4
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fpprint.html
@@ -0,0 +1,223 @@
+ <span id="pprint-data-pretty-printer"></span><h1>pprint — Data pretty printer</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/pprint.py">Lib/pprint.py</a></p> <p>The <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code>pprint</code></a> 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.</p> <p>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 <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> objects explicitly if you need to adjust the width constraint.</p> <p>Dictionaries are sorted by key before the display is computed.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for pretty-printing <a class="reference internal" href="types#types.SimpleNamespace" title="types.SimpleNamespace"><code>types.SimpleNamespace</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Added support for pretty-printing <a class="reference internal" href="dataclasses#dataclasses.dataclass" title="dataclasses.dataclass"><code>dataclasses.dataclass</code></a>.</p> </div> <p>The <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code>pprint</code></a> module defines one class:</p> <span class="target" id="index-0"></span><dl class="py class"> <dt class="sig sig-object py" id="pprint.PrettyPrinter">
+<code>class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)</code> </dt> <dd>
+<p>Construct a <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> instance. This constructor understands several keyword parameters.</p> <p><em>stream</em> (default <code>sys.stdout</code>) is a <a class="reference internal" href="../glossary#term-file-like-object"><span class="xref std std-term">file-like object</span></a> to which the output will be written by calling its <code>write()</code> method. If both <em>stream</em> and <code>sys.stdout</code> are <code>None</code>, then <a class="reference internal" href="#pprint.PrettyPrinter.pprint" title="pprint.PrettyPrinter.pprint"><code>pprint()</code></a> silently returns.</p> <p>Other values configure the manner in which nesting of complex data structures is displayed.</p> <p><em>indent</em> (default 1) specifies the amount of indentation added for each nesting level.</p> <p><em>depth</em> controls the number of nesting levels which may be printed; if the data structure being printed is too deep, the next contained level is replaced by <code>...</code>. By default, there is no constraint on the depth of the objects being formatted.</p> <p><em>width</em> (default 80) specifies the desired maximum number of characters per line in the output. If a structure cannot be formatted within the width constraint, a best effort will be made.</p> <p><em>compact</em> impacts the way that long sequences (lists, tuples, sets, etc) are formatted. If <em>compact</em> is false (the default) then each item of a sequence will be formatted on a separate line. If <em>compact</em> is true, as many items as will fit within the <em>width</em> will be formatted on each output line.</p> <p>If <em>sort_dicts</em> is true (the default), dictionaries will be formatted with their keys sorted, otherwise they will display in insertion order.</p> <p>If <em>underscore_numbers</em> is true, integers will be formatted with the <code>_</code> character for a thousands separator, otherwise underscores are not displayed (the default).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>compact</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>sort_dicts</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>underscore_numbers</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>No longer attempts to write to <code>sys.stdout</code> if it is <code>None</code>.</p> <pre data-language="python">&gt;&gt;&gt; import pprint
+&gt;&gt;&gt; stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
+&gt;&gt;&gt; stuff.insert(0, stuff[:])
+&gt;&gt;&gt; pp = pprint.PrettyPrinter(indent=4)
+&gt;&gt;&gt; pp.pprint(stuff)
+[ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
+ 'spam',
+ 'eggs',
+ 'lumberjack',
+ 'knights',
+ 'ni']
+&gt;&gt;&gt; pp = pprint.PrettyPrinter(width=41, compact=True)
+&gt;&gt;&gt; pp.pprint(stuff)
+[['spam', 'eggs', 'lumberjack',
+ 'knights', 'ni'],
+ 'spam', 'eggs', 'lumberjack', 'knights',
+ 'ni']
+&gt;&gt;&gt; tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
+... ('parrot', ('fresh fruit',))))))))
+&gt;&gt;&gt; pp = pprint.PrettyPrinter(depth=6)
+&gt;&gt;&gt; pp.pprint(tup)
+('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
+</pre> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pprint.pformat">
+<code>pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)</code> </dt> <dd>
+<p>Return the formatted representation of <em>object</em> as a string. <em>indent</em>, <em>width</em>, <em>depth</em>, <em>compact</em>, <em>sort_dicts</em> and <em>underscore_numbers</em> are passed to the <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> constructor as formatting parameters and their meanings are as described in its documentation above.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pprint.pp">
+<code>pprint.pp(object, *args, sort_dicts=False, **kwargs)</code> </dt> <dd>
+<p>Prints the formatted representation of <em>object</em> followed by a newline. If <em>sort_dicts</em> is false (the default), dictionaries will be displayed with their keys in insertion order, otherwise the dict keys will be sorted. <em>args</em> and <em>kwargs</em> will be passed to <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code>pprint()</code></a> as formatting parameters.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pprint.pprint">
+<code>pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)</code> </dt> <dd>
+<p>Prints the formatted representation of <em>object</em> on <em>stream</em>, followed by a newline. If <em>stream</em> is <code>None</code>, <code>sys.stdout</code> is used. This may be used in the interactive interpreter instead of the <a class="reference internal" href="functions#print" title="print"><code>print()</code></a> function for inspecting values (you can even reassign <code>print = pprint.pprint</code> for use within a scope).</p> <p>The configuration parameters <em>stream</em>, <em>indent</em>, <em>width</em>, <em>depth</em>, <em>compact</em>, <em>sort_dicts</em> and <em>underscore_numbers</em> are passed to the <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> constructor and their meanings are as described in its documentation above.</p> <pre data-language="python">&gt;&gt;&gt; import pprint
+&gt;&gt;&gt; stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
+&gt;&gt;&gt; stuff.insert(0, stuff)
+&gt;&gt;&gt; pprint.pprint(stuff)
+[&lt;Recursion on list with id=...&gt;,
+ 'spam',
+ 'eggs',
+ 'lumberjack',
+ 'knights',
+ 'ni']
+</pre> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pprint.isreadable">
+<code>pprint.isreadable(object)</code> </dt> <dd>
+<p id="index-1">Determine if the formatted representation of <em>object</em> is “readable”, or can be used to reconstruct the value using <a class="reference internal" href="functions#eval" title="eval"><code>eval()</code></a>. This always returns <code>False</code> for recursive objects.</p> <pre data-language="python">&gt;&gt;&gt; pprint.isreadable(stuff)
+False
+</pre> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="pprint.isrecursive">
+<code>pprint.isrecursive(object)</code> </dt> <dd>
+<p>Determine if <em>object</em> requires a recursive representation. This function is subject to the same limitations as noted in <a class="reference internal" href="#pprint.saferepr" title="pprint.saferepr"><code>saferepr()</code></a> below and may raise an <a class="reference internal" href="exceptions#RecursionError" title="RecursionError"><code>RecursionError</code></a> if it fails to detect a recursive object.</p> </dd>
+</dl> <p>One more support function is also defined:</p> <dl class="py function"> <dt class="sig sig-object py" id="pprint.saferepr">
+<code>pprint.saferepr(object)</code> </dt> <dd>
+<p>Return a string representation of <em>object</em>, protected against recursion in some common data structures, namely instances of <a class="reference internal" href="stdtypes#dict" title="dict"><code>dict</code></a>, <a class="reference internal" href="stdtypes#list" title="list"><code>list</code></a> and <a class="reference internal" href="stdtypes#tuple" title="tuple"><code>tuple</code></a> or subclasses whose <code>__repr__</code> has not been overridden. If the representation of object exposes a recursive entry, the recursive reference will be represented as <code>&lt;Recursion on typename with id=number&gt;</code>. The representation is not otherwise formatted.</p> <pre data-language="python">&gt;&gt;&gt; pprint.saferepr(stuff)
+"[&lt;Recursion on list with id=...&gt;, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
+</pre> </dd>
+</dl> <section id="prettyprinter-objects"> <span id="id1"></span><h2>PrettyPrinter Objects</h2> <p><a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> instances have the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="pprint.PrettyPrinter.pformat">
+<code>PrettyPrinter.pformat(object)</code> </dt> <dd>
+<p>Return the formatted representation of <em>object</em>. This takes into account the options passed to the <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> constructor.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="pprint.PrettyPrinter.pprint">
+<code>PrettyPrinter.pprint(object)</code> </dt> <dd>
+<p>Print the formatted representation of <em>object</em> on the configured stream, followed by a newline.</p> </dd>
+</dl> <p>The following methods provide the implementations for the corresponding functions of the same names. Using these methods on an instance is slightly more efficient since new <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> objects don’t need to be created.</p> <dl class="py method"> <dt class="sig sig-object py" id="pprint.PrettyPrinter.isreadable">
+<code>PrettyPrinter.isreadable(object)</code> </dt> <dd>
+<p id="index-2">Determine if the formatted representation of the object is “readable,” or can be used to reconstruct the value using <a class="reference internal" href="functions#eval" title="eval"><code>eval()</code></a>. Note that this returns <code>False</code> for recursive objects. If the <em>depth</em> parameter of the <a class="reference internal" href="#pprint.PrettyPrinter" title="pprint.PrettyPrinter"><code>PrettyPrinter</code></a> is set and the object is deeper than allowed, this returns <code>False</code>.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="pprint.PrettyPrinter.isrecursive">
+<code>PrettyPrinter.isrecursive(object)</code> </dt> <dd>
+<p>Determine if the object requires a recursive representation.</p> </dd>
+</dl> <p>This method is provided as a hook to allow subclasses to modify the way objects are converted to strings. The default implementation uses the internals of the <a class="reference internal" href="#pprint.saferepr" title="pprint.saferepr"><code>saferepr()</code></a> implementation.</p> <dl class="py method"> <dt class="sig sig-object py" id="pprint.PrettyPrinter.format">
+<code>PrettyPrinter.format(object, context, maxlevels, level)</code> </dt> <dd>
+<p>Returns three values: the formatted version of <em>object</em> as a string, a flag indicating whether the result is readable, and a flag indicating whether recursion was detected. The first argument is the object to be presented. The second is a dictionary which contains the <a class="reference internal" href="functions#id" title="id"><code>id()</code></a> of objects that are part of the current presentation context (direct and indirect containers for <em>object</em> that are affecting the presentation) as the keys; if an object needs to be presented which is already represented in <em>context</em>, the third return value should be <code>True</code>. Recursive calls to the <a class="reference internal" href="#pprint.PrettyPrinter.format" title="pprint.PrettyPrinter.format"><code>format()</code></a> method should add additional entries for containers to this dictionary. The third argument, <em>maxlevels</em>, gives the requested limit to recursion; this will be <code>0</code> if there is no requested limit. This argument should be passed unmodified to recursive calls. The fourth argument, <em>level</em>, gives the current level; recursive calls should be passed a value less than that of the current call.</p> </dd>
+</dl> </section> <section id="example"> <span id="pprint-example"></span><h2>Example</h2> <p>To demonstrate several uses of the <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code>pprint()</code></a> function and its parameters, let’s fetch information about a project from <a class="reference external" href="https://pypi.org">PyPI</a>:</p> <pre data-language="python">&gt;&gt;&gt; import json
+&gt;&gt;&gt; import pprint
+&gt;&gt;&gt; from urllib.request import urlopen
+&gt;&gt;&gt; with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
+... project_info = json.load(resp)['info']
+</pre> <p>In its basic form, <a class="reference internal" href="#module-pprint" title="pprint: Data pretty printer."><code>pprint()</code></a> shows the whole object:</p> <pre data-language="python">&gt;&gt;&gt; pprint.pprint(project_info)
+{'author': 'The Python Packaging Authority',
+ 'author_email': 'pypa-dev@googlegroups.com',
+ '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'}
+</pre> <p>The result can be limited to a certain <em>depth</em> (ellipsis is used for deeper contents):</p> <pre data-language="python">&gt;&gt;&gt; pprint.pprint(project_info, depth=1)
+{'author': 'The Python Packaging Authority',
+ 'author_email': 'pypa-dev@googlegroups.com',
+ 'bugtrack_url': None,
+ 'classifiers': [...],
+ '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': {...},
+ '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': {...},
+ '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'}
+</pre> <p>Additionally, maximum character <em>width</em> can be suggested. If a long object cannot be split, the specified width will be exceeded:</p> <pre data-language="python">&gt;&gt;&gt; pprint.pprint(project_info, depth=1, width=60)
+{'author': 'The Python Packaging Authority',
+ 'author_email': 'pypa-dev@googlegroups.com',
+ 'bugtrack_url': None,
+ 'classifiers': [...],
+ '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': {...},
+ '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': {...},
+ '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'}
+</pre> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/pprint.html" class="_attribution-link">https://docs.python.org/3.12/library/pprint.html</a>
+ </p>
+</div>