Using Rich for Python Development
00:00
Using Rich for Python development, Rich can help to make your life as a developer a little bit easier. For instance, it has built-in support for formatting and syntax highlighting Python code and data structures, and it has a useful inspect()
function that lets you examine deeply nested data.
00:18 The Rich installation has support for IPython and Jupyter, but here you’ll be exploring it with Python’s built-in REPL. The first Rich feature that you’ll explore is syntax highlighting.
00:30
This helps to clarify the structure of programming statements and data. Syntax highlighting is built into Rich’s print()
function. Open the Python REPL, define a simple data structure and print it using Python’s built-in print()
function.
00:58 This does exactly what you’d expect. The output has all the information that you want to display, but it doesn’t look very exciting. It would be nice if the different data types were color-coded to provide some visual variety and help you keep the information straight.
01:13 Fortunately, Rich’s version does exactly that.
01:20
Rich’s print()
is a drop-in replacement for Python’s built-in function, so you could safely override the built-in print()
, but here to make comparisons easier, it’s been imported under the alias rprint()
.
01:34
Now the different data types are highlighted. The syntax highlighting makes it easy to see what types the structure contains. Python Standard Library includes a package named pprint
that supports pretty printing.
01:47
While its formatting of complex data structures is a definite improvement over the built-in print()
. It doesn’t support colored text. The real benefits of pretty printing code appear when you’re dealing with more complex structures on screen, you’ll see a small example.
02:04 Let’s suppose you are designing a data structure for your latest app and you’ve hand-coded this dictionary.
02:22 The Python REPL is fairly forgiving about input format, so as long as the code is valid Python as it is here, then the REPL is happy to accept it.
02:33 You carry on coding and a little later you decide to write the code that passes this data structure to remind yourself of the details. You print it to the screen,
02:45 the data is all there, but the REPL’s formatting leaves a lot to be desired. It’s certainly possible to troll through this print output and identify the nested dictionaries and lists, but it’s not easy to do and it’s easy to miss something or make a mistake with a larger data structure.
03:02 This would be next to impossible. Now let’s see what happens. When you print it with Rich,
03:12 the code is neatly formatted and takes advantage of syntax. Highlighting. The layout of the printout makes much more sense and the data types are colored differently.
03:22 This makes the structure of the data much easier to understand. Do you always want your data structures presented like this while you are developing? As you probably know, when you simply type a variable name into the REPL and press enter, it echoes that variable’s value to the console.
03:37
By default, the format is the same as for print()
, but it would be nice to have that value pretty printed by default, and you can do that by installing Rich Pretty Printing to the REPL.
03:55
Now, when you type the variable’s name into the REPL, you automatically get a pretty printed and highlighted representation. Just as you saw with rprint()
.
04:07 Having your data structures displayed in a pleasing and readable format can make your coding experience much more pleasant and productive. You can even configure your REPL to always install Rich’s pretty printing at startup.
04:22
To do so, you’ll need to edit the hidden Python startup file, which is located in your home directory. As you might expect, the code is standard Python, which is executed silently when the REPL is started. pretty
and traceback
are imported from Rich, and in the case of them not being found, the exception is just passed over.
04:43
The else
clause will only be performed if there’s no exception, and it installs pretty
and traceback
to the REPL so they will be present every time.
04:53 It’s great that your data structures are now pretty, color-coded, and formatted, but that’s not always enough in development. Sometimes you want to have a look under the hood of a data structure and really get a snapshot of how it works.
05:05
For that, you can use Rich’s inspect()
function.
05:12 Here you can see what it does with your example data structure.
05:20
With inspect()
, you can really examine the inner workings of an object. Since superhero
is a dictionary, Rich also shows you all the available constructors before displaying the pretty printed data as before.
05:32
You can also add the optional methods=True
parameter to get a handy summary of the object’s methods with their short docstrings and parameter types.
05:59
The inspect()
function is quite powerful. You can get a comprehensive description of its parameters by typing inspect(inspect)
as seen on screen.
06:20
The Python Standard Library has its own inspect
module that also allows live inspection of code objects. It’s much more powerful but also much more complex than the Rich function you’ve been using.
06:32
You should check it out if you need to do heavy-duty code introspection. However, Python’s inspect
module doesn’t offer syntax highlighting. For many investigations, you may find the Rich function more convenient.
06:46
In the next section of the course, you’ll deepen your understanding of Rich, looking at the Console
class and logging.
Become a Member to join the conversation.