16.2. TOML Load
16.2.1. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: TOML Load Version
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% English
# 1. Read configuration from `FILE`
# 2. Define `result: str` with version read from config
# 3. Use `tomllib.load()`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj konfigurację z `FILE`
# 2. Zdefiniuj `result: str` z werją wczytaną z konfiguracji
# 3. Użyj `tomllib.load()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `open(filename, mode='rb')`
# - `import tomllib`
# - `tomllib.load()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 11), \
'Python 3.11+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> from pprint import pprint
>>> pprint(result)
'1.0.0'
>>> from pathlib import Path
>>> Path(FILE).unlink(missing_ok=True)
"""
import tomllib
FILE = '_temporary.toml'
with open(FILE, mode='wb') as file:
file.write(b"""
project = 'My App'
version = '1.0.0'
""")
# Read configuration from `FILE`
# Define `result: str` with version read from config
# type: str
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: TOML Load Database
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% English
# 1. Read configuration from `FILE`
# 2. Define `result: dict` with database config
# 3. Use `tomllib.load()`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj konfigurację z `FILE`
# 2. Zdefiniuj `result: dict` z konfiguracją bazy danych
# 3. Użyj `tomllib.load()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `open(filename, mode='rb')`
# - `import tomllib`
# - `tomllib.load()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 11), \
'Python 3.11+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'
>>> assert result['hostname'] == '127.0.0.1'
>>> assert result['port'] == 5432
>>> assert result['username'] == 'mwatney'
>>> assert result['password'] == 'Ares3'
>>> assert result['database'] == 'nasa'
>>> from pathlib import Path
>>> Path(FILE).unlink(missing_ok=True)
"""
import tomllib
FILE = '_temporary.toml'
with open(FILE, mode='wb') as file:
file.write(b"""
project = 'My App'
version = '1.0.0'
[database]
hostname = '127.0.0.1'
port = 5432
username = 'mwatney'
password = 'Ares3'
database = 'nasa'
""")
# Read configuration from `FILE`
# Define `result: dict` with database config
# type: dict[str, str|int]
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: TOML Load Authors
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% English
# 1. Read configuration from `FILE`
# 2. Define `result: datetime` with release date
# 3. Use `tomllib.load()`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj konfigurację z `FILE`
# 2. Zdefiniuj `result: datetime` z datą wdrożenia
# 3. Użyj `tomllib.load()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `open(filename, mode='rb')`
# - `import tomllib`
# - `tomllib.load()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 11), \
'Python 3.11+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> from datetime import datetime
>>> assert type(result) is datetime, \
'Variable `result` has invalid type, should be datetime'
>>> from pprint import pprint
>>> pprint(result)
datetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
>>> from pathlib import Path
>>> Path(FILE).unlink(missing_ok=True)
"""
import tomllib
FILE = '_temporary.toml'
with open(FILE, mode='wb') as file:
file.write(b"""
project = 'My App'
version = '1.0.0'
[database]
hostname = '127.0.0.1'
port = 5432
username = 'mwatney'
password = 'Ares3'
database = 'nasa'
[metadata]
release_date = 2020-01-01T00:00:00Z
""")
# Read configuration from `FILE`
# Define `result: datetime` with release date
# type: dict[str, str|int]
result = ...
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: TOML Load Authors
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% English
# 1. Read configuration from `FILE`
# 2. Define `result: list[str]` with author emails
# 3. Use `tomllib.load()`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj konfigurację z `FILE`
# 2. Zdefiniuj `result: list[str]` z adresami email autorów
# 3. Użyj `tomllib.load()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `open(filename, mode='rb')`
# - `import tomllib`
# - `tomllib.load()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 11), \
'Python 3.11+ required'
>>> from pprint import pprint
>>> from pathlib import Path
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> result = sorted(result)
>>> pprint(result)
['avogel@nasa.gov',
'bjohanssen@nasa.gov',
'cbeck@nasa.gov',
'mlewis@nasa.gov',
'mwatney@nasa.gov',
'rmartinez@nasa.gov']
>>> Path(FILE).unlink(missing_ok=True)
"""
import tomllib
FILE = '_temporary.toml'
with open(FILE, mode='wb') as file:
file.write(b"""
project = 'My App'
version = '1.0.0'
[database]
hostname = '127.0.0.1'
port = 5432
username = 'mwatney'
password = 'Ares3'
database = 'nasa'
[metadata]
release_date = 2020-01-01T00:00:00Z
authors = [
{ name = 'Mark Watney', email = 'mwatney@nasa.gov' },
{ name = 'Melisa Lewis', email = 'mlewis@nasa.gov' },
{ name = 'Rick Martinez', email = 'rmartinez@nasa.gov' },
{ name = 'Alex Vogel', email = 'avogel@nasa.gov' },
{ name = 'Beth Johanssen', email = 'bjohanssen@nasa.gov' },
{ name = 'Chris Beck', email = 'cbeck@nasa.gov' },
]
""")
# Read configuration from `FILE`
# Define `result: list[str]` with author emails
# type: list[str]
result = ...
# FIXME: czy coś jest nie tak z tym plikiem? (make template się wywala)
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: TOML Load Stats
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% English
# 1. File `FILE` contains game characters stats
# 2. Define function `get_stats()`
# - Parameter `character_name: str`, example: 'Sarevok'
# - Returns `dict` with character stats
# 3. Use `tomllib.load()`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Plik `FILE` zawiera statystyki postaci z gry
# 2. Zdefiniuj funkcję `get_stats()`
# - Parametr `character_name: str`, przykład: 'Sarevok'
# - Zwraca `dict` ze statystykami postaci
# 3. Użyj `tomllib.load()`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `open(filename, mode='rb')`
# - `import tomllib`
# - `tomllib.load()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 11), \
'Python 3.11+ required'
>>> imoen = get_stats('Imoen')
>>> assert imoen['character_class'] == 'Thief'
>>> assert imoen['race'] == 'Human'
>>> assert imoen['alignment'] == 'Neutral Good'
>>> assert imoen['strength'] == 9
>>> assert imoen['dexterity'] == 18
>>> assert imoen['constitution'] == 16
>>> assert imoen['intelligence'] == 17
>>> assert imoen['wisdom'] == 11
>>> assert imoen['charisma'] == 16
>>> minsc = get_stats('Minsc')
>>> assert minsc['character_class'] == 'Ranger'
>>> assert minsc['race'] == 'Human'
>>> assert minsc['alignment'] == 'Neutral Good'
>>> assert minsc['strength'] == 18
>>> assert minsc['dexterity'] == 15
>>> assert minsc['constitution'] == 15
>>> assert minsc['intelligence'] == 8
>>> assert minsc['wisdom'] == 6
>>> assert minsc['charisma'] == 9
>>> neera = get_stats('Neera')
>>> assert neera['character_class'] == 'Wild Mage'
>>> assert neera['race'] == 'Half-elf'
>>> assert neera['alignment'] == 'Chaotic Neutral'
>>> assert neera['strength'] == 11
>>> assert neera['dexterity'] == 17
>>> assert neera['constitution'] == 14
>>> assert neera['intelligence'] == 17
>>> assert neera['wisdom'] == 10
>>> assert neera['charisma'] == 11
>>> sarevok = get_stats('Sarevok')
>>> assert sarevok['character_class'] == 'Fighter'
>>> assert sarevok['race'] == 'Human'
>>> assert sarevok['alignment'] == 'Chaotic Evil'
>>> assert sarevok['strength'] == 18
>>> assert sarevok['dexterity'] == 17
>>> assert sarevok['constitution'] == 18
>>> assert sarevok['intelligence'] == 17
>>> assert sarevok['wisdom'] == 10
>>> assert sarevok['charisma'] == 15
>>> from pathlib import Path
>>> Path(FILE).unlink(missing_ok=True)
"""
import tomllib
FILE = '_temporary.toml'
with open(FILE, mode='wb') as file:
file.write(b"""
[Imoen]
character_class = 'Thief'
race = 'Human'
alignment = 'Neutral Good'
strength = 9
dexterity = 18
constitution = 16
intelligence = 17
wisdom = 11
charisma = 16
[Minsc]
character_class = 'Ranger'
race = 'Human'
alignment = 'Neutral Good'
strength = 18
dexterity = 15
constitution = 15
intelligence = 8
wisdom = 6
charisma = 9
[Neera]
character_class = 'Wild Mage'
race = 'Half-elf'
alignment = 'Chaotic Neutral'
strength = 11
dexterity = 17
constitution = 14
intelligence = 17
wisdom = 10
charisma = 11
[Sarevok]
character_class = 'Fighter'
race = 'Human'
alignment = 'Chaotic Evil'
strength = 18
dexterity = 17
constitution = 18
intelligence = 17
wisdom = 10
charisma = 15
""")
# File `FILE` contains game characters stats
# Define function `get_stats()`
# - Parameter `character_name: str`, example: 'Sarevok'
# - Returns `dict` with character stats
# type: Callable[[str], dict]
def get_stats(character_name):
...