Skip to content

Commit

Permalink
kinfs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Apr 10, 2024
1 parent b393c82 commit f8a4e08
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pint/delegates/txt_defparser/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ def from_string_and_config(
if not (s.startswith("[") and "=" in s):
return None

name, value, *aliases = (p.strip() for p in s.split("="))
name, value, *aliases = (p.strip() for p in s.split("="))

preferred_unit = None
if aliases:
if aliases[0] == "_":
Expand Down
2 changes: 1 addition & 1 deletion pint/facets/plain/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class DerivedDimensionDefinition(DimensionDefinition):

#: reference dimensions.
reference: UnitsContainer
# preferred unit
# preferred unit
preferred_unit: str | None = None

@property
Expand Down
13 changes: 10 additions & 3 deletions pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ def wrapped(self, *args, **kwargs):
result = f(self, *args, **kwargs)
print(args[0].kind, self.kind, f.__name__)
# TODO: Implement this for div, maybe move to mul and div funcs
if hasattr(self, "kind") and hasattr(args[0], "kind") and f.__name__ in ["_mul_div"]:
if (
hasattr(self, "kind")
and hasattr(args[0], "kind")
and f.__name__ in ["_mul_div"]
):
result_units_container = UnitsContainer({self.kind: 1, args[0].kind: 1})
print(args[0].kind, self.kind)
for dim in self._REGISTRY._dimensions.values():
if hasattr(dim, "reference") and dim.reference == result_units_container:
if (
hasattr(dim, "reference")
and dim.reference == result_units_container
):
return result.to_kind(dim.name)
raise ValueError(
f"Cannot multiply quantities with kinds {self.kind} and {args[0].kind}"
Expand Down Expand Up @@ -776,7 +783,7 @@ def _add_sub(self, other, op):
raise DimensionalityError(
self._units, other._units, self.dimensionality, other.dimensionality
)

if hasattr(self, "kind") and hasattr(other, "kind"):
if self.kind != other.kind:
# TODO: Should this be a KindError?
Expand Down
10 changes: 6 additions & 4 deletions pint/testsuite/test_kind.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from pint import UnitRegistry
from __future__ import annotations

import pytest

class TestKind():
from pint import UnitRegistry


class TestKind:
def test_torque_energy(self):
ureg = UnitRegistry()
Q_ = ureg.Quantity
moment_arm = Q_(1, "m").to_kind("[moment_arm]")
force = Q_(1, "lbf").to_kind("[force]")
# to_kind converts to the preferred_unit of the kind
assert force.units == ureg.N

# both force and moment_arm have kind defined.
# Torque is defined in default_en:
# [torque] = [force] * [moment_arm]
Expand All @@ -29,4 +32,3 @@ def test_torque_energy(self):
# Torque is not energy so cannot be added
with pytest.raises(ValueError):
energy + torque

0 comments on commit f8a4e08

Please sign in to comment.