Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Hands on the Python Library toml

Tips & Traps

  1. Please refer to Parse TOML File in Python for general tips on parsing TOML in Python.

Installatoion

In [1]:
pip3 install toml
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: toml in /usr/local/lib/python3.8/dist-packages (0.10.2)

Parse TOML from a String

In [2]:
import toml
In [3]:
toml_string = """
    # This is a TOML document.
    title = "TOML Example"

    [owner.info]
    name = "Tom Preston-Werner"
    dob = 1979-05-27T07:32:00-08:00 # First class dates

    [database]
    server = "192.168.1.1"
    ports = [ 8001, 8001, 8002 ]
    connection_max = 5000
    enabled = true

    [servers]

    [servers.alpha]
    ip = "10.0.0.1"
    dc = "eqdc10"

    [servers.beta]
    ip = "10.0.0.2"
    dc = "eqdc10"

    [clients]
    data = [ ["gamma", "delta"], [1, 2] ]

    # Line breaks are OK when inside arrays
    hosts = [
    "alpha",
    "omega"
    ]
    """
In [4]:
dic = toml.loads(toml_string)
dic
Out[4]:
{'title': 'TOML Example',
 'owner': {'info': {'name': 'Tom Preston-Werner',
   'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=<toml.tz.TomlTz object at 0x7f3d687e93d0>)}},
 'database': {'server': '192.168.1.1',
  'ports': [8001, 8001, 8002],
  'connection_max': 5000,
  'enabled': True},
 'servers': {'alpha': {'ip': '10.0.0.1', 'dc': 'eqdc10'},
  'beta': {'ip': '10.0.0.2', 'dc': 'eqdc10'}},
 'clients': {'data': [['gamma', 'delta'], [1, 2]],
  'hosts': ['alpha', 'omega']}}
In [6]:
dic = toml.load(Path("pyproject.toml").open())
dic
Out[6]:
{'tool': {'poetry': {'name': 'xinstall',
   'version': '0.51.1',
   'description': 'Easy Cross-platform Installation and Configuration of Apps.',
   'authors': ['Benjamin Du <longendu@yahoo.com>'],
   'license': 'MIT',
   'readme': 'readme.md',
   'repository': 'https://github.com/legendu-net/xinstall',
   'keywords': ['installation', 'configuration', 'cross-platform'],
   'scripts': {'xinstall': 'xinstall:main.main'},
   'dependencies': {'python': '>=3.7.1,<4',
    'distro': '>=1.5.0',
    'tqdm': '>=4.48.2',
    'findspark': '>=1.4.2',
    'requests': '>=2.25.0',
    'packaging': '>=20.4',
    'tomlkit': '>=0.7.0',
    'dulwich': '>=0.20.24'},
   'dev-dependencies': {'pytest': '>=3.0',
    'pylint': '>=2.4.4',
    'pytype': {'version': '>=2020.08.10', 'python': '<3.9'},
    'yapf': '>=0.32.0',
    'coverage': '>=5.3',
    'deepdiff': '>=5.2.3'}},
  'pylint': {'master': {'ignore': '.venv,.ipynb_checkpoints',
    'unsafe-load-any-extension': 'no',
    'load-plugins': 'pylint.extensions.docparams',
    'extension-pkg-whitelist': 'numpy,cv2,pyspark',
    'generated-members': 'sqlite3.*,cv2.*,pyspark.*',
    'ignored-modules': 'pyspark.sql.functions'},
   'messages_control': {'disable': 'C0103,C0200,C0301,C0302,C0303,C0330,R0801,R0902,R0903,R0904,R0911,R0912,R0913,R0914,W0621,W0622,W0702,W0703,W1116,W9012,W9016'},
   'typecheck': {'ignored-classes': 'Namespace'}},
  'yapf': {'based_on_style': 'facebook', 'column_limit': '88'},
  'yapfignore': {'ignore_patterns': ['*/.ipynb_checkpoints/']}},
 'build-system': {'requires': ['poetry>=1.0.0'],
  'build-backend': 'poetry.masonry.api'}}
In [7]:
print(toml.dumps(dic))
[build-system]
requires = [ "poetry>=1.0.0",]
build-backend = "poetry.masonry.api"

[tool.poetry]
name = "xinstall"
version = "0.51.1"
description = "Easy Cross-platform Installation and Configuration of Apps."
authors = [ "Benjamin Du <longendu@yahoo.com>",]
license = "MIT"
readme = "readme.md"
repository = "https://github.com/legendu-net/xinstall"
keywords = [ "installation", "configuration", "cross-platform",]

[tool.yapf]
based_on_style = "facebook"
column_limit = "88"

[tool.yapfignore]
ignore_patterns = [ "*/.ipynb_checkpoints/",]

[tool.poetry.scripts]
xinstall = "xinstall:main.main"

[tool.poetry.dependencies]
python = ">=3.7.1,<4"
distro = ">=1.5.0"
tqdm = ">=4.48.2"
findspark = ">=1.4.2"
requests = ">=2.25.0"
packaging = ">=20.4"
tomlkit = ">=0.7.0"
dulwich = ">=0.20.24"

[tool.poetry.dev-dependencies]
pytest = ">=3.0"
pylint = ">=2.4.4"
yapf = ">=0.32.0"
coverage = ">=5.3"
deepdiff = ">=5.2.3"

[tool.pylint.master]
ignore = ".venv,.ipynb_checkpoints"
unsafe-load-any-extension = "no"
load-plugins = "pylint.extensions.docparams"
extension-pkg-whitelist = "numpy,cv2,pyspark"
generated-members = "sqlite3.*,cv2.*,pyspark.*"
ignored-modules = "pyspark.sql.functions"

[tool.pylint.messages_control]
disable = "C0103,C0200,C0301,C0302,C0303,C0330,R0801,R0902,R0903,R0904,R0911,R0912,R0913,R0914,W0621,W0622,W0702,W0703,W1116,W9012,W9016"

[tool.pylint.typecheck]
ignored-classes = "Namespace"

[tool.poetry.dev-dependencies.pytype]
version = ">=2020.08.10"
python = "<3.9"

Parse TOML from a File

In [21]:
with open("example.toml", "r") as fin:
    dic = toml.load(fin)
dic
Out[21]:
{'title': 'TOML Example',
 'database': {'server': '192.168.1.1',
  'ports': [8001, 8001, 8002],
  'connection_max': 5000,
  'enabled': True},
 'clients': {'data': [['gamma', 'delta'], [1, 2]],
  'hosts': ['alpha', 'omega']},
 'owner': {'info': {'name': 'Tom Preston-Werner',
   'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=<toml.tz.TomlTz object at 0x11f7b7490>)}},
 'servers': {'alpha': {'ip': '10.0.0.1', 'dc': 'eqdc10'},
  'beta': {'ip': '10.0.0.2', 'dc': 'eqdc10'}}}

Dump an Object to a TOML String

In [ ]:
with open("example.toml", "r") as fin:
    dic = toml.load(fin)
In [18]:
print(toml.dumps(dic))
title = "TOML Example"

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002,]
connection_max = 5000
enabled = true

[clients]
data = [ [ "gamma", "delta",], [ 1, 2,],]
hosts = [ "alpha", "omega",]

[owner.info]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"

[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"

Dump an Object to a TOML File

In [ ]:
with open("example.toml", "r") as fin:
    dic = toml.load(fin)
In [ ]:
with open("/tmp/example.toml", "w") as fout:
    toml.dump(dic, fout)

Comments