From 74d5f451d396a17d4783ce9ec91238ea22e61d5a Mon Sep 17 00:00:00 2001 From: "Joshua L. Adelman" Date: Fri, 1 Mar 2024 16:07:27 -0500 Subject: [PATCH 1/2] fix utcnow deprecation for python3.12 --- betfairlightweight/resources/baseresource.py | 6 ++++-- betfairlightweight/streaming/betfairstream.py | 4 +++- betfairlightweight/streaming/stream.py | 7 +++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/betfairlightweight/resources/baseresource.py b/betfairlightweight/resources/baseresource.py index dfe96200..cbcf6794 100644 --- a/betfairlightweight/resources/baseresource.py +++ b/betfairlightweight/resources/baseresource.py @@ -10,7 +10,7 @@ class BaseResource: def __init__(self, **kwargs): self.elapsed_time = kwargs.pop("elapsed_time", None) - now = datetime.datetime.utcnow() + now = datetime.datetime.now(tz=datetime.timezone.utc) self._datetime_created = now self._datetime_updated = now self._data = kwargs @@ -31,7 +31,9 @@ def strip_datetime(value: Union[str, int]) -> Optional[datetime.datetime]: return elif isinstance(value, integer_types): try: - return datetime.datetime.utcfromtimestamp(value / 1e3) + return datetime.datetime.fromtimestamp( + value / 1e3, tz=datetime.timezone.utc + ) except (ValueError, OverflowError, OSError): return diff --git a/betfairlightweight/streaming/betfairstream.py b/betfairlightweight/streaming/betfairstream.py index 2eae11e7..ebff3c62 100644 --- a/betfairlightweight/streaming/betfairstream.py +++ b/betfairlightweight/streaming/betfairstream.py @@ -225,7 +225,9 @@ def _read_loop(self) -> None: received_data_raw = self._receive_all() if self._running: self.receive_count += 1 - self.datetime_last_received = datetime.datetime.utcnow() + self.datetime_last_received = datetime.datetime.now( + tz=datetime.timezone.utc + ) received_data_split = received_data_raw.split(self.__CRLF) for received_data in received_data_split: if received_data: diff --git a/betfairlightweight/streaming/stream.py b/betfairlightweight/streaming/stream.py index fbba55ba..0fbecb0f 100644 --- a/betfairlightweight/streaming/stream.py +++ b/betfairlightweight/streaming/stream.py @@ -33,8 +33,8 @@ def __init__(self, listener: object, unique_id: int): self._updates_processed = 0 self._on_creation() - self.time_created = datetime.datetime.utcnow() - self.time_updated = datetime.datetime.utcnow() + self.time_created = datetime.datetime.now(tz=datetime.timezone.utc) + self.time_updated = datetime.datetime.now(tz=datetime.timezone.utc) def on_subscribe(self, data: dict) -> None: self._update_clk(data) @@ -147,7 +147,7 @@ def _update_clk(self, data: dict) -> None: self._initial_clk = initial_clk if clk: self._clk = clk - self.time_updated = datetime.datetime.utcnow() + self.time_updated = datetime.datetime.now(tz=datetime.timezone.utc) @staticmethod def _calc_latency(publish_time: int) -> float: @@ -246,7 +246,6 @@ def _process(self, data: list, publish_time: int) -> bool: class RaceStream(BaseStream): - """ Cache contains latest update: marketId: RaceCache From c9ebe908d0a5489d6bc13c8b717fbc9b20efdb03 Mon Sep 17 00:00:00 2001 From: "Joshua L. Adelman" Date: Tue, 5 Mar 2024 13:59:59 -0500 Subject: [PATCH 2/2] make utcnow and utcfromtimestamp utility functions --- betfairlightweight/resources/baseresource.py | 7 +++---- betfairlightweight/streaming/betfairstream.py | 5 ++--- betfairlightweight/streaming/stream.py | 7 ++++--- betfairlightweight/utils.py | 11 +++++++++++ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/betfairlightweight/resources/baseresource.py b/betfairlightweight/resources/baseresource.py index cbcf6794..4ea958b6 100644 --- a/betfairlightweight/resources/baseresource.py +++ b/betfairlightweight/resources/baseresource.py @@ -3,6 +3,7 @@ from typing import Union, Optional from ..compat import basestring, integer_types, json, parse_datetime +from ..utils import utcfromtimestamp, utcnow class BaseResource: @@ -10,7 +11,7 @@ class BaseResource: def __init__(self, **kwargs): self.elapsed_time = kwargs.pop("elapsed_time", None) - now = datetime.datetime.now(tz=datetime.timezone.utc) + now = utcnow() self._datetime_created = now self._datetime_updated = now self._data = kwargs @@ -31,9 +32,7 @@ def strip_datetime(value: Union[str, int]) -> Optional[datetime.datetime]: return elif isinstance(value, integer_types): try: - return datetime.datetime.fromtimestamp( - value / 1e3, tz=datetime.timezone.utc - ) + return utcfromtimestamp(value / 1e3) except (ValueError, OverflowError, OSError): return diff --git a/betfairlightweight/streaming/betfairstream.py b/betfairlightweight/streaming/betfairstream.py index ebff3c62..7a9c84e6 100644 --- a/betfairlightweight/streaming/betfairstream.py +++ b/betfairlightweight/streaming/betfairstream.py @@ -7,6 +7,7 @@ from ..exceptions import SocketError, ListenerError from ..compat import json +from ..utils import utcnow from .listener import BaseListener logger = logging.getLogger(__name__) @@ -225,9 +226,7 @@ def _read_loop(self) -> None: received_data_raw = self._receive_all() if self._running: self.receive_count += 1 - self.datetime_last_received = datetime.datetime.now( - tz=datetime.timezone.utc - ) + self.datetime_last_received = utcnow() received_data_split = received_data_raw.split(self.__CRLF) for received_data in received_data_split: if received_data: diff --git a/betfairlightweight/streaming/stream.py b/betfairlightweight/streaming/stream.py index 0fbecb0f..7460a7da 100644 --- a/betfairlightweight/streaming/stream.py +++ b/betfairlightweight/streaming/stream.py @@ -3,6 +3,7 @@ import time from typing import Optional +from ..utils import utcnow from .cache import CricketMatchCache, MarketBookCache, OrderBookCache, RaceCache logger = logging.getLogger(__name__) @@ -33,8 +34,8 @@ def __init__(self, listener: object, unique_id: int): self._updates_processed = 0 self._on_creation() - self.time_created = datetime.datetime.now(tz=datetime.timezone.utc) - self.time_updated = datetime.datetime.now(tz=datetime.timezone.utc) + self.time_created = utcnow() + self.time_updated = utcnow() def on_subscribe(self, data: dict) -> None: self._update_clk(data) @@ -147,7 +148,7 @@ def _update_clk(self, data: dict) -> None: self._initial_clk = initial_clk if clk: self._clk = clk - self.time_updated = datetime.datetime.now(tz=datetime.timezone.utc) + self.time_updated = utcnow() @staticmethod def _calc_latency(publish_time: int) -> float: diff --git a/betfairlightweight/utils.py b/betfairlightweight/utils.py index cffb8eec..4e77d14f 100644 --- a/betfairlightweight/utils.py +++ b/betfairlightweight/utils.py @@ -88,3 +88,14 @@ def create_date_string(date: datetime.datetime) -> Optional[str]: """ if date: return date.strftime(BETFAIR_DATE_FORMAT) + + +def utcnow(): + """return timezone naive now""" + return datetime.datetime.now(tz=datetime.timezone.utc).replace(tzinfo=None) + + +def utcfromtimestamp(ts): + return datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc).replace( + tzinfo=None + )