Exemplo n.º 1
0
def testConversionToFullScalar() -> None:
    y = _LightweightScalar(value=1.0, unit="kg/m3", category="density")
    x = Scalar(value=y.GetValue(), unit=y.GetUnit(), category=y.GetCategory())

    assert x.GetValue() == 1.0

    assert x.GetUnit() == "kg/m3"
    assert x.GetCategory() == "density"
    assert x.GetQuantityType() == "density"

    assert x.GetQuantity().GetUnit() == "kg/m3"
    assert x.GetQuantity().GetCategory() == "density"
    assert x.GetQuantity().GetQuantityType() == "density"

    # Full Scalar supports conversion
    assert x.GetValue(unit="g/cm3") == 0.001
Exemplo n.º 2
0
    def ChangingIndex(self,
                      index: int,
                      value: Union[float, "Scalar", Tuple],
                      use_value_unit: bool = True) -> "FixedArray":
        """
        Creates a FixedArray from based on this FixedArray changing a single value based on the
        passed index.

        i.e.: array.ChangingIndex(0, Scalar(1.0, 'm'))
              will create a new FixedArray where the index == 0 is changed to the passed value.

        :param value:
            The value to be used to set at the given index.

        :param index:
            The index which should be changed.

        :param use_value_unit:
            If True and a Scalar is passed, the newly created array will have the unit of the
            scalar, not of the original array, otherwise (if False) the FixedArray unit will be
            kept.

        :return:
            The created FixedArray.
        """
        from barril.units import Scalar

        if isinstance(value, tuple):
            scalar = Scalar(self.GetValues()[index],
                            self.GetUnit()).CreateCopy(*value)

        elif not isinstance(value, Scalar):
            scalar = Scalar(value, self.GetUnit())

        else:
            scalar = value

        if use_value_unit:
            quantity = scalar.GetQuantity()
        else:
            quantity = self.GetQuantity()

        values = list(self.GetValues(quantity.GetUnit()))
        values[index] = scalar.GetValue(quantity.GetUnit())
        return FixedArray(self.dimension, quantity, tuple(values))