Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
eskildsf committed Jan 4, 2025
1 parent e0b9ace commit 7f3d6d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
35 changes: 25 additions & 10 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,29 +897,35 @@ def _get_root_units(
except KeyError:
pass


accumulators: dict[str | None, int] = defaultdict(int)
fraction: dict[str, dict[Decimal|Float, Decimal|int]] = dict(numerator=dict(), denominator=dict())
fraction: dict[str, dict[Decimal | float, Decimal | int]] = dict(
numerator=dict(), denominator=dict()
)
self._get_root_units_recurse(input_units, 1, accumulators, fraction)

# Identify if terms appear in both numerator and denominator
def terms_are_unique(fraction):
for n_factor, n_exp in fraction["numerator"].items():
if n_factor in fraction["denominator"]:
return False
return True

# Cancel out terms where factor matches
while not terms_are_unique(fraction):
for n_factor, n_exponent in fraction["numerator"].items():
if n_factor in fraction["denominator"]:
if n_exponent >= fraction["denominator"][n_factor]:
fraction["numerator"][n_factor] -= fraction["denominator"][n_factor]
fraction["numerator"][n_factor] -= fraction["denominator"][
n_factor
]
del fraction["denominator"][n_factor]
continue
for d_factor, d_exponent in fraction["denominator"].items():
if d_factor in fraction["numerator"]:
if d_exponent >= fraction["numerator"][d_factor]:
fraction["denominator"][d_factor] -= fraction["numerator"][d_factor]
fraction["denominator"][d_factor] -= fraction["numerator"][
d_factor
]
del fraction["numerator"][d_factor]
continue

Expand Down Expand Up @@ -981,7 +987,11 @@ def get_base_units(
# TODO: accumulators breaks typing list[int, dict[str, int]]
# So we have changed the behavior here
def _get_root_units_recurse(
self, ref: UnitsContainer, exp: Scalar, accumulators: dict[str | None, int], fraction: dict[str, dict[Decimal|float, Decimal|int]]
self,
ref: UnitsContainer,
exp: Scalar,
accumulators: dict[str | None, int],
fraction: dict[str, dict[Decimal | float, Decimal | int]],
) -> None:
"""
Expand All @@ -996,13 +1006,18 @@ def _get_root_units_recurse(
accumulators[key] += exp2
else:
# Build numerator and denominator
#print(reg.converter.scale, exp2)
if exp2 < 0:
fraction['denominator'][reg.converter.scale] = fraction['denominator'].get(reg.converter.scale, 0)-exp2
fraction["denominator"][reg.converter.scale] = (
fraction["denominator"].get(reg.converter.scale, 0) - exp2
)
else:
fraction['numerator'][reg.converter.scale] = fraction['numerator'].get(reg.converter.scale, 0)+exp2
fraction["numerator"][reg.converter.scale] = (
fraction["numerator"].get(reg.converter.scale, 0) + exp2
)
if reg.reference is not None:
self._get_root_units_recurse(reg.reference, exp2, accumulators, fraction)
self._get_root_units_recurse(
reg.reference, exp2, accumulators, fraction
)

def get_compatible_units(self, input_units: QuantityOrUnitLike) -> frozenset[UnitT]:
""" """
Expand Down
8 changes: 4 additions & 4 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,17 +1355,17 @@ def test_issue2107():
# Use decimal
ureg = UnitRegistry(non_int_type=decimal.Decimal)
# 2 L/h is equal to 48 L/day
flow = decimal.Decimal('2')*ureg.L/ureg.h
assert flow.to(ureg.L/ureg.day).magnitude == 48.0
flow = decimal.Decimal("2") * ureg.L / ureg.h
assert flow.to(ureg.L / ureg.day).magnitude == 48.0
# 1 inch is equal to 1000 thou
distance = ureg.Quantity(decimal.Decimal("1.0"), ureg.inch)
assert distance.to(ureg.thou).magnitude == 1000.0

# Perform the same conversions without decimal
ureg = UnitRegistry()
# 2 L/h is equal to 48 L/day
flow = 2*ureg.L/ureg.h
assert flow.to(ureg.L/ureg.day).magnitude == 48.0
flow = 2 * ureg.L / ureg.h
assert flow.to(ureg.L / ureg.day).magnitude == 48.0
# 1 inch is equal to 1000 thou
distance = ureg.Quantity(1, ureg.inch)
assert distance.to(ureg.thou).magnitude == 1000.0

0 comments on commit 7f3d6d9

Please sign in to comment.