From 9830c1850549f5cc8461f0b3f6f4993b1496d2aa Mon Sep 17 00:00:00 2001 From: Hernan Grecco Date: Sat, 15 Jul 2023 15:41:21 -0300 Subject: [PATCH] Migrate parse_single_unit to functools.cache --- pint/facets/context/registry.py | 1 - pint/facets/plain/registry.py | 14 ++------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/pint/facets/context/registry.py b/pint/facets/context/registry.py index 18a024077..540481567 100644 --- a/pint/facets/context/registry.py +++ b/pint/facets/context/registry.py @@ -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( diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index e39c4b75c..95e28308a 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -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 @@ -1080,20 +1075,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