MIL-002: Read with the csv library #27

Merged
Tirsvad merged 5 commits from mil-002-read-with-csv into main 2026-10-13 09:28:24 +02:00
5 changed files with 182 additions and 1 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ document of a type. `Primary File` may contain a glob (e.g.
| PP | Project Plan | docs/project-plan.md | 002 |
| MIL | Milestone / Gateway | docs/milestones/*.md | 006 |
| DICT | Domain Dictionary | docs/dictionary.md | 002 |
| RC | SQA Review Record | docs/sqa/reviews/rc-*.md | 010 |
| RC | SQA Review Record | docs/sqa/reviews/rc-*.md | 011 |
## Languages
+61
View File
@@ -0,0 +1,61 @@
# RC-010: Code review of MIL-002 (the csv reader: read_rows and read_temperatures)
## Metadata
| Key | Value |
| --- | --- |
| ID | RC-010 |
| CrossReference | [MIL-002], [QC-PY-001] |
## Version History
| Date | Status | Author | Reviewer | Change | Commit |
| --- | --- | --- | --- | --- | --- |
| 2026-10-13 | Proposed | Jens Tirsvad Nielsen | AI as stand-in for S01 | Initial version | [ff6d1b2] |
---
## Artifact Under Review
- Instance reviewed: the source and tests that [MIL-002] delivers (the csv reader: read_rows and read_temperatures)
- Checklist used: [QC-PY-001]
- Scope: full review of the files changed on the branch of the milestone
- Language and domain: n/a
- Language reviewer: none
- Mode: autonomous, not independent (the reviewer is `AI as stand-in for S01`; the assistant also wrote the code; the review of the files was done by the `python-developer` agent of the session)
## Checklist Results
| # | Criterion | Status | Evidence/Notes |
| --- | --- | --- | --- |
| 1 | Packages, modules, functions, variables, classes and constants follow PEP 8 casing (`snake_case`, `PascalCase`, `UPPER_SNAKE`) | Pass | `csv_reader`, `read_rows`, `read_temperatures`, `small_csv` are `snake_case`; no new constant. |
| 2 | Names state purpose in the domain's language; no unexplained abbreviations, no single-letter names outside tiny scopes | Pass | Names say what they return (`read_rows`, `read_temperatures`, `data_rows`); `row` and `file` are tiny-scope names from the lecture. |
| 3 | Code is produced by the project's formatter and passes its linter with no unexplained suppressions | Pass | `python -m ruff check src tests` and `python -m ruff format --check src tests` exit 0; no `noqa`. |
| 4 | Every function and method signature is type-annotated, including `-> None` | Pass | `read_rows(path: Path | str = DATA_FILE) -> list[list[str]]` and `read_temperatures(...) -> list[int]`; the tests are annotated `-> None`. |
| 5 | No bare `except:`, no swallowed exceptions; specific exceptions are raised and the cause is kept (`raise ... from`) | N-A | The module raises and catches nothing; a missing file raises `FileNotFoundError` and a non-number raises `ValueError` to the caller, which is the lecture's behaviour. |
| 6 | No mutable default arguments and no shadowed builtins | Pass | The only default is an immutable `DATA_FILE`; no builtin is shadowed (`file` is not a builtin in Python 3). |
| 7 | Files, locks and connections are managed with context managers | Pass | The file is opened with `with open(...)` in `read_rows`; no other file is opened. |
| 8 | Public modules, classes and functions have docstrings that say what, not how | Pass | Module docstring and Doxygen `@brief`, `@param`, `@return` on both functions; `doxygen Doxyfile` exits 0 with no warnings. |
| 9 | Logging uses `logging`, not `print`; no secrets or personal data in log output | N-A | The module neither logs nor prints. |
| 10 | Classes and operations trace to the Design Class Diagram they implement; deviations are recorded | Pass | No Design Class Diagram exists (a learning project of this size, recorded in the open issues of [PP-001]); the two functions trace to the first lecture and to the tasks of [MIL-002]. |
| 11 | Tests exist for new behaviour, are named for the behaviour, and do not depend on order or the network | Pass | `tests/test_csv_reader.py` has 8 tests named for behaviour (header included, temperatures as `int`, header skipped, small file in `tmp_path`, default path from another folder, `path` override, no `pandas` import in a fresh interpreter); none depends on order or the network; `pytest` reports 14 passed. |
| 12 | Type checker runs in strict mode without errors; `Any` is justified in a comment | Pass | `python -m mypy` runs with `strict = true`: no issues in 6 source files; no `Any`. |
| 13 | Dependencies are declared and pinned in the project's dependency file, none unused | Pass | The module imports only the standard library; `dependencies = []` is unchanged. |
| 14 | Each piece of knowledge (a business rule, constant, format, validation or query) is defined in one place; code is merged only where it expresses the same knowledge, not where it merely looks alike | Pass | The file name, the encoding and the temperature position are read from `constants.py`; the temperatures are read through `read_rows`, so the reading is written once. |
| 15 | Imports point inward: business-rule modules import no framework, database, UI or delivery-mechanism code, and there are no import cycles | Pass | `csv_reader` imports `csv`, `pathlib` and `weather.constants`; no import cycle. |
| 16 | SOLID holds: a class or module has one reason to change; new behaviour is added by extension, not by editing a type test in several places; subclasses honour the contract of their base; protocols are narrow; high-level code depends on protocols or abstract types and receives its implementations | Pass | Two small functions, each with one reason to change; no class hierarchy. |
## Overall Verdict
Go — all Mandatory criteria pass or are not applicable. Assessed on 2026-10-13 by the assistant that wrote the code, as stand-in for S01 in autonomous mode. Mode: autonomous, not independent. The pull request is the second look.
## Action Items
| Action | Owner | Due |
| --- | --- | --- |
| Challenge this `Go` in the run summary or on the pull request if the stand-in decision is not wanted | S01 | 2026-10-23 |
---
[MIL-002]: ../../milestones/mil-002-read-with-csv.md
[QC-PY-001]: ../../../framework/qc/code/qc-programming-python.md
[PP-001]: ../../project-plan.md
[ff6d1b2]: https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/025-weather/commit/ff6d1b29c58da4a83c5973a70d36dd5ee5283034
+35
View File
@@ -0,0 +1,35 @@
## @file csv_reader.py
# @brief Reads the weather data file with Python's built-in `csv` library.
#
# This is the first half of the first lecture: the rows are read with
# `csv.reader` and the temperatures are converted to whole numbers. The module
# needs no third-party dependency.
"""Reads the weather data file with the built-in csv library."""
import csv
from pathlib import Path
from weather.constants import DATA_FILE, FILE_ENCODING, TEMP_COLUMN_INDEX
def read_rows(path: Path | str = DATA_FILE) -> list[list[str]]:
"""! @brief Read every row of a CSV file, the header row included.
@param path Path of the CSV file; the weather data file by default.
@return The rows, each a list of strings, in file order.
"""
with open(path, newline="", encoding=FILE_ENCODING) as file:
return [row for row in csv.reader(file) if row]
def read_temperatures(path: Path | str = DATA_FILE) -> list[int]:
"""! @brief Read the temperatures of a weather CSV file as whole numbers.
The first row is the header and is skipped.
@param path Path of the CSV file; the weather data file by default.
@return The temperature of each data row, in file order.
"""
data_rows = read_rows(path)[1:]
return [int(row[TEMP_COLUMN_INDEX]) for row in data_rows]
+8
View File
@@ -0,0 +1,8 @@
day,temp,condition
Monday,12,Sunny
Tuesday,14,Rain
Wednesday,15,Rain
Thursday,14,Cloudy
Friday,21,Sunny
Saturday,22,Sunny
Sunday,24,Sunny
1 day temp condition
2 Monday 12 Sunny
3 Tuesday 14 Rain
4 Wednesday 15 Rain
5 Thursday 14 Cloudy
6 Friday 21 Sunny
7 Saturday 22 Sunny
8 Sunday 24 Sunny
+77
View File
@@ -0,0 +1,77 @@
"""Tests of the csv reader against the real data file and small files."""
import subprocess
import sys
from pathlib import Path
import pytest
from weather import csv_reader
from weather.constants import DATA_FILE
LECTURE_TEMPERATURES = [12, 14, 15, 14, 21, 22, 24]
@pytest.fixture
def small_csv(tmp_path: Path) -> Path:
file = tmp_path / "small.csv"
file.write_text("day,temp,condition\nMonday,3,Rain\nTuesday,-4,Snow\n")
return file
def test_read_rows_gives_header_first_and_eight_rows_for_the_real_file() -> None:
rows = csv_reader.read_rows()
assert rows[0] == ["day", "temp", "condition"]
assert len(rows) == 8
assert all(isinstance(value, str) for row in rows for value in row)
def test_read_temperatures_gives_the_lecture_values_for_the_real_file() -> None:
temperatures = csv_reader.read_temperatures()
assert temperatures == LECTURE_TEMPERATURES
assert all(isinstance(value, int) for value in temperatures)
def test_read_temperatures_skips_the_header() -> None:
assert len(csv_reader.read_temperatures()) == len(csv_reader.read_rows()) - 1
def test_read_temperatures_gives_the_values_of_a_small_file(small_csv: Path) -> None:
assert csv_reader.read_temperatures(small_csv) == [3, -4]
def test_read_rows_gives_the_rows_of_a_small_file(small_csv: Path) -> None:
assert csv_reader.read_rows(small_csv) == [
["day", "temp", "condition"],
["Monday", "3", "Rain"],
["Tuesday", "-4", "Snow"],
]
def test_default_path_works_from_another_working_directory(tmp_path: Path) -> None:
assert Path.cwd() == tmp_path
assert DATA_FILE.parent != Path.cwd()
assert csv_reader.read_temperatures() == LECTURE_TEMPERATURES
def test_path_argument_overrides_the_default_as_str_and_as_path(
small_csv: Path,
) -> None:
assert csv_reader.read_temperatures(small_csv) == [3, -4]
assert csv_reader.read_temperatures(str(small_csv)) == [3, -4]
def test_importing_csv_reader_does_not_import_pandas() -> None:
code = "import sys, weather.csv_reader; sys.exit('pandas' in sys.modules)"
result = subprocess.run(
[sys.executable, "-I", "-c", code],
capture_output=True,
text=True,
check=False,
)
assert result.returncode == 0, result.stderr