Code

Read the ID Card

pyproject.toml is written in TOML, a simple config format. It has [tables] with key = value pairs inside. Since Python 3.11, the standard library can read it with tomllib:

import tomllib

data = tomllib.loads(text)        # from a string
with open("pyproject.toml", "rb") as f:
    data = tomllib.load(f)        # from a file (binary mode!)

data["project"]["name"]          # 'lantern-tools'

TOML tables become nested dicts. [project.scripts] ends up as data["project"]["scripts"]. The [project.scripts] table creates command-line commands: lantern = "lantern_tools.cli:main" means typing lantern runs main() from lantern_tools/cli.py.

Dependencies are requirement strings such as "rich>=13". That means the name rich, at version 13 or newer.

Pip tip: tomllib can only read TOML. To write it, you'd need a third-party package such as tomli-w.

Assignment

Write summarize_project(text) that parses the TOML text and returns a dict with:

  1. "name" and "version" from [project]
  2. "python": the requires-python value, or "any" if missing
  3. "deps": dependency names only (use the dep_name helper), or []
  4. "commands": the sorted names in [project.scripts], or []

If there is no [project] table, raise ValueError.