Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Modernize a measurement report

This repository contains a working laboratory report utility written in a manual style. Refactor it using the modern Python features introduced in this section while preserving its behavior.

Set up and establish the baseline

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.

Checkpoint 1: make the record explicit

Replace the manually implemented Measurement class with:

@dataclass(frozen=True, slots=True)
class Measurement:
    sample_id: str
    value: float
    unit: str

The 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.

Checkpoint 2: use Path objects

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.

Checkpoint 3: simplify report writing

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.

Checkpoint 4: complete the docstrings

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.

Verify and submit

Run all checks and inspect the refactor:

python -m pytest
python -m ruff format --check .
python -m ruff check .
git diff
git status

Commit and submit the result:

git add measurement_report.py tests/test_measurement_report.py
git commit -m "Modernize measurement reporting"
gh student submit

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages