Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix A — Python Setup

Goal

Run the ShopOps reference code on Python 3.11+ with typing and tests.

Install

cd manuscript/code/shopops
python3.11 -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -e ".[dev]"
pytest -q

No model API key is required for mock/scripted policies. Set MODEL_API_KEY only when exercising live gateway paths.

Typing

  • Prefer list[str], dict[str, Any], X | None (3.10+).
  • Use dataclass for State / Action / Observation.
  • Use Protocol for stores and executors you will swap in tests.

Example:

from typing import Protocol

class MemoryStore(Protocol):
    def get(self, key: str) -> dict | None: ...
    def put(self, key: str, value: dict) -> None: ...

Testing rings

RingCommandIntent
0pytest tests/ -qPure units: policy, FSM, schemas
1pytest evals/ -qTrajectory properties on fixtures
2nightly simulatorFault injection (optional)

Put regression fixtures next to the behaviour they protect. A red send-window test blocks merge.

Project layout reminder

See manuscript/code/shopops/README.md and manuscript/code-plan.md. Teaching scripts may live under examples/chXX_.

Common issues

IssueFix
Wrong Pythonpython3 --version ≥ 3.11
Imports failpip install -e . from shopops/ root
Tests miss packagepythonpath = ["."] in pyproject.toml
Docker smokeAppendix C / docker compose up

Optional tools

  • ruff / mypy — author recommendation for larger forks; not required to read the book.
  • httpx — when chapters introduce real HTTP tools.