Use a TOML Config File

Introduction

TOML - A config file format for humans.

TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.

Project #1

There are many of the projects that have hard coded configurations as part of the code. Pick one or more and convert them to use a TOML configuration file. For example:

Install TOML

As of Python 3.11 TOML is built in. You do not need to install it.

TOML Examples

# This is a TOML document title = "TOML Example" [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00-08:00 [database] enabled = true ports = [ 8000, 8001, 8002 ] data = [ ["delta", "phi"], [3.14] ] temp_targets = { cpu = 79.5, case = 72.0 } [servers] [servers.alpha] ip = "10.0.0.1" role = "frontend" [servers.beta] ip = "10.0.0.2" role = "backend"

# This is a TOML document title = "ImpalaPay Co." [database] server = "192.168.1.1" ports = [ 8000, 8001, 8002 ] connection_max = 5000 enabled = true # Line breaks are OK when inside arrays hosts = [ "alpha", "omega" ] [servers] # Indentation (tabs and/or spaces) # is allowed but not required [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10"

Python Example

import tomllib from pprint import pprint filename = 'my_config.toml with open(filename,'rb') as f: toml_config: dict = tomllib.load(f) pprint(toml_config, sort_dicts=False)

Links

TOML (home)

TOML (Wikipedia)

Learn TOML in 10 Minutes (Tutorial) (YouTube)

Pyproject.toml: The modern Python project definition file, explained (YouTube)

tomllib — Parse TOML files (Python documentation)