### Navigation

- index
- modules |
- next |
- previous |
- IPython v0.10 documentation »
- The IPython API »

# external.pretty

## Module: `external.pretty`

Inheritance diagram for `IPython.external.pretty`:

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 prettyprint.rb library by 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 __pretty__ method to your object and call the methods on the pretty printer passed:

    class MyObject(object):

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

Depending on the python version you want to support you have two possibilities. The following list shows the python 2.5 version and the compatibility one.

Here the example implementation of a __pretty__ method for a list subclass for python 2.5 and higher (python 2.5 requires the with statement __future__ import):

    class MyList(list):

        def __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 cycle parameter is True if pretty detected a cycle. You have to react to that or the result is an infinite loop. p.text() just adds non breaking text to the output, p.breakable() either adds a whitespace or breaks here. If you pass it an argument it’s used instead of the default space. 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 not breaked (if the items are short enough) or aligned with the right edge of the opening bracked of MyList.

If you want to support python 2.4 and lower you can use this code:

    class MyList(list):

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

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

    with p.indent(2):
        ...

Or under python2.4 you might want to modify `p.indentation` by hand but this is rather ugly.

copyright: | 2007 by Armin Ronacher. Portions (c) 2009 by Robert Kern.
license: | BSD License.

## Classes

### `Breakable`

class `IPython.external.pretty.``Breakable`(seq, width, pretty)

  Bases: `IPython.external.pretty.Printable`

  `__init__`(seq, width, pretty)
  `output`(stream, output_width)

### `Group`

class `IPython.external.pretty.``Group`(depth)

  Bases: `IPython.external.pretty.Printable`

  `__init__`(depth)

### `GroupQueue`

class `IPython.external.pretty.``GroupQueue`(*groups)

  Bases: `object`

  `__init__`(*groups)
  `deq`()
  `enq`(group)
  `remove`(group)

### `PrettyPrinter`

class `IPython.external.pretty.``PrettyPrinter`(output, max_width=79, newline='n')

  Bases: `IPython.external.pretty._PrettyPrinterBase`

  Baseclass for the RepresentationPrinter prettyprinter that is used to generate pretty reprs of objects. Contrary to the RepresentationPrinter this printer knows nothing about the default pprinters or the __pretty__ callback method.

  `__init__`(output, max_width=79, newline='n')
  `begin_group`(indent=0, open='')

    Begin a group. If you want support for python < 2.5 which doesn’t has the with statement this is the preferred way:

    > p.begin_group(1, ‘{‘) ... p.end_group(1, ‘}’)

    The python 2.5 expression would be this:

    > with p.group(1, ‘{‘, ‘}’):
    >   ...

    The first parameter specifies the indentation for the next line (usually the width of the opening text), the second the opening text. All parameters are optional.

  `breakable`(sep=' ')
    Add a breakable separator to the output. This does not mean that it will automatically break here. If no breaking on this position takes place the sep is inserted which default to one space.
  `end_group`(dedent=0, close='')
    End a group. See begin_group for more details.
  `flush`()
    Flush data that is left in the buffer.
  `text`(obj)
    Add literal text to the output.

### `Printable`

class `IPython.external.pretty.``Printable`

  Bases: `object`

  `__init__`()
    x.__init__(...) initializes x; see x.__class__.__doc__ for signature
  `output`(stream, output_width)

### `RepresentationPrinter`

class `IPython.external.pretty.``RepresentationPrinter`(output, verbose=False, max_width=79, newline='n')

  Bases: `IPython.external.pretty.PrettyPrinter`

  Special pretty printer that has a pretty method that calls the pretty printer for a python object.

  This class stores processing data on self so you must never use this class in a threaded environment. Always lock it or reinstanciate it.

  Instances also have a verbose flag callbacks can access to control their output. For example the default instance repr prints all attributes and methods that are not prefixed by an underscore if the printer is in verbose mode.

  `__init__`(output, verbose=False, max_width=79, newline='n')
  `pretty`(obj)
    Pretty print the given object.

### `Text`

class `IPython.external.pretty.``Text`

  Bases: `IPython.external.pretty.Printable`

  `__init__`()
  `add`(obj, width)
  `output`(stream, output_width)

## Functions

`IPython.external.pretty.``for_type`(typ, func)
  Add a pretty printer for a given type.

`IPython.external.pretty.``for_type_by_name`(type_module, type_name, func)
  Add a pretty printer for a type specified by the module and name of a type rather than the type object itself.

`IPython.external.pretty.``pprint`(obj, verbose=False, max_width=79, newline='n')
  Like pretty but print to stdout.

`IPython.external.pretty.``pretty`(obj, verbose=False, max_width=79, newline='n')
  Pretty print the object’s representation.

### Navigation

- index
- modules |
- next |
- previous |
- IPython v0.10 documentation »
- The IPython API »