Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix utcnow deprecation for python3.12 #562

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions betfairlightweight/resources/baseresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from typing import Union, Optional

from ..compat import basestring, integer_types, json, parse_datetime
from ..utils import utcfromtimestamp, utcnow


class BaseResource:
"""Lightweight data structure for resources."""

def __init__(self, **kwargs):
self.elapsed_time = kwargs.pop("elapsed_time", None)
now = datetime.datetime.utcnow()
now = utcnow()
self._datetime_created = now
self._datetime_updated = now
self._data = kwargs
Expand All @@ -31,7 +32,7 @@ def strip_datetime(value: Union[str, int]) -> Optional[datetime.datetime]:
return
elif isinstance(value, integer_types):
try:
return datetime.datetime.utcfromtimestamp(value / 1e3)
return utcfromtimestamp(value / 1e3)
except (ValueError, OverflowError, OSError):
return

Expand Down
3 changes: 2 additions & 1 deletion betfairlightweight/streaming/betfairstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -225,7 +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.utcnow()
self.datetime_last_received = utcnow()
received_data_split = received_data_raw.split(self.__CRLF)
for received_data in received_data_split:
if received_data:
Expand Down
8 changes: 4 additions & 4 deletions betfairlightweight/streaming/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -33,8 +34,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 = utcnow()
self.time_updated = utcnow()

def on_subscribe(self, data: dict) -> None:
self._update_clk(data)
Expand Down Expand Up @@ -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.utcnow()
self.time_updated = utcnow()

@staticmethod
def _calc_latency(publish_time: int) -> float:
Expand Down Expand Up @@ -246,7 +247,6 @@ def _process(self, data: list, publish_time: int) -> bool:


class RaceStream(BaseStream):

"""
Cache contains latest update:
marketId: RaceCache
Expand Down
11 changes: 11 additions & 0 deletions betfairlightweight/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Loading