Git Product home page Git Product logo

pretty-py3's Introduction

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

  • Merged pull request #2 (#2) from @alexshpilkin, "Fix double indentation in PrettyPrinter.group()"
  • Merged pull request #3 (#3) from @alexshpilkin, "Better support for set and OrderedDict types"

0.2.3

  • Merged pull request #1 (#1) from @avoidscorn, "Add missing comma in single-element tuple."

0.2.2

  • Finish porting it to python3

Copyright

copyright 2007 by Armin Ronacher.

copyright 2014 by Mark Grandi - python 3 port

license BSD License.

pretty-py3's People

Contributors

alexshpilkin avatar mgrandi avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

pretty-py3's Issues

Broken hanging indents

Thank you for handling my previous fixes! This one is not going to be so easy, I’m afraid.

In short:

>>> from pretty import pprint
>>> pprint({'not-accounted-for': ['a'*20, 'b'*20]}, max_width=20)
{'not-accounted-for': ['aaaaaaaaaaaaaaaaaaaa',
  'bbbbbbbbbbbbbbbbbbbb']}

The problem is, I don’t see any code inside the prettyprinter that would account for the width of the dict key, or in fact any code that would track the current column at all, and the indent argument to begin_group is insufficient to handle this case (for extra complexity, remember that the key could also be printed on several lines).

Maybe this code was unreleased for a reason...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.