From 7b3b90566d184322d30e5ef87d31e9aaaaae4684 Mon Sep 17 00:00:00 2001 From: wslaghekke Date: Thu, 14 Nov 2024 12:06:53 +0000 Subject: [PATCH 1/3] Use aiozoneinfo to avoid blocking io inside ZoneInfo --- poetry.lock | 29 +++++++++++++++++++++++++++-- pyproject.toml | 1 + src/twentemilieu/twentemilieu.py | 4 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/poetry.lock b/poetry.lock index 44f21857..3d83394a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. [[package]] name = "aiohappyeyeballs" @@ -122,6 +122,20 @@ files = [ [package.dependencies] frozenlist = ">=1.1.0" +[[package]] +name = "aiozoneinfo" +version = "0.2.1" +description = "Tools to fetch zoneinfo with asyncio" +optional = false +python-versions = "<4.0,>=3.8" +files = [ + {file = "aiozoneinfo-0.2.1-py3-none-any.whl", hash = "sha256:04579f855f030cd0edb1758659c513142ef1aaf7fcc97b59eb2262ed0c453cce"}, + {file = "aiozoneinfo-0.2.1.tar.gz", hash = "sha256:457e2c665a2c7e093119efb87cc5e0da29e6f59aac504a544bec822c5be1cb6b"}, +] + +[package.dependencies] +tzdata = ">=2024.1" + [[package]] name = "annotated-types" version = "0.7.0" @@ -1904,6 +1918,17 @@ files = [ {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, ] +[[package]] +name = "tzdata" +version = "2024.2" +description = "Provider of IANA time zone data" +optional = false +python-versions = ">=2" +files = [ + {file = "tzdata-2024.2-py2.py3-none-any.whl", hash = "sha256:a48093786cdcde33cad18c2555e8532f34422074448fbc874186f0abd79565cd"}, + {file = "tzdata-2024.2.tar.gz", hash = "sha256:7d85cc416e9382e69095b7bdf4afd9e3880418a2413feec7069d533d6b4e31cc"}, +] + [[package]] name = "urllib3" version = "2.2.3" @@ -2058,4 +2083,4 @@ propcache = ">=0.2.0" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "c31ea233bec4cf4ef79ff625b1150e5c853a7d6923f06e610084a4e3f5e4185d" +content-hash = "4e11b958f245b28b2c2f1d59dcd98ab3b85d770b6e08d591870a08d83c816a84" diff --git a/pyproject.toml b/pyproject.toml index e2155e5e..78160aa2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ version = "0.0.0" aiohttp = ">=3.0.0" python = "^3.11" yarl = ">=1.6.0" +aiozoneinfo = "^0.2.1" [tool.poetry.dev-dependencies] aresponses = "3.0.0" diff --git a/src/twentemilieu/twentemilieu.py b/src/twentemilieu/twentemilieu.py index e39675c3..9ad354a1 100644 --- a/src/twentemilieu/twentemilieu.py +++ b/src/twentemilieu/twentemilieu.py @@ -12,9 +12,9 @@ import aiohttp from aiohttp.client import ClientSession +from aiozoneinfo import async_get_time_zone from attr import dataclass from yarl import URL -from zoneinfo import ZoneInfo from .exceptions import ( TwenteMilieuAddressError, @@ -169,7 +169,7 @@ async def update(self) -> dict[WasteType, list[date]]: """ await self.unique_id() - timezone = ZoneInfo("Europe/Amsterdam") + timezone = await async_get_time_zone("Europe/Amsterdam") response = await self._request( "GetCalendar", data={ From 6801b1d85434454cd597bb5cbdbf5f64d3226479 Mon Sep 17 00:00:00 2001 From: wslaghekke Date: Fri, 15 Nov 2024 11:14:27 +0000 Subject: [PATCH 2/3] Instantiate ZoneInfo on construct so we dont need aiozoneinfo anymore --- poetry.lock | 27 +-------------------------- pyproject.toml | 1 - src/twentemilieu/twentemilieu.py | 10 +++++----- 3 files changed, 6 insertions(+), 32 deletions(-) diff --git a/poetry.lock b/poetry.lock index 3d83394a..b5834de3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -122,20 +122,6 @@ files = [ [package.dependencies] frozenlist = ">=1.1.0" -[[package]] -name = "aiozoneinfo" -version = "0.2.1" -description = "Tools to fetch zoneinfo with asyncio" -optional = false -python-versions = "<4.0,>=3.8" -files = [ - {file = "aiozoneinfo-0.2.1-py3-none-any.whl", hash = "sha256:04579f855f030cd0edb1758659c513142ef1aaf7fcc97b59eb2262ed0c453cce"}, - {file = "aiozoneinfo-0.2.1.tar.gz", hash = "sha256:457e2c665a2c7e093119efb87cc5e0da29e6f59aac504a544bec822c5be1cb6b"}, -] - -[package.dependencies] -tzdata = ">=2024.1" - [[package]] name = "annotated-types" version = "0.7.0" @@ -1918,17 +1904,6 @@ files = [ {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, ] -[[package]] -name = "tzdata" -version = "2024.2" -description = "Provider of IANA time zone data" -optional = false -python-versions = ">=2" -files = [ - {file = "tzdata-2024.2-py2.py3-none-any.whl", hash = "sha256:a48093786cdcde33cad18c2555e8532f34422074448fbc874186f0abd79565cd"}, - {file = "tzdata-2024.2.tar.gz", hash = "sha256:7d85cc416e9382e69095b7bdf4afd9e3880418a2413feec7069d533d6b4e31cc"}, -] - [[package]] name = "urllib3" version = "2.2.3" @@ -2083,4 +2058,4 @@ propcache = ">=0.2.0" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "4e11b958f245b28b2c2f1d59dcd98ab3b85d770b6e08d591870a08d83c816a84" +content-hash = "c31ea233bec4cf4ef79ff625b1150e5c853a7d6923f06e610084a4e3f5e4185d" diff --git a/pyproject.toml b/pyproject.toml index 78160aa2..e2155e5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,6 @@ version = "0.0.0" aiohttp = ">=3.0.0" python = "^3.11" yarl = ">=1.6.0" -aiozoneinfo = "^0.2.1" [tool.poetry.dev-dependencies] aresponses = "3.0.0" diff --git a/src/twentemilieu/twentemilieu.py b/src/twentemilieu/twentemilieu.py index 9ad354a1..40ca41c1 100644 --- a/src/twentemilieu/twentemilieu.py +++ b/src/twentemilieu/twentemilieu.py @@ -12,9 +12,9 @@ import aiohttp from aiohttp.client import ClientSession -from aiozoneinfo import async_get_time_zone from attr import dataclass from yarl import URL +from zoneinfo import ZoneInfo from .exceptions import ( TwenteMilieuAddressError, @@ -42,6 +42,7 @@ class TwenteMilieu: company_code: str = "8d97bb56-5afd-4cbc-a651-b4f7314264b4" api_host: str = "twentemilieuapi.ximmio.com" + timezone = ZoneInfo("Europe/Amsterdam") house_letter: str | int = "" request_timeout: int = 10 session: ClientSession | None = None @@ -169,16 +170,15 @@ async def update(self) -> dict[WasteType, list[date]]: """ await self.unique_id() - timezone = await async_get_time_zone("Europe/Amsterdam") response = await self._request( "GetCalendar", data={ "companyCode": self.company_code, "uniqueAddressID": self._unique_id, - "startDate": (datetime.now(tz=timezone) - timedelta(days=1)) + "startDate": (datetime.now(tz=self.timezone) - timedelta(days=1)) .date() .isoformat(), - "endDate": (datetime.now(tz=timezone) + timedelta(days=365)) + "endDate": (datetime.now(tz=self.timezone) + timedelta(days=365)) .date() .isoformat(), }, @@ -197,7 +197,7 @@ async def update(self) -> dict[WasteType, list[date]]: pickup_date_raw, "%Y-%m-%dT%H:%M:%S", ) - .replace(tzinfo=timezone) + .replace(tzinfo=self.timezone) .date() ) pickups[waste_type].append(pickup_date) From dd381096fa4dfb6a1283ecbc40bd2f78f796aadb Mon Sep 17 00:00:00 2001 From: wslaghekke Date: Fri, 15 Nov 2024 12:47:54 +0000 Subject: [PATCH 3/3] Instantiate ZoneInfo on construct so we dont need aiozoneinfo anymore --- src/twentemilieu/twentemilieu.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/twentemilieu/twentemilieu.py b/src/twentemilieu/twentemilieu.py index 40ca41c1..f3f4c731 100644 --- a/src/twentemilieu/twentemilieu.py +++ b/src/twentemilieu/twentemilieu.py @@ -22,6 +22,8 @@ TwenteMilieuError, ) +TIMEZONE_INFO = ZoneInfo("Europe/Amsterdam") + class WasteType(IntEnum): """Enum for the types of waste.""" @@ -42,7 +44,6 @@ class TwenteMilieu: company_code: str = "8d97bb56-5afd-4cbc-a651-b4f7314264b4" api_host: str = "twentemilieuapi.ximmio.com" - timezone = ZoneInfo("Europe/Amsterdam") house_letter: str | int = "" request_timeout: int = 10 session: ClientSession | None = None @@ -175,10 +176,10 @@ async def update(self) -> dict[WasteType, list[date]]: data={ "companyCode": self.company_code, "uniqueAddressID": self._unique_id, - "startDate": (datetime.now(tz=self.timezone) - timedelta(days=1)) + "startDate": (datetime.now(tz=TIMEZONE_INFO) - timedelta(days=1)) .date() .isoformat(), - "endDate": (datetime.now(tz=self.timezone) + timedelta(days=365)) + "endDate": (datetime.now(tz=TIMEZONE_INFO) + timedelta(days=365)) .date() .isoformat(), }, @@ -197,7 +198,7 @@ async def update(self) -> dict[WasteType, list[date]]: pickup_date_raw, "%Y-%m-%dT%H:%M:%S", ) - .replace(tzinfo=self.timezone) + .replace(tzinfo=TIMEZONE_INFO) .date() ) pickups[waste_type].append(pickup_date)