Loading video player…

Using TOML as a Configuration Format

00:00 Use TOML as a Configuration Format

00:04 TOML is short for Tom’s Obvious Minimal Language and is humbly named after its creator, Tom Preston-Werner. It was designed expressly to be a configuration file format that should be easy to parse into data structures in a wide variety of languages.

00:18 In this section, you’ll start thinking about configuration files and look at what TOML brings to the table.

00:25 A configuration is an important part of almost any application or system. It will allow you to change settings or behavior without changing the source code.

00:34 Sometimes, you’ll use a configuration to specify information needed to connect to another service, such as a database or cloud storage. Other times, you’ll use configuration settings to allow your users to customize their experience with your project.

00:48 Using a configuration file for your project is a good way to separate your code from its settings. It also encourages you to be conscious about which parts of your system are genuinely configurable, giving you a tool to name magic values in your source code.

01:03 For now, consider the configuration file seen on screen for a hypothetical tic-tac-toe game. You could potentially code this directly into your source code, but by moving the settings into a separate file, you gain the following: you give explicit names to values, you provide these values more visibility, and you make it simpler to change these values.

01:25 Look once more at the hypothetical configuration file. Those values are conceptually different. The colors are values that your framework probably supports changing.

01:35 In other words, if you replace blue with red, that would be honored without any special handling in your code. You could even consider if it’s worth exposing this configuration to your end users through the front end, but the board size may or may not be configurable.

01:50 A tic-tac-toe game is played on a three by three grid. It’s not certain that the logic would still work for other board sizes. It may still make sense to keep the value in your configuration file, both to give a name to the value and to make it visible.

02:04 Finally, the project URL is usually essential when deploying an application. It’s not something that a typical user will change, but a power user may want to re-deploy the game to a different server.

02:16 To be more explicit about these different use cases, you may want to add some organization to your configuration.

02:23 One popular action is to separate your configuration into additional files, each dealing with a different concern. Another option is to group configuration values.

02:33 For example, you can organize your hypothetical configuration file as seen on screen. The organization of the file makes the role of each configuration item clearer.

02:44 You can also add comments into the configuration file with instructions to anyone thinking about making changes to it.

02:51 The actual format of the configuration file isn’t important for this discussion. These principles hold independently of how you specify your configuration values. As it happens, the examples you’ve seen so far can be parsed by Python’s ConfigParser class.

03:06 There are many ways for you to specify a configuration. Windows has traditionally used .ini files, which resemble the configuration file you’ve just seen. Unix systems have relied on plain text, human-readable configuration files although the actual format varies between different services.

03:23 Over time, more and more applications have come to use well-defined formats such as XML, JSON or YAML for their configuration needs. These formats were designed as data interchange or serialization formats usually meant for computer communication.

03:39 On the other hand, configuration files are often written or edited by humans. Many developers have become frustrated with JSON’s strict comment rules when updating their Visual Studio Code settings or with YAML’s nested indentations when setting up a cloud service.

03:54 Despite their ubiquity, these file formats aren’t the easiest to write by hand. But there is a better solution to this problem, and it’s the subject of this course, TOML, which you’ll look at in the next section.

Become a Member to join the conversation.