From 6b878f95816966c0bc9789a02edb752f9cd6df95 Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Thu, 7 Dec 2023 14:17:49 +0100 Subject: [PATCH] Generate prices list for gas with example (#275) --- examples/prices_list.py | 25 ++++++++++++++++++++ src/energyzero/models.py | 49 ++++++++++++++++++++++++---------------- tests/test_models.py | 1 + 3 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 examples/prices_list.py diff --git a/examples/prices_list.py b/examples/prices_list.py new file mode 100644 index 0000000..19e7b1d --- /dev/null +++ b/examples/prices_list.py @@ -0,0 +1,25 @@ +"""Asynchronous Python client for the EnergyZero API.""" + +import asyncio +from datetime import date + +from energyzero import EnergyZero, VatOption + + +async def main() -> None: + """Show example on fetching the timestamp lists from EnergyZero.""" + async with EnergyZero(vat=VatOption.INCLUDE) as client: + today = date(2023, 12, 5) + energy = await client.energy_prices(start_date=today, end_date=today) + gas = await client.gas_prices(start_date=today, end_date=today) + + print("--- ENERGY ---") + print(energy.timestamp_prices) + print() + + print("--- GAS ---") + print(gas.timestamp_prices) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/src/energyzero/models.py b/src/energyzero/models.py index cfc1a91..251ee55 100644 --- a/src/energyzero/models.py +++ b/src/energyzero/models.py @@ -44,6 +44,24 @@ def _get_pricetime( return func(prices, key=prices.get) # type: ignore[call-arg] +def _generate_timestamp_list( + prices: dict[datetime, float], +) -> list[dict[str, float | datetime]]: + """Return a list of timestamps. + + Args: + ---- + prices: A dictionary with the hourprices. + + Returns: + ------- + A list of timestamps. + """ + return [ + {"timestamp": timestamp, "price": price} for timestamp, price in prices.items() + ] + + @dataclass class Electricity: """Object representing electricity data.""" @@ -110,7 +128,7 @@ def timestamp_prices(self) -> list[dict[str, float | datetime]]: ------- list of prices with timestamp """ - return self.generate_timestamp_list(self.prices) + return _generate_timestamp_list(self.prices) @property def hours_priced_equal_or_lower(self) -> int: @@ -132,25 +150,6 @@ def utcnow(self) -> datetime: """ return datetime.now(timezone.utc) - def generate_timestamp_list( - self, - prices: dict[datetime, float], - ) -> list[dict[str, float | datetime]]: - """Return a list of timestamps. - - Args: - ---- - prices: A dictionary with the hourprices. - - Returns: - ------- - A list of timestamps. - """ - return [ - {"timestamp": timestamp, "price": price} - for timestamp, price in prices.items() - ] - def price_at_time(self, moment: datetime) -> float | None: """Return the price at a specific time. @@ -220,6 +219,16 @@ def extreme_prices(self) -> tuple[float, float]: """ return min(self.prices.values()), max(self.prices.values()) + @property + def timestamp_prices(self) -> list[dict[str, float | datetime]]: + """Return a list of prices with timestamp. + + Returns + ------- + list of prices with timestamp + """ + return _generate_timestamp_list(self.prices) + def utcnow(self) -> datetime: """Return the current timestamp in the UTC timezone. diff --git a/tests/test_models.py b/tests/test_models.py index cf931f0..51a69ec 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -155,6 +155,7 @@ async def test_gas_model(aresponses: ResponsesMockServer) -> None: # The next hour price next_hour = datetime(2022, 12, 7, 15, 0, tzinfo=timezone.utc) assert gas.price_at_time(next_hour) == 1.47 + assert isinstance(gas.timestamp_prices, list) @pytest.mark.freeze_time("2022-12-07 04:00:00+01:00")