Skip to content

Commit

Permalink
Report times in future only
Browse files Browse the repository at this point in the history
  • Loading branch information
abhidg committed Sep 3, 2024
1 parent 7f28917 commit 3c5963b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
13 changes: 3 additions & 10 deletions sunblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import requests

CLEARISH_SKY_WMO_CODES = [0, 1]
NOW = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M")


class SunblockResult(NamedTuple):
Expand Down Expand Up @@ -52,15 +53,6 @@ def fetch(loc: Location) -> dict[str, Any] | None:


def fahrenheit_to_celsius(temp: pd.Series | float) -> pd.Series | float:
if isinstance(temp, float):
if math.isnan(temp):
raise ValueError("Temperature cannot be nan")
if math.isinf(temp):
raise ValueError("Temperature cannot be infinity")
if temp < -459.67:
raise ValueError(
"Temperature below minimum fahrenheit temperature at absolute zero"
)
return 5 * (temp - 32) / 9


Expand All @@ -87,7 +79,7 @@ def process(data: dict[str, Any] | None) -> pd.DataFrame | None:
)


def find_sun(data: pd.DataFrame, num_hours: int) -> SunblockResult:
def find_sun(data: pd.DataFrame, num_hours: int, now: str = NOW) -> SunblockResult:
"Finds sun for `num_hours` in the data, after `day_start`"
assert isinstance(num_hours, int), "num_hours should be an integer"
if num_hours < 1:
Expand All @@ -96,6 +88,7 @@ def find_sun(data: pd.DataFrame, num_hours: int) -> SunblockResult:
raise ValueError("Maximum sun block allowed is 23 hours")
sunny = (
(data.sunrise <= data.time)
& (data.time > now)
& (data.time <= data.sunset)
& (data.weather_code.isin(CLEARISH_SKY_WMO_CODES))
)
Expand Down
3 changes: 3 additions & 0 deletions sunblock_monolithic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sys
import pandas as pd
import datetime
import requests

CLEARISH_SKY_WMO_CODES = [0, 1]
Expand Down Expand Up @@ -54,13 +55,15 @@ def fahrenheit_to_celsius(temp):
}
)

NOW = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M")
assert (
isinstance(sunblock_hours, int) and 0 < sunblock_hours < 23
), "Number of hours must be between 0 and 23"

# Sunny when it is day time and the sky is clear or partly cloudy
sunny = (
(df.sunrise <= df.time)
& (df.time > NOW)
& (df.time <= df.sunset)
& (df.weather_code.isin(CLEARISH_SKY_WMO_CODES))
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def test_process(openmeteo_data):


def test_find_sun(openmeteo_data):
block = find_sun(openmeteo_data, num_hours=3)
assert_approx_equal(block.mean_temp_celsius, 19.916666666666668)
block = find_sun(openmeteo_data, num_hours=2, now="2024-08-22T21:00")
assert_approx_equal(block.mean_temp_celsius, 19.61111)
assert (block.num_hours, block.start, block.message) == (
3,
2,
datetime.datetime.fromisoformat("2024-08-23T11:00"),
"",
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# is equivalent to -273.15 degrees celsius.


@given(floats())
@given(floats(min_value=-450.0))
def test_fahrenheit_to_celsius(temp):
try:
assert fahrenheit_to_celsius(temp) > -273.15 # absolute zero
Expand All @@ -48,7 +48,7 @@ def test_fahrenheit_differs_from_celsius(temp):
# Test that a range of sunblock lengths does not crash the program
@given(num_hours=integers(min_value=1, max_value=23))
def test_find_sun(num_hours, openmeteo_data):
block = find_sun(openmeteo_data, num_hours)
block = find_sun(openmeteo_data, num_hours, now="2024-08-22T06:00")
if block.message != "No sunny interval found":
assert block.num_hours == num_hours
# This is not true everywhere of course, but should be true
Expand Down

0 comments on commit 3c5963b

Please sign in to comment.