14.3. TOML Use Case
14.3.1. SetUp
>>> DATA = b"""
... [project]
... name = "myproject"
... version = "1.0.0"
... requires-python = ">=3.13"
... authors = [{name = "Alice", email = "alice@example.com"}]
... readme = "README.md"
... license = {file = "LICENSE"}
... keywords = ["myproject", "myapp", "python", "django", "ninja"]
... 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.2.*",
... "django-ninja==1.4.*"]
... """
>>>
>>> with open('/tmp/myfile.toml', mode='wb') as file:
... file.write(DATA)
631
14.3.2. Load TOML File
>>> import tomllib
>>>
>>> with open('/tmp/myfile.toml', mode='rb') as file:
... data = tomllib.load(file)
14.3.3. Use
>>> data['project']
{'name': 'myproject',
'version': '1.0.0',
'requires-python': '>=3.13',
'authors': [{'name': 'Alice', 'email': 'alice@example.com'}],
'readme': 'README.md',
'license': {'file': 'LICENSE'},
'keywords': ['myproject', 'myapp', 'python', 'django', 'ninja'],
'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.2.*', 'django-ninja==1.4.*']}
>>> data['project']['name']
'myproject'
>>> data['project']['version']
'1.0.0'
>>> data['project']['requires-python']
'>=3.13'
>>> data['project']['authors']
[{'name': 'Alice', 'email': 'alice@example.com'}]
>>> data['project']['dependencies']
['django==5.2.*', 'django-ninja==1.4.*']
>>> 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'}