PrettyPrinter is a powerful, descriptive, descriptive landscaping print package from Python 3.6 and above. It uses the improved Wadler-Leijen layout algorithm, similar to the prettyprinter in Haskell's print beautification library, as well as anti-wl-pprint, JavaScript's Prettier, Ruby's prettypreinter.rb, and IPython's Ipython.lib.pretty. Python's PrettyPrinter is based on the strengths of the above, and continues to improve on this basis, so it is now the most powerful landscaping output tool for Python.
Here's a screenshot of the output using PrettyPrinter:
Why does Python need an extra landscaping print package?
Whether the IDE or the developer manually runs the command, printing the data to the screen is the most basic interface for the programmer and the numerical interaction during the running of the program. Improving this interface will help improve the development experience and productivity. Both Python itself and third-party libraries provide tools to do this:
The two underscore methods __repr__ and __str__ return a normal string. __repr__ should return as much grammatically correct Python expression as possible. The most common use of assertion failures and console calculations is the method. Because it is completely based on string formatting, it does not have the ability to beautify printing.
The pprint module in the standard library provides landscaping for built-in data types such as dicts, lists, tuples, sets, and frozensets. It applies the __repr__ method to a user-defined class instance. However, it uses a very greedy layout algorithm that causes problems with beautification printing in many cases. Since custom beautification printing is limited by __repr__, the effect of pprint is limited to the built-in data type.
The third-party library pprintpp is an improvement and alternative to pprint, and can also optimize the output, but like the pprint is limited by the code beautification definition used by __repr__.
The default print module in IPython, IPython.lib.pretty, is aimed at a more advanced alternative to pprint. Compared to pprint, it performs better in many ways: in most cases the algorithm can beautify the output, and provides a definition tool for the user-defined type of beautification output, which can be better with other parts of the output. Combination of. However, in order to implement your own beautification printing method, you need to understand the layout algorithm. In addition, the API has some inherent side effects: calling the landscaping print tool to push data directly into the layout buffer does not allow the original layout to initially detect the data.
All of the above tools do not meet my requirements for the beautification print experience, so I started to make the following improvements:
Implement an algorithm that can beautify as much as possible, even with some sacrifice in efficiency. It takes a tenth of a second to beautify the output, because it will save 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 print tools. Python members hardly override the __repr__ method because it's painful; almost no one is willing to write neat print 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 return a valid grammar, which interrupts normal syntax highlighting in the event of a grammatical error.
I am very surprised by the experience of using the new code landscaping package. The algorithm works very well and the efficiency also meets the demand. The user-defined way to customize the rules is also very simple, just need to understand two descriptive functions register_pretty and pretty_call. The syntax highlighting looks very beautiful and won't be interrupted by invalid syntax. In particular, the syntax highlighting will make it difficult for you to return to the normal landscaping printing tool, which greatly enhances the programmer's development experience.
The most interesting improvement is the descriptive API, and below is how it works.
Simple, descriptive API
Defining output beautification methods in PrettyPrinter is primarily based on (creating) function calls. All non-character Python values ​​need to be represented by function results. The library's main function is pretty_call, which allows you to describe what type of function call the PrettyPrinter should output. The following is an example of a pretty_call call:
The process that PrettyPrinter handles the original layout is similar to the following statement:
(The first parameter, ctx, allows the user to control the nested data in the [5,3,6,1] list in the case. The True value of the reverse parameter is rendered accordingly. In most cases, the default value can be used directly.)
The above describes how to use Pretty_call, and then define our own type.
Use the register_pretty modifier to define the beautification method for the MyClass class:
The output of cpprint is as follows:
Click on the PrettyPrinter definition code for standard library types to see more examples.
Representation with state instance
One drawback of calling functions is that they do not represent well-cased instances. Usually you want to output some additional information to indicate the state of the instance. PrettyPrinter solves this problem with an explanatory comment, and I am quite satisfied with this powerful feature. Use comments to annotate Python values ​​(or the original layout of Python values), which will magically appear in the output.
Suppose we define a Connection class that contains its state of connection and disconnection:
If you want to get the following output:
Can be achieved by the following definition:
in conclusion
I really enjoy PrettyPrinter as part of the development kit. A single article can only share some points roughly, and there are many interesting parts waiting for you to explore. I highly recommend everyone to try it! It works better in IPython because all results in the interactive interpreter environment can automatically use the PrettyPrinter printout. A description of the settings for this command is available in the documentation.
Click source code on GitHub to view the source code for the project. The documentation is on documentation on readthedocs.io (which may still be a bit simplistic). The package has built-in definitions for Django models, QuerySets, and all classes created with the attrs package. So if you happen to use one of them, there is no doubt that you will want to try it right away!
Industrial PC, a Fan-less cooling and smallest case allows for space-saving design, so it can be placed horizontally or vertically to offer you best placement options, and you`ll set it on a desk and forget its even there.
Low cost mini pc, Personal computer, Miniconputer, Industrial PC, Mini pc windows
Shenzhen Innovative Cloud Computer Co., Ltd. , https://www.xcypc.com