Skip to content

Commit a8da88f

Browse files
committed
Implement get on CaseInsensitiveDict
get was previously provided by the parent class which had to raise KeyError for missing values. Since try/except is only cheap for the non-exception case the performance was not good when the key was missing similar to python/cpython#106665 but in the HA case we call this even more frequently
1 parent ac4171a commit a8da88f

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

async_upnp_client/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ def replace(self, new_data: abcMapping) -> None:
5656
self._data = {**new_data}
5757
self._case_map = {k.lower(): k for k in self._data}
5858

59+
def get(self, key: str, default: Any = None) -> Any:
60+
"""Get item with default.
61+
62+
This implementation is case insensitive and avoids
63+
calling __getitem__ which would raise KeyError and
64+
cause unnecessary exception handling.
65+
"""
66+
case_map = self._case_map
67+
data_key = case_map.get(key, case_map.get(key.lower(), _SENTINEL))
68+
if data_key is not _SENTINEL:
69+
return self._data.get(data_key, default)
70+
return default
71+
5972
def __setitem__(self, key: str, value: Any) -> None:
6073
"""Set item."""
6174
lower_key = key.lower()

0 commit comments

Comments
 (0)