-
Notifications
You must be signed in to change notification settings - Fork 9
Pavel Tisner #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pavel-tisner
wants to merge
3
commits into
HackYourAssignment:main
Choose a base branch
from
pavel-tisner:week5-attempt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pavel Tisner #14
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| station,timestamp,temperature_c,humidity_pct | ||
| Copenhagen,2025-01-15T10:00,18.5,72 | ||
| ,2025-01-15T11:00,20.1,65 | ||
| Aarhus,2025-01-15T12:00,N/A,58 | ||
| Odense,2025-01-15T13:00,15.2,150 | ||
| Copenhagen,2025-01-15T10:00,19.0,70 | ||
| Aalborg,,14.8,62 | ||
| Roskilde,2025-01-15T15:00,-95.0,45 | ||
| Esbjerg,2025-01-15T16:00,16.3, | ||
| Herning,2025-01-15T17:00,17.1,55 | ||
| Kolding,2025-01-15T18:00,14.9,68 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,4 @@ | ||
| # Task 2: Pin every dependency your pipeline uses. | ||
| # | ||
| # Format: package==version | ||
| # Example: requests==2.31.0 | ||
| # | ||
| # Find the current version of any package: | ||
| # pip show <package> | ||
| # | ||
| # Always include pytest and ruff: | ||
| # pytest== | ||
| # ruff== | ||
| # | ||
| # Add your pinned dependencies below: | ||
| pydantic==2.13.4 | ||
| pytest==9.0.3 | ||
| requests==2.34.2 | ||
| ruff==0.15.15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # Step 5 — Task 5: Database Storage | ||
| # create_tables() — run once at startup to set up raw_weather and weather_readings. | ||
| # insert_raw() — store every record before validation so nothing is lost. | ||
| # upsert_readings()— insert valid records; ON CONFLICT updates instead of duplicating. | ||
| # count_readings() — query the final row count for the pipeline summary. | ||
| import sqlite3 | ||
| from pathlib import Path | ||
|
|
||
| from src.models import WeatherReading | ||
|
|
||
| DB_PATH = Path("weather.db") | ||
|
|
||
|
|
||
| def get_connection() -> sqlite3.Connection: | ||
| conn = sqlite3.connect(DB_PATH) | ||
| conn.row_factory = sqlite3.Row | ||
| return conn | ||
|
|
||
|
|
||
| def create_tables(conn: sqlite3.Connection) -> None: | ||
| """Create raw_weather and weather_readings tables if they do not exist. | ||
|
|
||
| raw_weather columns: id, station, timestamp, temperature_c, humidity_pct, source, ingested_at | ||
| weather_readings columns: id, station, timestamp, temperature_c, humidity_pct | ||
| + UNIQUE(station, timestamp) constraint for upserts | ||
| """ | ||
|
|
||
| conn.execute( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS raw_weather ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| station TEXT, | ||
| timestamp TEXT, | ||
| temperature_c TEXT, | ||
| humidity_pct TEXT, | ||
| source TEXT NOT NULL, | ||
| ingested_at TEXT DEFAULT CURRENT_TIMESTAMP | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| conn.execute( | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS weather_readings ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| station TEXT NOT NULL, | ||
| timestamp TEXT NOT NULL, | ||
| temperature_c REAL NOT NULL, | ||
| humidity_pct INTEGER NOT NULL, | ||
| UNIQUE(station, timestamp) | ||
| ) | ||
| """ | ||
| ) | ||
|
|
||
| conn.commit() | ||
|
|
||
|
|
||
| def insert_raw(conn: sqlite3.Connection, records: list[dict], source: str) -> None: | ||
| """Insert raw records (before validation) into raw_weather. | ||
|
|
||
| Use parameterized queries with placeholder syntax; do not build SQL via string formatting. | ||
| """ | ||
|
|
||
| rows = [ | ||
| ( | ||
| record.get("station"), | ||
| record.get("timestamp"), | ||
| record.get("temperature_c"), | ||
| record.get("humidity_pct"), | ||
| source, | ||
| ) | ||
| for record in records | ||
| ] | ||
|
|
||
| conn.executemany( | ||
| """ | ||
| INSERT INTO raw_weather ( | ||
| station, | ||
| timestamp, | ||
| temperature_c, | ||
| humidity_pct, | ||
| source | ||
| ) | ||
| VALUES (?, ?, ?, ?, ?) | ||
| """, | ||
| rows, | ||
| ) | ||
|
|
||
| conn.commit() | ||
|
|
||
|
|
||
| def upsert_readings(conn: sqlite3.Connection, readings: list[WeatherReading]) -> None: | ||
| """Upsert valid WeatherReading objects into weather_readings. | ||
|
|
||
| Use the upsert pattern to handle duplicate (station, timestamp) pairs. | ||
| Use parameterized queries. | ||
| """ | ||
|
|
||
| rows = [ | ||
| ( | ||
| reading.station, | ||
| reading.timestamp, | ||
| reading.temperature_c, | ||
| reading.humidity_pct, | ||
| ) | ||
| for reading in readings | ||
| ] | ||
|
|
||
| conn.executemany( | ||
| """ | ||
| INSERT INTO weather_readings ( | ||
| station, | ||
| timestamp, | ||
| temperature_c, | ||
| humidity_pct | ||
| ) | ||
| VALUES (?, ?, ?, ?) | ||
| ON CONFLICT(station, timestamp) | ||
| DO UPDATE SET | ||
| temperature_c = excluded.temperature_c, | ||
| humidity_pct = excluded.humidity_pct | ||
| """, | ||
| rows, | ||
| ) | ||
|
|
||
| conn.commit() | ||
|
|
||
|
|
||
| def count_readings(conn: sqlite3.Connection) -> int: | ||
| """Return the total number of rows in weather_readings.""" | ||
|
|
||
| cursor = conn.execute("SELECT COUNT(*) FROM weather_readings") | ||
| result = cursor.fetchone() | ||
|
|
||
| return result[0] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the image depend on local bundled data. That is okay for a demo, but for a shippable pipeline it is often better to pass input paths or mount data at runtime, so the image stays reusable across environments