From b52a076c5bcaf8d9d21e97556b8bf857ecf7335b Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 5 Nov 2023 12:29:46 +0000 Subject: [PATCH] Split sunrise/sunset code into own file --- agenda/__init__.py | 27 ++++----------------------- agenda/sun.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 agenda/sun.py diff --git a/agenda/__init__.py b/agenda/__init__.py index 07698d8..97d4863 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -11,7 +11,6 @@ from typing import List import dateutil import dateutil.parser import dateutil.tz -import ephem import exchange_calendars import holidays import lxml @@ -24,7 +23,7 @@ from dateutil.relativedelta import FR, relativedelta from agenda import thespacedevs -from . import fx, gwr, markets, waste_schedule +from . import fx, gwr, markets, sun, waste_schedule from .types import Event warnings.simplefilter(action="ignore", category=FutureWarning) @@ -35,7 +34,6 @@ warnings.simplefilter(action="ignore", category=FutureWarning) # morzine ski lifts # chalet availablity calendar -# sunrise and sunset # starlink visible here = dateutil.tz.tzlocal() @@ -450,23 +448,6 @@ def bristol_waste_collection_events(start_date: date) -> list[Event]: return waste_schedule.get_bristol_gov_uk(start_date, data_dir, uprn) -def bristol() -> ephem.Observer: - """Location of Bristol.""" - observer = ephem.Observer() - observer.lat, observer.lon = "51.4545", "-2.5879" - return observer - - -def sunrise(observer: ephem.Observer) -> datetime: - """Sunrise.""" - return typing.cast(datetime, observer.next_rising(ephem.Sun(observer)).datetime()) - - -def sunset(observer: ephem.Observer) -> datetime: - """Sunrise.""" - return typing.cast(datetime, observer.next_setting(ephem.Sun(observer)).datetime()) - - def build_events_for_calendar(events: list[Event]) -> list[dict[str, typing.Any]]: """Build list of events for FullCalendar.""" items: list[dict[str, typing.Any]] = [] @@ -562,9 +543,9 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]: event = Event(name=key, date=value) events.append(event) - observer = bristol() - reply["sunrise"] = sunrise(observer) - reply["sunset"] = sunset(observer) + observer = sun.bristol() + reply["sunrise"] = sun.sunrise(observer) + reply["sunset"] = sun.sunset(observer) for key, value in xmas_last_posting_dates.items(): events.append(Event(name=f"xmas_last_{key}", date=value)) diff --git a/agenda/sun.py b/agenda/sun.py new file mode 100644 index 0000000..72adc69 --- /dev/null +++ b/agenda/sun.py @@ -0,0 +1,23 @@ +"""Sunrise and sunset times.""" + +import typing +from datetime import datetime + +import ephem + + +def bristol() -> ephem.Observer: + """Location of Bristol.""" + observer = ephem.Observer() + observer.lat, observer.lon = "51.4545", "-2.5879" + return observer + + +def sunrise(observer: ephem.Observer) -> datetime: + """Sunrise.""" + return typing.cast(datetime, observer.next_rising(ephem.Sun(observer)).datetime()) + + +def sunset(observer: ephem.Observer) -> datetime: + """Sunrise.""" + return typing.cast(datetime, observer.next_setting(ephem.Sun(observer)).datetime())