Skip to content

Commit

Permalink
Merge branch 'add_python' of https://github.com/LLNL/units into add_p…
Browse files Browse the repository at this point in the history
…ython
  • Loading branch information
phlptp committed Dec 21, 2024
2 parents 65e8102 + 62fafef commit 02e5ff6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 59 deletions.
102 changes: 52 additions & 50 deletions python/units_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ NB_MODULE(units_llnl_ext, mod)
.def(
"__init__",
[](units::precise_unit* type,
const char* arg0,
const char* commodity) {
new (type) units::precise_unit{
units::unit_from_string(std::string(arg0)),
units::getCommodity(std::string(commodity))};
const char* arg0,
const char* commodity) {
new (type) units::precise_unit{
units::unit_from_string(std::string(arg0)),
units::getCommodity(std::string(commodity))};
})
.def("multiplier", &units::precise_unit::multiplier)
.def(
Expand Down Expand Up @@ -74,28 +74,28 @@ NB_MODULE(units_llnl_ext, mod)
.def(
"is_exactly_the_same",
[](const units::precise_unit& type1,
const units::precise_unit& type2) {
return type1.is_exactly_the_same(type2);
const units::precise_unit& type2) {
return type1.is_exactly_the_same(type2);
})
.def(
"has_same_base",
[](const units::precise_unit& type1,
const units::precise_unit& type2) {
return type1.has_same_base(type2);
const units::precise_unit& type2) {
return type1.has_same_base(type2);
},
"check if two units have the same base units as each other")
.def(
"equivalent_non_counting",
[](const units::precise_unit& type1,
const units::precise_unit& type2) {
return type1.equivalent_non_counting(type2);
const units::precise_unit& type2) {
return type1.equivalent_non_counting(type2);
},
"check if two units are equivalent in the non-counting units portion of the units (moles|radians|count)")
.def(
"is_convertible",
[](const units::precise_unit& type1,
const units::precise_unit& type2) {
return type1.is_convertible(type2);
const units::precise_unit& type2) {
return type1.is_convertible(type2);
},
"check if two units are convertible to each other")
.def(
Expand Down Expand Up @@ -158,8 +158,8 @@ NB_MODULE(units_llnl_ext, mod)
return units::to_string(type);
})
.def("to_string", [](const units::precise_unit& type) {
return units::to_string(type);
});
return units::to_string(type);
});

nb::class_<units::precise_measurement>(
mod,
Expand All @@ -175,17 +175,17 @@ NB_MODULE(units_llnl_ext, mod)
.def(
"__init__",
[](units::precise_measurement* measurement,
double value,
const char* arg0) {
new (measurement) units::precise_measurement(
value, units::unit_from_string(std::string(arg0)));
double value,
const char* arg0) {
new (measurement) units::precise_measurement(
value, units::unit_from_string(std::string(arg0)));
})
.def(
"__init__",
[](units::precise_measurement* measurement,
double value,
const units::precise_unit& unit) {
new (measurement) units::precise_measurement(value, unit);
double value,
const units::precise_unit& unit) {
new (measurement) units::precise_measurement(value, unit);
})
.def("value", &units::precise_measurement::value)
.def(
Expand All @@ -203,28 +203,28 @@ NB_MODULE(units_llnl_ext, mod)
.def(
"value_as",
[](const units::precise_measurement& measurement,
const units::precise_unit& unit) {
return measurement.value_as(unit);
const units::precise_unit& unit) {
return measurement.value_as(unit);
})
.def(
"value_as",
[](const units::precise_measurement& measurement,
const char* units) {
return measurement.value_as(
units::unit_from_string(std::string(units)));
const char* units) {
return measurement.value_as(
units::unit_from_string(std::string(units)));
})
.def(
"convert_to",
[](const units::precise_measurement& measurement,
const units::precise_unit& unit) {
return measurement.convert_to(unit);
const units::precise_unit& unit) {
return measurement.convert_to(unit);
})
.def(
"convert_to",
[](const units::precise_measurement& measurement,
const char* units) {
return measurement.convert_to(
units::unit_from_string(std::string(units)));
const char* units) {
return measurement.convert_to(
units::unit_from_string(std::string(units)));
})
.def("convert_to_base", &units::precise_measurement::convert_to_base,"convert a measurement to a measurement using the base si units")
.def("as_unit", &units::precise_measurement::as_unit)
Expand All @@ -244,7 +244,9 @@ NB_MODULE(units_llnl_ext, mod)
.def(nb::self < nb::self)
.def(
"__pow__",
[](const units::precise_measurement& measurement, int power) { return pow(measurement,power); },
[](const units::precise_measurement& measurement, int power) {
return pow(measurement, power);
},
nb::is_operator())
.def(
"is_valid",
Expand Down Expand Up @@ -272,15 +274,15 @@ NB_MODULE(units_llnl_ext, mod)
return units::to_string(measurement);
})
.def("to_string", [](const units::precise_measurement& measurement) {
return units::to_string(measurement);
});
return units::to_string(measurement);
});

