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

Bump pandas from 2.0.3 to 2.1.0 #115

Merged
merged 4 commits into from
Sep 1, 2023
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
98 changes: 45 additions & 53 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">= 3.11, < 3.12"
pandas = {version = "^2.0.3", extras = ["html"]}
pandas = {version = "^2.1.0", extras = ["html"]}

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.3.3"
Expand Down
25 changes: 2 additions & 23 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import pytest

from xil._headers import _DEFAULT_CONTEXT, UA_HEADER, get_url_response
from xil._headers import _DEFAULT_CONTEXT, get_url_response


@pytest.fixture(name="url")
Expand All @@ -35,38 +35,17 @@ def _compare_requests(request1: Request, request2: Request) -> bool:
)


@pytest.mark.parametrize(
("default_headers", "expected_headers"), [(False, {}), (True, UA_HEADER)]
)
def test_get_url_response_headers(
url: str,
default_headers: bool,
expected_headers: dict[str, str],
mock_urlopen: Mock,
) -> None:
"""Test the get_url_response request headers"""
get_url_response(url, default_headers)
mock_urlopen.assert_called_once()
actual_request = mock_urlopen.call_args.args[0]
expected_request = Request(url=url, headers=expected_headers)
assert _compare_requests(
actual_request, expected_request
), "The actual request is different than expected"


@pytest.mark.parametrize("default_headers", [False, True])
@pytest.mark.parametrize(
("set_context", "expected_context"), [(False, None), (True, _DEFAULT_CONTEXT)]
)
def test_get_url_response_context(
url: str,
default_headers: bool,
set_context: bool,
expected_context: ssl.SSLContext | None,
mock_urlopen: Mock,
) -> None:
"""Test that get_url_response sets an SSL context when it is asked to"""
get_url_response(url, default_headers=default_headers, set_context=set_context)
get_url_response(url, set_context=set_context)
mock_urlopen.assert_called_once()
assert mock_urlopen.call_args.kwargs == {
"context": expected_context,
Expand Down
7 changes: 1 addition & 6 deletions xil/_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@

def get_url_response(
url: str,
default_headers: bool = True,
set_context: bool = False,
) -> http.client.HTTPResponse:
"""
Return the response from a URL with custom headers and SSL context when opening if
set_context is True.
"""
if default_headers:
headers = UA_HEADER
else:
headers = {}
if set_context:
context = _DEFAULT_CONTEXT
else:
context = None
request = urllib.request.Request(url, headers=headers)
request = urllib.request.Request(url)
return urllib.request.urlopen( # type: ignore[no-any-return]
request, context=context
)
4 changes: 1 addition & 3 deletions xil/boi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
[[_CURRENCY_KEY], ["code", "official rate", "change (%)", "amount", "time"]]
)

get_boi_url_response = partial(
get_url_response, default_headers=False, set_context=True
)
get_boi_url_response = partial(get_url_response, set_context=True)


def get_boi_df(url: str = _BOI_URL) -> pd.DataFrame:
Expand Down
24 changes: 12 additions & 12 deletions xil/fibi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import pandas as pd

from xil._df_normalizer import DataFrameNormalizer
from xil._headers import get_url_response
from xil._headers import UA_HEADER

_FIBI_URL = "http://apps.fibi.co.il/Matach/matach.aspx"
_ENCODING = "iso-8859-8"
_MATCH = "המחאות"
_MATCH = "העברות והמחאות"
_HEADER = [0, 1]
_ATTRS = {"class": "clsPart"}
_RELEVANT_COLS = [
Expand All @@ -22,8 +22,8 @@
("יציג", "יציג"),
("בנקנוטים", "מכירה"),
("בנקנוטים", "קניה"),
("המחאות", "מכירה"),
("המחאות", "קניה"),
("העברות והמחאות", "מכירה"),
("העברות והמחאות", "קניה"),
]
_IDX0 = pd.MultiIndex.from_product([["currency"], ["name", "amount", "official rate"]])
_IDX1 = pd.MultiIndex.from_product([["cash", "transfer"], ["sell", "buy"]])
Expand All @@ -32,14 +32,14 @@

def get_fibi_df(url: str = _FIBI_URL) -> pd.DataFrame:
"""Get FIBI exchange rates"""
with get_url_response(url) as response:
dfs = pd.read_html(
response, # type: ignore[arg-type]
match=_MATCH,
header=_HEADER,
encoding=_ENCODING,
attrs=_ATTRS,
)
dfs = pd.read_html(
url,
match=_MATCH,
header=_HEADER,
encoding=_ENCODING,
attrs=_ATTRS,
storage_options=UA_HEADER, # type: ignore[call-arg]
)

df = dfs[0] # guaranteed to have at least one element at this point
df = df[_RELEVANT_COLS]
Expand Down