typestar

Reading TOML and INI in Python

tomllib is in the standard library, read-only; configparser handles INI.

import configparser
import tomllib

TOML = """
name = "typestar"
languages = 10
[server]
port = 8080
debug = true
"""

config = tomllib.loads(TOML)
print(config["name"], config["languages"], config["server"]["port"])
print(type(config["server"]["debug"]).__name__)

ini = configparser.ConfigParser()
ini.read_string("[server]\nport = 8080\ndebug = yes\n")
print(ini["server"]["port"], ini.getboolean("server", "debug"))

How it works

  1. tomllib.loads takes text, load takes a binary file.
  2. TOML gives you real types: numbers, booleans, dates, tables.
  3. configparser reads everything as a string until you ask otherwise.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
419
Tokens
118
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 67 seconds.

Type this snippet

Step 1 of 3 in Configuration & time, step 32 of 41 in Domain tools.

← Previous Next →