Skip to content

Commit

Permalink
Merge pull request #564 from betcode-org/release/2.20.0
Browse files Browse the repository at this point in the history
history / version
  • Loading branch information
liampauling authored Mar 11, 2024
2 parents 9dfed6a + c5b2178 commit 973c1f3
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 81 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
Expand All @@ -21,10 +21,6 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Check setup.py
run: |
python setup.py check
- name: Install dependencies
run: |
pip install -r requirements.txt
Expand All @@ -47,7 +43,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -89,7 +85,7 @@ jobs:
- name: Build package and docs
run: |
python setup.py sdist bdist_wheel
python -m build
twine check dist/*
mkdocs build
Expand Down
16 changes: 16 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
Release History
---------------

2.20.0 (2024-03-11)
+++++++++++++++++++

**Improvements**

- #550 update create socket for py3.12
- #562 Fix utcnow deprecation for python3.12 (@synapticarbors)
- toml file added

**Dependencies**

- python 3.12 added
- ciso8601 upgraded to 2.3.1
- orjson upgraded to 3.9.13
- black upgraded to 24.2.0

2.19.1 (2023-11-06)
+++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Lightweight, super fast (uses C and Rust libraries) pythonic wrapper for [Betfai

[join betcode slack group](https://join.slack.com/t/betcode-org/shared_invite/zt-25yz6dt1y-LHya5VzHLOzN3RZEQrSnrA)

Currently tested on Python 3.8, 3.9, 3.10 and 3.11.
Currently tested on Python 3.8, 3.9, 3.10, 3.11 and 3.12.

# installation

Expand Down
2 changes: 1 addition & 1 deletion betfairlightweight/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = "betfairlightweight"
__description__ = "Lightweight python wrapper for Betfair API-NG"
__url__ = "https://github.com/betcode-org/betfair"
__version__ = "2.19.1"
__version__ = "2.20.0"
__author__ = "Liam Pauling"
__license__ = "MIT"
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
4 changes: 2 additions & 2 deletions betfairlightweight/resources/racecardresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def __init__(
surface=None,
ratingSymbol=None,
ratingvalue=None,
**kwargs
**kwargs,
):
self.racing_type = racingType
self.surface = surface
Expand Down Expand Up @@ -303,7 +303,7 @@ def __init__(
preRaceMasterRating=None,
preRaceWeightAdjustedMasterRating=None,
seasonMasterRating=None,
**kwargs
**kwargs,
):
self.age = age
self.comment = comment
Expand Down
10 changes: 7 additions & 3 deletions 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 @@ -211,8 +212,11 @@ def _create_socket(self) -> socket.socket:
"""Creates ssl socket, connects to stream api and
sets timeout.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(s)
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.verify_mode = ssl.CERT_NONE
s = context.wrap_socket(
socket.socket(socket.AF_INET, socket.SOCK_STREAM), server_hostname=self.host
)
s.settimeout(self.timeout)
s.connect((self.host, self.__port))
return s
Expand All @@ -225,7 +229,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
10 changes: 6 additions & 4 deletions betfairlightweight/streaming/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def __init__(
rc: str = None,
lsrc: str = None,
cd: int = None,
**kwargs
**kwargs,
):
self.publish_time = publish_time
self.bet_id = id
Expand Down Expand Up @@ -487,9 +487,11 @@ def serialise(
"marketId": market_id,
"matchedDate": self._matched_date_string,
"orderType": StreamingOrderType[self.order_type].value,
"persistenceType": StreamingPersistenceType[self.persistence_type].value
if self.persistence_type
else None,
"persistenceType": (
StreamingPersistenceType[self.persistence_type].value
if self.persistence_type
else None
),
"placedDate": self._placed_date_string,
"priceSize": {"price": self.price, "size": self.size},
"regulatorAuthCode": self.regulator_auth_code,
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
)
50 changes: 50 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "betfairlightweight"
description = "Lightweight python wrapper for Betfair API-NG"
readme = "README.md"
license = {file = "LICENSE"}
requires-python = ">=3.8"
authors = [
{ name = "Liam Pauling", email = "[email protected]" },
]
classifiers = [
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"requests<2.32.0",
]
dynamic = ["version"]

[project.optional-dependencies]
speed = [
"ciso8601==2.3.1",
"orjson==3.9.13",
]
test = [
"black==24.2.0",
"coverage",
"mkdocs",
"mkdocs-material",
"twine",
"wheel",
]

[project.urls]
Homepage = "https://github.com/betcode-org"
Documentation = "https://betcode-org.github.io/betfair/"
Repository = "https://github.com/betcode-org/betfair.git"
"Bug Tracker" = "https://github.com/betcode-org/betfair/issues"
Changelog = "https://github.com/betcode-org/betfair/blob/master/HISTORY.rst"

[tool.hatch.version]
path = "betfairlightweight/__version__.py"
4 changes: 2 additions & 2 deletions requirements-speed.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ciso8601==2.3.0
orjson==3.9.9
ciso8601==2.3.1
orjson==3.9.13
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Tests & Linting
black==23.10.0
black==24.2.0
coverage

# Documentation
Expand Down
50 changes: 0 additions & 50 deletions setup.py

This file was deleted.

17 changes: 14 additions & 3 deletions tests/test_betfairstream.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import socket
import time
import ssl
import threading
from unittest import mock

Expand Down Expand Up @@ -252,13 +253,23 @@ def test_subscribe_to_cricket_matches(self, mock_send):
self.betfair_stream._unique_id, "cricketSubscription"
)

@mock.patch("ssl.wrap_socket")
@mock.patch("ssl.SSLContext")
@mock.patch("socket.socket")
def test_create_socket(self, mock_socket, mock_wrap_socket):
def test_create_socket(self, mock_socket, mock_ssl_context):
self.betfair_stream._create_socket()

mock_socket.assert_called_with(socket.AF_INET, socket.SOCK_STREAM)
assert mock_wrap_socket.call_count == 1
self.assertEqual(mock_ssl_context.call_count, 1)
mock_ssl_context.assert_called_with(ssl.PROTOCOL_TLS)
mock_ssl_context.return_value.wrap_socket.assert_called_with(
mock_socket.return_value, server_hostname="stream-api.betfair.com"
)
mock_ssl_context.return_value.wrap_socket.return_value.settimeout.assert_called_with(
6
)
mock_ssl_context.return_value.wrap_socket.return_value.connect.assert_called_with(
("stream-api.betfair.com", 443)
)

@mock.patch(
"betfairlightweight.streaming.betfairstream.BetfairStream._data",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def test_update_cache_new(self, mock_order_book_runner):
self.order_book_cache.market_id,
1234,
self.order_book_cache.lightweight,
**order_changes
**order_changes,
)

def test_update_cache_closed(self):
Expand Down

0 comments on commit 973c1f3

Please sign in to comment.