Skip to content

Commit

Permalink
Small speed up to verifying keys are present and true (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jul 14, 2024
1 parent 3b3c6b1 commit 07bd0f6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
19 changes: 5 additions & 14 deletions async_upnp_client/ssdp_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,21 @@ def is_valid_location(location: str) -> bool:

def valid_search_headers(headers: CaseInsensitiveDict) -> bool:
"""Validate if this search is usable."""
return bool(
headers.get_lower("_udn")
and headers.get_lower("st")
and is_valid_location(headers.get_lower("location", ""))
return headers.lower_values_true(("_udn", "st")) and is_valid_location(
headers.get_lower("location", "")
)


def valid_advertisement_headers(headers: CaseInsensitiveDict) -> bool:
"""Validate if this advertisement is usable for connecting to a device."""
return bool(
headers.get_lower("_udn")
and headers.get_lower("nt")
and headers.get_lower("nts")
and is_valid_location(headers.get_lower("location", ""))
return headers.lower_values_true(("_udn", "nt", "nts")) and is_valid_location(
headers.get_lower("location", "")
)


def valid_byebye_headers(headers: CaseInsensitiveDict) -> bool:
"""Validate if this advertisement has required headers for byebye."""
return bool(
headers.get_lower("_udn")
and headers.get_lower("nt")
and headers.get_lower("nts")
)
return headers.lower_values_true(("_udn", "nt", "nts"))


@lru_cache(maxsize=128)
Expand Down
31 changes: 20 additions & 11 deletions async_upnp_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
EXTERNAL_IP = "1.1.1.1"
EXTERNAL_PORT = 80

_SENTINEL = object()

UTC = timezone(timedelta(hours=0))
_UNCOMPILED_MATCHERS: Dict[str, Callable] = {
# date
Expand Down Expand Up @@ -72,11 +70,13 @@ class CaseInsensitiveDict(abcMutableMapping):

def __init__(self, data: Optional[abcMapping] = None, **kwargs: Any) -> None:
"""Initialize."""
self._data: Dict[str, Any] = {**(data or {}), **kwargs}
self._data: Dict[Any, Any] = {**(data or {}), **kwargs}
self._case_map: Dict[str, Any] = {
k
if type(k) is lowerstr # pylint: disable=unidiomatic-typecheck
else k.lower(): k
(
k
if type(k) is lowerstr # pylint: disable=unidiomatic-typecheck
else k.lower()
): k
for k in self._data
}

Expand Down Expand Up @@ -113,7 +113,7 @@ def combine_lower_dict(
"""
# pylint: disable=protected-access
_combined = CaseInsensitiveDict.__new__(CaseInsensitiveDict)
_combined._data = {**self._data, **lower_dict} # type: ignore[dict-item]
_combined._data = {**self._data, **lower_dict}
_combined._case_map = {**self._case_map, **{k: k for k in lower_dict}}
return _combined

Expand All @@ -131,7 +131,14 @@ def as_lower_dict(self) -> Dict[str, Any]:

def get_lower(self, lower_key: str, default: Any = None) -> Any:
"""Get a lower case key."""
return self._data.get(self._case_map.get(lower_key, _SENTINEL), default)
return self._data.get(self._case_map.get(lower_key), default)

def lower_values_true(self, lower_keys: Tuple[str, ...]) -> bool:
"""Check if all lower case keys are present and true values."""
for lower_key in lower_keys:
if not self._data.get(self._case_map.get(lower_key)):
return False
return True

def replace(self, new_data: abcMapping) -> None:
"""Replace the underlying dict without making a copy if possible."""
Expand All @@ -141,9 +148,11 @@ def replace(self, new_data: abcMapping) -> None:
else:
self._data = {**new_data}
self._case_map = {
k
if type(k) is lowerstr # pylint: disable=unidiomatic-typecheck
else k.lower(): k
(
k
if type(k) is lowerstr # pylint: disable=unidiomatic-typecheck
else k.lower()
): k
for k in self._data
}

Expand Down
1 change: 1 addition & 0 deletions changes/238.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Small speed up to verifying keys are present and true in CaseInsensitiveDict

0 comments on commit 07bd0f6

Please sign in to comment.