IPython 8.4.0 Pypi GitHub Homepage
Other Docs
BackRef

To remove in the future –– IPython.lib.pretty

Python advanced pretty printer. This pretty printer is intended to replace the old pprint python module which does not allow developers to provide their own pretty print callbacks.

This module is based on ruby's :None:None:`prettyprint.rb` library by :None:None:`Tanaka Akira`.

Example Usage

To directly print the representation of an object use pprint :

from pretty import pprint
pprint(complex_object)

To get a string of the output use pretty :

from pretty import pretty
string = pretty(complex_object)

Extending

The pretty library allows developers to add pretty printing rules for their own objects. This process is straightforward. All you have to do is to add a :None:None:`_repr_pretty_` method to your object and call the methods on the pretty printer passed:

class MyObject(object):

    def _repr_pretty_(self, p, cycle):
        ...

Here's an example for a class with a simple constructor:

class MySimpleObject:

    def __init__(self, a, b, *, c=None):
        self.a = a
        self.b = b
        self.c = c

    def _repr_pretty_(self, p, cycle):
        ctor = CallExpression.factory(self.__class__.__name__)
        if self.c is None:
            p.pretty(ctor(a, b))
        else:
            p.pretty(ctor(a, b, c=c))

Here is an example implementation of a :None:None:`_repr_pretty_` method for a list subclass:

class MyList(list):

    def _repr_pretty_(self, p, cycle):
        if cycle:
            p.text('MyList(...)')
        else:
            with p.group(8, 'MyList([', '])'):
                for idx, item in enumerate(self):
                    if idx:
                        p.text(',')
                        p.breakable()
                    p.pretty(item)

The :None:None:`cycle` parameter is :None:None:`True` if pretty detected a cycle. You have to react to that or the result is an infinite loop. :None:None:`p.text()` just adds non breaking text to the output, :None:None:`p.breakable()` either adds a whitespace or breaks here. If you pass it an argument it's used instead of the default space. :None:None:`p.pretty` prettyprints another object using the pretty print method.

The first parameter to the group function specifies the extra indentation of the next line. In this example the next item will either be on the same line (if the items are short enough) or aligned with the right edge of the opening bracket of :None:None:`MyList`.

If you just want to indent something you can use the group function without open / close parameters. You can also use this code:

with p.indent(2):
    ...

Inheritance diagram:

.. inheritance-diagram:: IPython.lib.pretty
    ('parts', '3')
    
copyright

2007 by Armin Ronacher. Portions (c) 2009 by Robert Kern.

license

BSD License.

Examples

See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

IPython.lib.pretty.RepresentationPrinter IPython.lib.pretty IPython.lib.pretty.pprint

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /IPython/lib/pretty.py#0
type: <class 'module'>
Commit: