Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def unit(value, unit="m"):
return units.Scalar.CreateWithQuantity(GetQuantity(unit), value)
unit_database.AddUnitBase("length", "meters", "m")
unit_database.AddUnit("length", "milimeters", "mm", "x * 1000.0", "x / 1000.0")
unit_database.AddUnit("length", "centimeters", "cm", "x * 100.0", "x / 100.0")
unit_database.AddCategory("well-diameter", "length")
unit_database.AddUnitBase("time", "seconds", "s")
unit_database.AddCategory(category="Time", quantity_type="time")
s1 = Scalar("well-diameter", 10, "m")
s2 = Scalar("well-diameter", 10, "cm")
s3 = Scalar("Time", 10, "s")
assert Scalar("well-diameter", 10.10, "m") == s1 + s2
assert Scalar("well-diameter", 9.90, "m") == s1 - s2
quantity = Quantity.CreateDerived(OrderedDict())
assert Scalar.CreateWithQuantity(quantity, 100) == s1 / s2
assert Scalar("well-diameter", 9.90, "m") == s1 - s2
with pytest.raises(InvalidOperationError):
s1.__sub__(s3)
def testBadUnit(unit_database_well_length):
x = units.Scalar.CreateWithQuantity(GetQuantity(), 500)
with pytest.raises(units.InvalidUnitError):
x.CreateCopy(unit="invalidUnit")
def testDivision(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
quantity, value = unit_database.Divide(m, km_city, 1, 0.01)
calculated1 = Scalar.CreateWithQuantity(quantity, value)
s1 = Scalar.CreateWithQuantity(m, 1)
s2 = Scalar.CreateWithQuantity(km_city, 0.01)
assert calculated1 == s1 / s2
def testCreationWithDerivedQuantity(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
quantity, value = unit_database.Multiply(m, km_city, 1, 0.01)
calculated1 = Scalar.CreateWithQuantity(quantity, value)
assert str(calculated1)
s1 = Scalar.CreateWithQuantity(m, 1)
s2 = Scalar.CreateWithQuantity(km_city, 0.01)
assert calculated1 == s1 * s2
def testDivision(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
quantity, value = unit_database.Divide(m, km_city, 1, 0.01)
calculated1 = Scalar.CreateWithQuantity(quantity, value)
s1 = Scalar.CreateWithQuantity(m, 1)
s2 = Scalar.CreateWithQuantity(km_city, 0.01)
assert calculated1 == s1 / s2
def testScalar(unit_database_well_length):
q = ObtainQuantity("m", "well-length")
s = Scalar.CreateWithQuantity(q, 10)
assert 10 == s.value
assert approx(abs(393.700787402 - s.GetValue(unit="in")), 7) == 0
s = s.CreateCopy(value=0.254, unit="in")
assert 0.254 == s.value
assert "in" == s.unit
assert "well-length" == s.category
with pytest.raises(AttributeError):
setattr(s, "value", 10)
def testEq(unit_database_well_length):
u1 = units.Scalar.CreateWithQuantity(GetQuantity(), 1.0)
u2 = units.Scalar.CreateWithQuantity(GetQuantity(), 1.0)
assert u1 == u2
assert u1.unit == u2.unit
assert "cm" == u2.unit
def testOperations(unit_database_well_length):
x = units.Scalar.CreateWithQuantity(GetQuantity(), 500)
assert x.value == 500
assert x.unit == "cm"
assert x.GetValue("m") == 5
assert x.unit == "cm"
assert x.GetValue("cm") == 500
assert x.unit == "cm"
assert x.GetValue("km") == 0.005
def testDivision(unit_database_len_time):
unit_database = unit_database_len_time
m = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1])]))
km_city = Quantity.CreateDerived(OrderedDict([("City size", ["km", 1])]))
quantity, value = unit_database.Divide(m, km_city, 1, 0.01)
calculated1 = Scalar.CreateWithQuantity(quantity, value)
s1 = Scalar.CreateWithQuantity(m, 1)
s2 = Scalar.CreateWithQuantity(km_city, 0.01)
assert calculated1 == s1 / s2