Skip to main content

pretty-py3

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.

This was converted to python3 by Mark Grandi and others

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

Here the example implementation of a __pretty__ method for a list subclass::

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. The second and the third parameter are the opening and closing strings that will be printed before and after the group. 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.

Changelog

0.2.4

0.2.3

0.2.2

  • Finish porting it to python3

copyright 2007 by Armin Ronacher.

copyright 2014 by Mark Grandi - python 3 port

license BSD License.

Metadata

Release files for pretty-py3 0.2.4.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pretty-py3 0.2.4.post1
File Size Uploaded
pretty-py3-0.2.4.post1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pretty-py3 0.2.4.post1
File Interpreter ABI Platform
pretty_py3-0.2.4.post1-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / pretty-py3-0.2.4.post1.tar.gz

Download URL pretty-py3-0.2.4.post1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9589d46fe842724f44195f277ecb283c1efa536ebaa80e62b528e722402435dc
BLAKE2b-256 checksum
How to use checksums
dc0465e5bdbca3ffaa3987e8493d68126a2e3d333c2e468fc8c8702a56e835fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.2

Release files / pretty_py3-0.2.4.post1-py3-none-any.whl

Download URL pretty_py3-0.2.4.post1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f171ad7710fc678540297951d7dd4325682de2d1e8f69167ad241548f9a7111f
BLAKE2b-256 checksum
How to use checksums
05552e64673fdb825988ce5f03ecb77667c57937cce94349141ca098c38725e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.2.4.post1 This release

2 release files

0.2.4

2 release files

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page