Dumping Python Objects as TOML
00:00 Dump Python Objects as TOML. You’ve already seen how to read TOML files with Python, but how do you do the opposite? TOML documents are often written by hand because they’re mainly used for configuration, but sometimes, you may need to convert a nested dictionary into a TOML document.
00:17
In this section of the course, you’ll start by coding up a basic TOML writer by hand, and then you’ll have a look at tools that are already available and use the third-party tomli_w
library to dump your data to TOML.
00:30 Recall the tic-tac-toe configuration you were working with earlier. You can represent a slightly changed version of it as a nested Python dictionary. Next, you’ll write a simplified TOML writer that’s able to write this dictionary as a TOML document.
00:45
You won’t implement all the features of TOML, and in particular, you are leaving out some value types, such as times, dates, and arrays of tables. You are also not handling keys that need to be quoted or multi-line strings, but your implementation will handle many of the typical use cases for TOML. First, code up a helper function named _dumps_value()
.
01:06
This function will take some value and return its TOML representation based on the value type. You can do this with isinstance()
checks.
01:44
You return true
or false
for Boolean values and add double quotation marks around strings. If your value is a list, you create a TOML array by the function calling itself recursively.
02:09
If you’re using Python 3.10 or newer, then you can replace your isinstance()
checks with a match
case statement instead. Next, you’ll add the code that handles the tables.
02:20 Your main function loops over a dictionary and converts each item into a key-value pair. If the value happens to be a dictionary, then you’ll add a table header and fill out the table recursively.
02:35 For convenience, you use a list that keeps track of each table or key-value pair as you add it. You convert this list to a string just before you return it.
03:17 In addition to the limitations mentioned earlier, there’s one subtle bug hiding in this function. Consider what happens if you try to dump the example from earlier.
03:50
It looks like ai_skill
and board_size
are keys in the user.player_o
table, but according to the original data, they should be members of the [user]
and root tables respectively.
04:01 The issue is there’s no way to mark the end of a TOML table. Instead, regular keys must be listed before any sub-tables. One way to fix your code is to sort your dictionary items so that dictionary values come after all the other values.
04:16 So to do this, update the function as seen on screen.
04:26
In practice, tables_at_end
returns false
or zero for all non-dictionary values and true
, which is equivalent to one for all dictionary values.
04:35 Using this as a sorting key ensures that nested dictionaries are handled after all other kinds of values.
04:49 You can now perform the previous example again.
04:56
Printing out the result to the screen, you should see this TOML document. Here, board_size
is listed at the top as part of the root table as expected, and ai_skill
is now a key in [user]
as it should be.
05:10 Even though TOML isn’t a complicated format, there are some finer points that you need to take into account when creating your own TOML writer. So instead of pursuing this task further, in the next section of the course, you’ll see how you can use an existing library to dump your data into TOML.
Become a Member to join the conversation.