16.4. TOML Use Case
16.4.1. SetUp
>>> DATA = b"""
... [project]
... name = "myproject"
... version = "1.0.0"
... requires-python = ">=3.13"
... authors = [{name = "Mark Watney", email = "mwatney@nasa.gov"}]
... readme = "README.md"
... license = {file = "LICENSE"}
... keywords = ["ares", "mars", "nasa", "human-spaceflight"]
... urls.homepage = "https://github.com/myusername/myproject"
... urls.repository = "https://github.com/myusername/myproject.git"
... urls.documentation = "https://github.com/myusername/myproject"
... urls.changelog = "https://github.com/myusername/myproject/releases"
... urls.bugtracker = "https://github.com/myusername/myproject/issues"
... dependencies = [
... "django==5.1.*",
... "django-ninja==1.3.*"]
... """
>>>
>>> with open('/tmp/myfile.toml', mode='wb') as file:
... file.write(DATA)
630
16.4.2. Load TOML File
>>> import tomllib
>>>
>>> with open('/tmp/myfile.toml', mode='rb') as file:
... data = tomllib.load(file)
16.4.3. Use
>>> data['project']
{'name': 'myproject',
'version': '1.0.0',
'requires-python': '>=3.13',
'authors': [{'name': 'Mark Watney', 'email': 'mwatney@nasa.gov'}],
'readme': 'README.md',
'license': {'file': 'LICENSE'},
'keywords': ['ares', 'mars', 'nasa', 'human-spaceflight'],
'urls': {'homepage': 'https://github.com/myusername/myproject',
'repository': 'https://github.com/myusername/myproject.git',
'documentation': 'https://github.com/myusername/myproject',
'changelog': 'https://github.com/myusername/myproject/releases',
'bugtracker': 'https://github.com/myusername/myproject/issues'},
'dependencies': ['django==5.1.*', 'django-ninja==1.3.*']}
>>> data['project']['name']
'myproject'
>>> data['project']['version']
'1.0.0'
>>> data['project']['requires-python']
'>=3.13'
>>> data['project']['authors']
[{'name': 'Mark Watney', 'email': 'mwatney@nasa.gov'}]
>>> data['project']['dependencies']
['django==5.1.*', 'django-ninja==1.3.*']
>>> data['project']['urls']
{'homepage': 'https://github.com/myusername/myproject',
'repository': 'https://github.com/myusername/myproject.git',
'documentation': 'https://github.com/myusername/myproject',
'changelog': 'https://github.com/myusername/myproject/releases',
'bugtracker': 'https://github.com/myusername/myproject/issues'}