This repository contains a working laboratory report utility written in a manual style. Refactor it using the modern Python features introduced in Sec. 6 while preserving its behavior.
Create or activate an environment and install the development tools:
python -m pip install -r requirements-dev.txt
python -m pytest
python -m ruff format --check .
python -m ruff check .The tests and formatting check initially pass. Ruff linting reports issues to address during the refactor. Read measurement_report.py and its tests before changing either file.
Replace the manually implemented Measurement class with:
@dataclass(frozen=True, slots=True)
class Measurement:
sample_id: str
value: float
unit: strThe public constructor and attributes must remain the same. Add tests showing:
- records with equal fields compare equal;
- records with at least one different field compare unequal;
- fields cannot be reassigned.
Use normal pytest discovery conventions, but choose descriptive test names of your own.
Change report_path to accept and return Path objects:
def report_path(output_dir: Path, sample_id: str) -> Path:Join path components with / instead of string concatenation. Update the tests and callers to construct paths with Path.
Use an f-string in render_report while preserving this exact output:
Sample A5: 21.35 C
Change save_report to accept a Path, create missing parent directories, and open the file with a context manager and UTF-8 encoding:
def save_report(path: Path, report: str) -> None:Use pytest's tmp_path fixture when testing file output. Tests must not write into the repository or depend on the process's working directory.
Add a Google-style Returns: section to each function that returns a value, including report_path and render_report. Ruff checks these sections using pydoclint. save_report returns None and does not need one.
Run all checks and inspect the refactor:
python -m pytest
python -m ruff format --check .
python -m ruff check .
git diff
git statusCommit and submit the result:
git add measurement_report.py tests/test_measurement_report.py
git commit -m "Modernize measurement reporting"
gh student submit