diff --git a/src/barril/units/_quantity.py b/src/barril/units/_quantity.py index 683f8f3..a9e48d0 100644 --- a/src/barril/units/_quantity.py +++ b/src/barril/units/_quantity.py @@ -660,9 +660,17 @@ def Convert(self, value, to_unit): :returns: An object with values to the passed unit. """ - return self._unit_database.Convert( - self._composing_categories, self._composing_units, to_unit, value - ) + try: + return self._unit_database.Convert( + self._composing_categories, self._composing_units, to_unit, value + ) + except: + return self._unit_database.Convert( + self._category, + self._CreateUnitsWithJoinedExponentsString(), + to_unit, + value, + ) @classmethod def _GetComparison(cls, operator, use_literals=False): diff --git a/src/barril/units/_scalar.py b/src/barril/units/_scalar.py index b5f1637..951a7f0 100644 --- a/src/barril/units/_scalar.py +++ b/src/barril/units/_scalar.py @@ -242,8 +242,8 @@ def GetFormatted(self, unit=None, value_format=None): def __eq__(self, other): return ( type(self) is type(other) - and self._value == other.value - and self._quantity == other._quantity + and self._value == other.GetValue(self.unit) + and self._quantity._category == other._quantity._category ) def AlmostEqual(self, other, precision): @@ -254,7 +254,8 @@ def AlmostEqual(self, other, precision): ) def __hash__(self, *args, **kwargs): - return hash((self._value, self._quantity)) + default_unit = self._unit_database.GetDefaultUnit(self._quantity._category) + return hash((self.GetValue(default_unit), self._quantity._category)) def __lt__(self, other): if self.quantity_type != other.quantity_type: diff --git a/src/barril/units/_tests/test_scalar.py b/src/barril/units/_tests/test_scalar.py index edf7cd8..07074bc 100644 --- a/src/barril/units/_tests/test_scalar.py +++ b/src/barril/units/_tests/test_scalar.py @@ -3,18 +3,17 @@ """ import pytest -from pytest import approx - -from barril._foundation.odict import odict from barril import units +from barril._foundation.odict import odict from barril.units import ( + ChangeScalars, InvalidOperationError, InvalidUnitError, ObtainQuantity, Quantity, Scalar, - ChangeScalars, ) +from pytest import approx def testScalarInterface(unit_database_well_length): @@ -615,3 +614,18 @@ class Fluid: assert fluid.density.GetValue("lbm/galUS") == 10 assert fluid.concentration.GetValue("%") == 1.0 + + +def testComparison(): + a = Scalar(1, "m") + b = Scalar(100, "cm") + c = Scalar(99, "cm") + + assert a == b + assert a <= b + assert b >= a + assert c <= b + assert c <= a + + # Test set creation with scalars + assert {b, a, c} == {a, b, c} == {c, a, b}