Skip to content

Commit

Permalink
Reorganize convert and _convert for better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jul 15, 2023
1 parent 964e7a5 commit 688a5af
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pint/facets/context/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ def _convert(
value: Magnitude,
src: UnitsContainer,
dst: UnitsContainer,
inplace: bool = False,
inplace: bool,
check_dimensionality: bool,
) -> Magnitude:
"""Convert value from some source to destination units.
Expand Down Expand Up @@ -406,7 +407,7 @@ def _convert(

value, src = src._magnitude, src._units

return super()._convert(value, src, dst, inplace)
return super()._convert(value, src, dst, inplace, check_dimensionality)

def _get_compatible_units(
self, input_units: UnitsContainer, group_or_system: Optional[str] = None
Expand Down
9 changes: 7 additions & 2 deletions pint/facets/nonmultiplicative/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ def _add_ref_of_log_or_offset_unit(
return all_units

def _convert(
self, value: T, src: UnitsContainer, dst: UnitsContainer, inplace: bool = False
self,
value: T,
src: UnitsContainer,
dst: UnitsContainer,
inplace: bool,
check_dimensionality: bool,
) -> T:
"""Convert value from some source to destination units.
Expand Down Expand Up @@ -251,7 +256,7 @@ def _convert(
)

if not (src_offset_unit or dst_offset_unit):
return super()._convert(value, src, dst, inplace)
return super()._convert(value, src, dst, inplace, check_dimensionality)

src_dim = self._get_dimensionality(src)
dst_dim = self._get_dimensionality(dst)
Expand Down
6 changes: 3 additions & 3 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,15 +981,15 @@ def convert(
if src == dst:
return value

return self._convert(value, src, dst, inplace)
return self._convert(value, src, dst, inplace, True)

def _convert(
self,
value: T,
src: UnitsContainer,
dst: UnitsContainer,
inplace: bool = False,
check_dimensionality: bool = True,
inplace: bool,
check_dimensionality: bool,
) -> T:
"""Convert value from some source to destination units.
Expand Down
6 changes: 3 additions & 3 deletions pint/registry_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _converter(ureg, values, strict):
for ndx in dependent_args_ndx:
value = values[ndx]
assert _replace_units(args_as_uc[ndx][0], values_by_name) is not None
new_values[ndx] = ureg._convert(
new_values[ndx] = ureg.convert(
getattr(value, "_magnitude", value),
getattr(value, "_units", UnitsContainer({})),
_replace_units(args_as_uc[ndx][0], values_by_name),
Expand All @@ -143,15 +143,15 @@ def _converter(ureg, values, strict):
# third pass: convert other arguments
for ndx in unit_args_ndx:
if isinstance(values[ndx], ureg.Quantity):
new_values[ndx] = ureg._convert(
new_values[ndx] = ureg.convert(
values[ndx]._magnitude, values[ndx]._units, args_as_uc[ndx][0]
)
else:
if strict:
if isinstance(values[ndx], str):
# if the value is a string, we try to parse it
tmp_value = ureg.parse_expression(values[ndx])
new_values[ndx] = ureg._convert(
new_values[ndx] = ureg.convert(
tmp_value._magnitude, tmp_value._units, args_as_uc[ndx][0]
)
else:
Expand Down
4 changes: 2 additions & 2 deletions pint/testsuite/benchmarks/test_10_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def test_convert_from_uc(benchmark, my_setup: SetupType, key: str, pre_run: bool
src, dst = key
ureg, data = my_setup
if pre_run:
no_benchmark(ureg._convert, 1.0, data[src], data[dst])
benchmark(ureg._convert, 1.0, data[src], data[dst])
no_benchmark(ureg._convert, 1.0, data[src], data[dst], False, True)
benchmark(ureg._convert, 1.0, data[src], data[dst], False, True)


def test_parse_math_expression(benchmark, my_setup):
Expand Down

0 comments on commit 688a5af

Please sign in to comment.