Python | Use Prettyprinter to Make Python Output More Beautiful, You Deserve It

Python | Use Prettyprinter to Make Python Output More Beautiful, You Deserve It

PrettyPrinter is a powerful, syntax highlighting, and descriptive beautification printing package for Python 3.6 and above. It uses an improved Wadler-Leijen layout algorithm, similar to prettyprinter and anti - wl - pprint in Haskell's print beautification library , JavaScript's Prettier, Ruby's pretty-printer.rb , and IPython 's Ipython.lib.pretty .

PrettyPrinter is a powerful, syntax highlighting, and descriptive beautification printing package for Python 3.6 and above. It uses an improved Wadler-Leijen layout algorithm, similar to prettyprinter and anti - wl - pprint in Haskell's print beautification library , JavaScript's Prettier, Ruby's prettypreinter.rb , and IPython 's Ipython.lib.pretty . Python's PrettyPrinter combines the strengths of the above, and continues to improve on this basis, so it has become the most powerful beautification output tool in Python.
The following is a screenshot of the output using

PrettyPrinter : Why does Python need an additional beautify-printing package?
Whether it is an IDE or a developer manually running commands, printing data to the screen is the most basic interface for programmers to interact with numerical values during program execution. Improving the interface can help improve the development experience and productivity. Both Python itself and third-party libraries provide some tools for this purpose:
__repr__ and __str__ return normal strings. __ repr __ should return a Python expression with correct syntax as much as possible. This method is most commonly used for assertion failure and console calculation result printing. Because it is completely based on string formatting, it does not have the function of beautifying printing.

pprint module in the standard library provides pretty print functionality for built-in data types such as dicts , lists, tuples, sets, and frozensets . It applies the __repr__ method to user-defined class instances. However, it uses a very greedy layout algorithm, which leads to problems with beautified printing in many cases. Since custom beautification printing is limited by __ repr __, the role of pprint is limited to built-in data types.
The third-party library pprintpp is an improvement and an alternative to pprint , and it can also optimize the output, but it is limited to the code beautification definition used by __ repr __ like pprint .
The default printing module in IPython , IPython.lib.pretty , aims to be a more advanced alternative to pprint . Compared with pprint , it performs better in many ways: the algorithm can beautify the output in most cases, and it provides a definition tool for beautifying the output for user-defined types, which can be implemented well with other parts of the output combination. However, in order to implement your own beautifying print, you need to know a bit about layout algorithms.
In addition, this API also has some inherent side effects: calling the beautification printing tool to push the data directly to the layout buffer, which does not allow the original layout to perform preliminary detection of the data.
All of the above tools fell short of what I wanted to beautify the printing experience, so I started making the following improvements:
Implement an algorithm that can beautify the print as much as possible, even at the expense of efficiency. Spending a tenth of a second beautifying the output is very cost-effective because it saves you two seconds when you need to find the data you need in the results.
Implement a super simple, descriptive interface to implement user-defined beautification printing tools. Python members rarely override the __repr__ method because it's a pain; hardly anyone wants to write neat printing rules for user-defined types unless the type is very simple.
Implement syntax highlighting that does not break on invalid Python syntax. Not all __repr__ methods will return valid syntax, and normal syntax highlighting will be interrupted in the event of a syntax error.

Prettyprinter.The experience of using the new code beautification package surprised me. The algorithm works very well and the efficiency meets the requirements. The method of user-defined beautification rules is also very simple, just need to understand two descriptive functions register_pretty and pretty_call . Syntax highlighting looks beautiful and doesn't break with invalid syntax. In particular, syntax highlighting will make it difficult for you to return to ordinary beautifying printing tools, which greatly improves the development experience of programmers.
The most interesting improvement is the descriptive API, here's how it works.
Simple, descriptive API
Defining output beautification methods in PrettyPrinter is mainly based on (create) function calls. All non-character Python values need to be represented by the function result. The main function of the library is pretty_call , which allows you to describe what type of function calls PrettyPrinter should output. Here is an example of a pretty_call call:
from prettyprinter import pretty_call

# ctx is available in pretty printer definitions
layout_primitive = pretty_call ( ctx , sorted, [ 5 , 3 , 6 , 1 ], reverse= True )
PrettyPrinter handles raw layouts similar to the following statement:
sorted( [ 5 , 3 , 6 , 1 ], reverse= True )
(The first parameter ctx allows the user to control the nested data in the [5,3,6,1] list in the case, and the True value of the reverse parameter is rendered according to this. In most cases, the default value can be used directly.)
above describes how to use Pretty_call , let's define our own type.
class MyClass :
def __init _ _ ( self, one, two) : = one
self.two = two
Using the register_pretty modifier, you can define beautification for the MyClass class:
from prettyprinter import register_pretty , pretty_call

@register_pretty ( MyClass ) _ _
def pretty_ myclass ( value, ctx ):
return pretty_call(ctx, MyClass,, two=value.two)
>>> from prettyprinter import cpprint
>>> cpprint(MyClass(1, 2))
MyClass(one=1, two=2)
Representation with state instance
One drawback of calling functions is that stateful instances are not well represented. Often you want to output some additional information to represent the state of the instance. PrettyPrinter solves this problem using explanatory comments, and I'm quite happy with this powerful feature. Annotate the Python value (or represent the original layout of the Python value) with a comment that will magically appear in the output.
Suppose we define a Connection class with its connected and disconnected states:
class Connection :
def __init _ _ ( self, hostname) :
self.hostname = hostname
self.is_open = False

def open (self):
self.is_open = True

def close (self):
self.is_open = False
If you want to get the following output:
Connection( ' ' ) # Status: Open
This can be achieved by the following definition:
from prettyprinter import register_pretty, pretty_call, comment

def pretty_connection(connection, ctx):
status_text = (
'Status: Open'
if connection.is_open
else 'Status: Closed'
return comment(
I really enjoy having PrettyPrinter as part of my development kit. A single article can only briefly share some points, there are many interesting parts waiting for you to explore, I strongly recommend everyone to try it! It works better in IPython , because all results in the interactive interpreter environment can be automatically printed out using PrettyPrinter . The documentation has a description of the settings for this command.
Click on source code on GitHub to view the source code of the project, and the documentation is in the documentation on (it may still be a little rudimentary at the moment). The package has built-in out -of-the-box definitions for Django models, QuerySets , and all classes created with the attrs package . So if you happen to be using one of these, you will no doubt want to try it right away!

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us