1.4. Built-in libraries#

1.4.1. tomllib - config language written for humans#

A new library is coming out with Python 3.11 - tomllib. It can be used to parse Tom’s Obvious Minimal Language (TOML) files just like JSONs.

TOML file format is very popular, with over 17k stars on GitHub. Here is how you will parse it with Python 3.11 in this fall:

1.4.2. Creating a custom context manager - a timer example#

One of the Python features that other languages dream of is context managers. Not only does Python have built-in context managers but it also allows you to build your own.

Here is a custom context manager as a timer 👇

1.4.3. Built-in variable inspector with locals and globals#

Did you know that Python has built-in variable inspectors available everywhere?

The “globals()” function returns a dictionary of variables in the global scope while “locals()” does the same for local scopes.

Manipulating variables through these dictionaries give you the chance to use string and dict notations on them (think about creating 100 axes in Matplotlib gridspec).

1.4.4. Error messages with assert statements#

In Python, you can throw custom error messages with assert statements as well. Just pass a text after the assert expression and that will be the error message if the assertion fails👇

1.4.5. __file__ variable#

Python has a __file__ variable that lets you see the full path to the current script. The variable isn’t available in notebooks or in REPL.

1.4.6. Reading the text of files with Pathlib#

You don’t have to use “open” function to read the contents of a file. You can use a much better alternative with Pathlib.

After passing the full file path to the Path class, you can call the read_text method which returns the contents as string👇