Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def GetQuantity(unit="cm"):
return units.ObtainQuantity(unit, "well-length")
def testFormatting(unit_database_start_units):
point = units.FixedArray(2, "length", [(100, 150), (50, 50)], "m")
assert "(100, 150) (50, 50) [m]" == str(point)
def MakeFixedArray(values):
"""Creates a fixed array with 3 dimensions used by testScalarOperations"""
return units.FixedArray(3, "length", values, "m")
def testPoscWithoutFillCategories(unit_database_posc_len_no_category):
unit_database = unit_database_posc_len_no_category
unit_database.AddCategory("my_len", "length")
# this category does not exist because we didn't fill the categories
with pytest.raises(UnitsError):
units.Scalar("length", 100, "km")
u1 = units.Scalar("my_len", 100, "km")
u2 = units.Scalar("my_len", 100000, "m")
assert approx(abs(u1.value - u2.GetValue("km")), 7) == 0
def db():
db = units.UnitDatabase.GetSingleton()
yield db
def testPoscLength(unit_database_posc):
# 'length'
u1 = units.Scalar("length", 10, "ft")
u2 = units.Scalar("length", 100, "km")
u3 = units.Scalar("length", 15, "yd")
u4 = units.Scalar("length", 15, "yd")
assert u1.GetQuantityType() == u2.GetQuantityType()
assert u1.unit != u2.unit
assert u3 == u4
# u1.unit = 'm' # from feet to metres
assert approx(abs(u1.GetValue("m") - 3.048), 7) == 0
# u2.unit = 'm' # from kilometres to metres
assert approx(abs(u2.GetValue("m") - 100000.0), 7) == 0
# u3.unit = 'ft' # from yd to ft
assert approx(abs(u3.GetValue("ft") - 45.0), 7) == 0
# now return u3.unit from feet to yards and compare u3.value with u4.value
# sanity-check
assert u3.GetValue("yd") == u4.value
def testQuantity(unit_database_well_length):
Quantity("well-length", "m")
with pytest.raises(units.InvalidQuantityTypeError):
Quantity("foo", "m")
def CreateUnitDatabaseLenTemp():
"""
:rtype: UnitDatabase
:returns:
Returns a unit database with length and time quantity types
"""
unit_database = units.UnitDatabase()
unit_database.AddUnitBase("length", "meters", "m")
unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0")
unit_database.AddUnitBase("temperature", "degC", "degC")
unit_database.AddUnit(
"temperature", "Kelvin", "K", "%f + 270", "%f - 270"
) # not correct convertion on purpose (for testing only)
unit_database.AddCategory("length", "length")
unit_database.AddCategory("temperature", "temperature")
return unit_database
def CreateUnitDatabaseLenPressure():
"""
:rtype: UnitDatabase
:returns:
Returns a unit database with length and time quantity types
"""
unit_database = units.UnitDatabase()
unit_database.AddUnitBase("length", "meters", "m")
unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0")
unit_database.AddUnitBase("pressure", "pascal", "Pa")
unit_database.AddUnit(
"pressure", "pounds/square inch", "psi", "%f / 6894.757", "%f * 6894.757"
)
unit_database.AddCategory("length", "length")
unit_database.AddCategory("pressure", "pressure")
return unit_database