From f63b4d6b083988afdf09aa72202b58961552cd52 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 18 Jul 2024 14:22:55 +0900 Subject: [PATCH 1/3] tell mypy to ignore the lack of types for ephem module --- agenda/sun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenda/sun.py b/agenda/sun.py index 72adc69..17995bb 100644 --- a/agenda/sun.py +++ b/agenda/sun.py @@ -3,7 +3,7 @@ import typing from datetime import datetime -import ephem +import ephem # type: ignore def bristol() -> ephem.Observer: From 5b9a481bb2f3d83c68a585d81ec999d5be19b464 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 18 Jul 2024 14:24:14 +0900 Subject: [PATCH 2/3] move time_function to utils --- agenda/data.py | 17 +---------------- agenda/utils.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index ae050bd..db320e3 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -36,6 +36,7 @@ from . import ( uk_holiday, ) from .types import Event, StrDict +from .utils import time_function here = dateutil.tz.tzlocal() @@ -94,22 +95,6 @@ def find_events_during_stay( return overlapping_markets -async def time_function( - name: str, - func: typing.Callable[..., typing.Coroutine[typing.Any, typing.Any, typing.Any]], - *args: typing.Any, - **kwargs: typing.Any, -) -> tuple[str, typing.Any, float, Exception | None]: - """Time the execution of an asynchronous function.""" - start_time, result, exception = time(), None, None - try: - result = await func(*args, **kwargs) - except Exception as e: - exception = e - end_time = time() - return name, result, end_time - start_time, exception - - def hide_markets_while_away( events: list[Event], accommodation_events: list[Event] ) -> None: diff --git a/agenda/utils.py b/agenda/utils.py index 00a4e50..656a60f 100644 --- a/agenda/utils.py +++ b/agenda/utils.py @@ -1,7 +1,9 @@ """Utility functions.""" import os +import typing from datetime import date, datetime, timezone +from time import time def as_date(d: datetime | date) -> date: @@ -84,3 +86,19 @@ def make_waste_dir(data_dir: str) -> None: waste_dir = os.path.join(data_dir, "waste") if not os.path.exists(waste_dir): os.mkdir(waste_dir) + + +async def time_function( + name: str, + func: typing.Callable[..., typing.Coroutine[typing.Any, typing.Any, typing.Any]], + *args: typing.Any, + **kwargs: typing.Any, +) -> tuple[str, typing.Any, float, Exception | None]: + """Time the execution of an asynchronous function.""" + start_time, result, exception = time(), None, None + try: + result = await func(*args, **kwargs) + except Exception as e: + exception = e + end_time = time() + return name, result, end_time - start_time, exception From 9f54c3ac038f8aa573f954b98019dd2d6351abf9 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 18 Jul 2024 14:24:44 +0900 Subject: [PATCH 3/3] rename n_somerset_waste_collection_events function --- agenda/data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index db320e3..a90f697 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -60,7 +60,7 @@ def timezone_transition( ] -async def waste_collection_events( +async def n_somerset_waste_collection_events( data_dir: str, postcode: str, uprn: str ) -> list[Event]: """Waste colllection events.""" @@ -152,7 +152,7 @@ async def get_data(now: datetime, config: flask.config.Config) -> AgendaData: time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir), time_function( "backwell_bins", - waste_collection_events, + n_somerset_waste_collection_events, data_dir, config["BACKWELL_POSTCODE"], config["BACKWELL_UPRN"],