Skip to content

Commit

Permalink
Migrate parse_single_unit to functools.cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jul 15, 2023
1 parent b4048cd commit f23e451
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 14 deletions.
1 change: 0 additions & 1 deletion pint/facets/context/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def __init__(self, registry_cache: RegistryCache) -> None:
self.root_units = {}
self.dimensionality = registry_cache.dimensionality
self.parse_unit = registry_cache.parse_unit
self.parse_unit_name = registry_cache.parse_unit_name


class GenericContextRegistry(
Expand Down
15 changes: 2 additions & 13 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ def __init__(self) -> None:
#: Cache the unit name associated to user input. ('mV' -> 'millivolt')
self.parse_unit: dict[str, UnitsContainer] = {}

#: Maps (string and case insensitive) to (prefix, unit name, suffix)
self.parse_unit_name: dict[
tuple[str, bool], tuple[tuple[str, str, str], ...]
] = {}

def __eq__(self, other: Any):
if not isinstance(other, self.__class__):
return False
Expand All @@ -144,7 +139,6 @@ def __eq__(self, other: Any):
"root_units",
"dimensionality",
"parse_unit",
"parse_unit_name",
)
return all(getattr(self, attr) == getattr(other, attr) for attr in attrs)

Expand Down Expand Up @@ -1080,20 +1074,15 @@ def parse_single_unit(

return self._parse_single_unit(unit_name, case_sensitive)

@functools.cache
def _parse_single_unit(
self, unit_name: str, case_sensitive: bool
) -> tuple[tuple[str, str, str], ...]:
"""Helper of parse_unit_name."""

key = (unit_name, case_sensitive)
this_cache = self._cache.parse_unit_name
if key in this_cache:
return this_cache[key]

out = this_cache[key] = self._dedup_candidates(
return self._dedup_candidates(
self._yield_potential_units(unit_name, case_sensitive=case_sensitive)
)
return out

def _yield_potential_units(
self, unit_name: str, case_sensitive: bool
Expand Down

0 comments on commit f23e451

Please sign in to comment.