Weather Utilities (Python)
This repository contains a small, dependency-free Python module for parsing simple daily weather records from CSV and producing human-readable summaries. It’s designed to be easy to reason about, testable end-to-end, and straightforward to extend.
Why it matters
- Focuses on composable, pure functions to simplify reasoning and testing.
- Clear separation between data access/formatting and core logic.
- Zero third-party dependencies for portability and reliability.
Repository Structure
- weather.py: Core, stateless functions for parsing, conversions, and summary generation.
- print_weather.py: Minimal example script for rendering results from the core module.
- tests: Unit tests, sample inputs under tests/data, and golden outputs under tests/expected_output.
Public API (core functions)
- convert_f_to_c(value_f): Fahrenheit to Celsius conversion.
- calculate_mean(values): Numeric mean for an iterable of numbers.
- find_min(values), find_max(values): Helpers used by summaries to identify extrema.
- convert_date(iso_string): Converts an ISO-like date string into a human-readable form.
- load_data_from_csv(path): Loads daily weather records from CSV into in-memory structures.
- generate_summary(data): Produces an overall summary string across the dataset.
- generate_daily_summary(data): Produces a per-day summary string across the dataset.
These functions are intentionally small, deterministic, and composable. Business formatting (e.g., user-facing strings) is centralized in the summary functions, while numeric operations and parsing are kept isolated and reusable.
Data Contract
- Inputs: Simple CSV files with daily readings (see examples in tests/data).
- Outputs: Human-readable summary strings (see expected results in tests/expected_output).
- Error handling: Non-numeric or malformed rows are safely skipped or normalized during load; downstream functions operate on already-sanitised inputs.
Design Notes
- Purity and isolation: Most functions are pure (no I/O), enabling targeted unit tests and easy reuse.
- Cohesion: Numeric helpers (mean, min, max, conversions) are decoupled from formatting.
- Extensibility: New metrics (e.g., median, range), units, or output formats can be added without touching existing logic.
- Testability: Golden-file style tests document the contract and prevent regressions.
Complexity and Performance
- Parsing is linear in the number of rows, O(n).
- Summary generation is linear in the number of daily records, O(n).
- Memory footprint is proportional to the number of rows parsed.
Getting Started
- Ensure Python 3.10+ is available on your PATH.
- Run the test suite:
python -m unittest tests/*.py- Explore the example inputs and expected outputs:
- Inputs: tests/data
- Expected outputs: tests/expected_output
- Optional: Inspect or adapt the minimal runner in print_weather.py.
Extension Ideas
- Add aggregation windows (weekly/monthly) or additional statistics (median, stdev).
- Support alternate input formats (JSON, NDJSON) behind a parsing interface.
- Provide multiple renderers (plain text, Markdown) via strategy pattern.
- Add a lightweight CLI with subcommands for “summary” and “daily-summary”.
Notes for Reviewers
- The tests act as executable documentation of the expected behavior and string formatting.
- The API surface is intentionally small and focused on clarity over cleverness.