🜏 Python Good Practices β€” The Discipline of the Craft

In this laboratory, Python is not merely a toolβ€”it is an instrument of precision.

Code that is unclear, irreproducible, or poorly structured is considered unstable magic.

This page defines the expected standards.


πŸœ‚ Environment Management β€” The First Rule

All projects must use isolated environments.

⚠️ Mandatory Tool: uv

You are required to use :contentReferenceoaicite:0 for:

  • Creating virtual environments
  • Managing dependencies
  • Installing packages

Why uv?

  • Extremely fast (Rust-based)
  • Unified toolchain (replaces pip, venv, pip-tools)
  • Deterministic dependency resolution
  • Simpler workflow

Minimal Workflow

# Create environment
uv init --python {python_version}

# Install dependencies
uv add numpy scipy torch

# Run a script
uv run python your_script.py

🜁 Project Structure β€” Order Before Power

A project must be organized as follows:

project/
β”œβ”€β”€ src/
β”‚   └── your_package/
β”œβ”€β”€ scripts/
β”œβ”€β”€ tests/
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ README.md
└── .venv/

Key Principles

  • Use src/ layout to avoid import ambiguity
  • Separate library code from scripts
  • Always include a pyproject.toml

πŸœƒ Code Quality β€” Clarity Over Cleverness

Rules

  • Functions must be short and single-purpose
  • Variable names must be explicit
  • Avoid hidden side effects
  • Prefer readability over micro-optimizations

Formatting & Linting

Recommended tools:

  • ruff
  • black

Example:

ruff check .
ruff format .

πŸœ„ Reproducibility β€” No Uncontrolled Variance

  • All dependencies must be pinned
  • Experiments must be reproducible from a fresh clone
  • Random seeds must be controlled when relevant

πŸœ‚ Testing β€” Trust, but Verify

Use:

  • pytest

Minimum expectations:

  • Core functions must have tests
  • Edge cases must be considered
  • Tests must run without manual intervention
uv run pytest

🜏 Documentation β€” Knowledge Must Persist

  • Every module must have a docstring
  • Complex logic must be explained
  • Reports must reference the code that generated results

🜏 Final Rule

If your code cannot be:

  • installed cleanly,
  • executed reproducibly,
  • and understood by another researcher,

then it is not complete.

Discipline in tooling and structure is not optional.