"pprint" — L’affichage élégant de données
*****************************************

**Code source :** Lib/pprint.py

======================================================================

Le module "pprint" permet « d’afficher élégamment » des structures de
données Python arbitraires sous une forme qui peut être utilisée
ensuite comme une entrée dans l’interpréteur. Si les structures
formatées incluent des objets qui ne sont pas des types Python
fondamentaux, leurs représentations peuvent ne pas être acceptables en
tant que telles par l’interpréteur. Cela peut être le cas si des
objets tels que des fichiers, des interfaces de connexion (*sockets*
en anglais) ou des classes sont inclus, c’est aussi valable pour
beaucoup d’autres types d’objets qui ne peuvent être représentés sous
forme littérale en Python.

L’affichage formaté affiche, tant que possible, les objets sur une
seule ligne et les sépare sur plusieurs lignes s’ils dépassent la
largeur autorisée par l’interpréteur. Créez explicitement des objets
"PrettyPrinter" si vous avez besoin de modifier les limites de
largeur.

Les dictionnaires sont classés par clés avant que l’affichage ne soit
calculé.

Modifié dans la version 3.9: Added support for pretty-printing
"types.SimpleNamespace".

Modifié dans la version 3.10: Added support for pretty-printing
"dataclasses.dataclass".


Functions
=========

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.

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

   Nouveau dans la version 3.8.

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

   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).

   The configuration parameters *stream*, *indent*, *width*, *depth*,
   *compact*, *sort_dicts* and *underscore_numbers* are passed to the
   "PrettyPrinter" constructor and their meanings are as described in
   its documentation below.

   Note that *sort_dicts* is "True" by default and you might want to
   use "pp()" instead where it is "False" by default.

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

   Return the formatted representation of *object* as a string.
   *indent*, *width*, *depth*, *compact*, *sort_dicts* and
   *underscore_numbers* are passed to the "PrettyPrinter" constructor
   as formatting parameters and their meanings are as described in its
   documentation below.

pprint.isreadable(object)

   Détermine si la représentation formatée de *object* est « lisible
   », ou s’il peut être utilisé pour recomposer sa valeur en utilisant
   la fonction "eval()". Cela renvoie toujours "False" pour les objets
   récursifs.

   >>> pprint.isreadable(stuff)
   False

pprint.isrecursive(object)

   Détermine si *object* requiert une représentation récursive.

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.

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


Les Objets PrettyPrinter
========================

This module defines one class:

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

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

   *stream* (default "sys.stdout") is a *file-like object* to which
   the output will be written by calling its "write()" method. If both
   *stream* and "sys.stdout" are "None", then "pprint()" silently
   returns.

   Other values configure the manner in which nesting of complex data
   structures is displayed.

   *indent* (default 1) specifies the amount of indentation added for
   each nesting level.

   *depth* 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 "...".  By default, there is no constraint on
   the depth of the objects being formatted.

   *width* (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.

   *compact* impacts the way that long sequences (lists, tuples, sets,
   etc) are formatted. If *compact* is false (the default) then each
   item of a 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.

   If *underscore_numbers* is true, integers will be formatted with
   the "_" character for a thousands separator, otherwise underscores
   are not displayed (the default).

   Modifié dans la version 3.4: Ajout du paramètre *compact*.

   Modifié dans la version 3.8: Ajout du paramètre *sort_dicts*.

   Modifié dans la version 3.10: Added the *underscore_numbers*
   parameter.

   Modifié dans la version 3.11: No longer attempts to write to
   "sys.stdout" if it is "None".

   >>> 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', (...)))))))

Les instances de la classe "PrettyPrinter" ont les méthodes suivantes
:

PrettyPrinter.pformat(object)

   Renvoie la représentation formatée de *object*. Cela prend en
   compte les options passées au constructeur de la classe
   "PrettyPrinter".

PrettyPrinter.pprint(object)

   Affiche sur le flux configuré la représentation formatée de
   *object*, suivie d’une fin de ligne.

Les méthodes suivantes fournissent les implémentations pour les
fonctions correspondantes de mêmes noms. L’utilisation de ces méthodes
sur une instance est légèrement plus efficace, car les nouveaux objets
"PrettyPrinter" n’ont pas besoin d’être créés.

PrettyPrinter.isreadable(object)

   Détermine si la représentation formatée de *object* est « lisible
   », ou si elle peut être utilisée pour recomposer sa valeur en
   utilisant la fonction "eval()". Cela renvoie toujours "False" pour
   les objets récursifs. Si le paramètre *depth* de la classe
   "PrettyPrinter" est initialisé et que l’objet est plus « profond »
   que permis, cela renvoie "False".

PrettyPrinter.isrecursive(object)

   Détermine si l’objet nécessite une représentation récursive.

Cette méthode est fournie sous forme de point d’entrée ou méthode (à
déclenchement) automatique (*hook* en anglais) pour permettre aux
sous-classes de modifier la façon dont les objets sont convertis en
chaînes. L’implémentation par défaut est celle de la fonction
"saferepr()".

PrettyPrinter.format(object, context, maxlevels, level)

   Renvoie trois valeurs : la version formatée de *object* sous forme
   de chaîne de caractères, une option indiquant si le résultat est «
   lisible », et une option indiquant si une récursion a été détectée.
   Le premier argument est l’objet à représenter. Le deuxième est un
   dictionnaire qui contient l'"id()" des objets (conteneurs directs
   ou indirects de *objet* qui affectent sa représentation) qui font
   partie du contexte de représentation courant tel que les clés; si
   un objet doit être représenté, mais l’a déjà été dans ce contexte,
   le troisième argument renvoie "True". Des appels récursifs à la
   méthode "format()" doivent ajouter des entrés additionnelles aux
   conteneurs de ce dictionnaire. Le troisième argument *maxlevels*,
   donne la limite maximale de récursivité; la valeur par défaut est
   "0". Cet argument doit être passé non modifié pour des appels non
   récursifs. Le quatrième argument, *level*, donne le niveau de
   récursivité courant; les appels récursifs doivent être passés à une
   valeur inférieure à celle de l’appel courant.


Exemple
=======

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

   >>> 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']

In its basic form, "pp()" shows the whole object:

   >>> pprint.pp(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'}

Le résultat peut être limité à une certaine profondeur en initialisant
*depth*. ( "…" est utilisé pour des contenus plus « profonds ») :

   >>> pprint.pp(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'}

De plus, une valeur maximale de caractères sur une ligne peut être
définie en initialisant le paramètre *width*. Si un long objet ne peut
être scindé, la valeur donnée à *width* sera outrepassée :

   >>> pprint.pp(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'}