mod.def(
"convert",
[](double val,
const units::precise_unit& unitIn,
const units::precise_unit& unitOut) {
return units::convert(val, unitIn, unitOut);
const units::precise_unit& unitIn,
const units::precise_unit& unitOut) {
return units::convert(val, unitIn, unitOut);
},
"value"_a,
"unit_in"_a,
Expand All @@ -301,10 +303,10 @@ NB_MODULE(units_llnl_ext, mod)
mod.def(
"convert_pu",
[](double val,
const units::precise_unit& unitIn,
const units::precise_unit& unitOut,
double base_value) {
return units::convert(val, unitIn, unitOut, base_value);
const units::precise_unit& unitIn,
const units::precise_unit& unitOut,
double base_value) {
return units::convert(val, unitIn, unitOut, base_value);
},
"value"_a,
"unit_in"_a,
Expand All @@ -314,14 +316,14 @@ NB_MODULE(units_llnl_ext, mod)
mod.def(
"convert_pu",
[](double val,
const char* unitIn,
const char* unitOut,
double base_value) {
return units::convert(
val,
units::unit_from_string(std::string(unitIn)),
units::unit_from_string(std::string(unitOut)),
base_value);
const char* unitIn,
const char* unitOut,
double base_value) {
return units::convert(
val,
units::unit_from_string(std::string(unitIn)),
units::unit_from_string(std::string(unitOut)),
base_value);
},
"value"_a,
"unit_in"_a,
Expand Down
16 changes: 8 additions & 8 deletions test/python/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ def test_basic_measurement():
m3=m1/m2
m4=u.measurement('4.0 m/s')
assert(m3==m4)

def test_basic_measurement2():
m1=u.measurement(10,'m')
m2=u.measurement(2.5, 's')
m3=m1/m2
m4=u.measurement(4.0, 'm/s')
assert(m3==m4)

def test_basic_measurement3():

u1=u.unit('m')
u2=u.unit('s')

m1=u.measurement(10,u1)
m2=u.measurement(2.5, u2)

m4=u.measurement(4.0, u1/u2)
assert(m1/m2==m4)
assert(m4.units()==u1/u2)
Expand All @@ -43,22 +43,22 @@ def test_conditions():

assert(not m3.is_normal())
assert(m3.is_valid())

def test_root():
m1=u.measurement('100 m^6 per second squared')
m2=m1.root(2)
m3=m1.sqrt()
m4=u.measurement('10 m*m*m/s')
assert(m2==m3)
assert(m3==m4)

def test_pow():
m1=u.measurement('10 m')
m2=m1**2
m3=m1**3
assert(m2==m1*m1)
assert(m3==m1*m1*m1)

def test_comparisons():
m1=u.measurement('10 m')
m2=u.measurement('11 m')
Expand Down
2 changes: 1 addition & 1 deletion test/python/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_basic_unit():
u3=u1/u2
u4=u.unit('mph')
assert(u3.is_convertible(u4))

def test_basic_multiplication():
u1=u.unit('m')
u2=u.unit('s')
Expand Down

0 comments on commit 02e5ff6

Please sign in to comment.