Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 18, 2024
1 parent 31fe596 commit 310091e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ test-requires = "pytest"

# Needed for full C++17 support
[tool.cibuildwheel.macos.environment]
MACOSX_DEPLOYMENT_TARGET = "10.14"
MACOSX_DEPLOYMENT_TARGET = "10.14"
17 changes: 8 additions & 9 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
cmake_minimum_required(VERSION 3.22...3.31)
project(llnl-units LANGUAGES CXX)

find_package(Python 3.10 REQUIRED COMPONENTS Interpreter Development.Module OPTIONAL_COMPONENTS Development.SABIModule)
find_package(
Python 3.10 REQUIRED
COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule
)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE
Expand All @@ -16,20 +20,15 @@ endif()

# Detect the installed nanobind package and import it into CMake
execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE nanobind_ROOT
)
find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(
units_llnl
NB_STATIC
STABLE_ABI
src/units_python.cpp
)
nanobind_add_module(units_llnl NB_STATIC STABLE_ABI src/units_python.cpp)

target_link_libraries(units_llnl PUBLIC units::units)

# Install directive for scikit-build-core
install(TARGETS units_llnl LIBRARY DESTINATION units_llnl)
install(TARGETS units_llnl LIBRARY DESTINATION units_llnl)
49 changes: 36 additions & 13 deletions python/src/units_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ NB_MODULE(units_llnl, mod)
"check if two units are convertible to eachother")
.def(
"is_convertible_to",
[](const units::precise_unit& type1,
const char *desired_units) {
return type1.is_convertible( units::unit_from_string(std::string(desired_units)));
[](const units::precise_unit& type1, const char* desired_units) {
return type1.is_convertible(
units::unit_from_string(std::string(desired_units)));
},
"check if the unit can be converted to the desired unit")
.def(
Expand Down Expand Up @@ -280,21 +280,36 @@ NB_MODULE(units_llnl, mod)
const units::precise_unit& unitIn,
const units::precise_unit& unitOut) {
return units::convert(val, unitIn, unitOut);
},"value"_a,"unit_in"_a,"unit_out"_a, "value represented by one unit in terms of another");
mod.def("convert", [](double val, const char* unitIn, const char* unitOut) {
return units::convert(
val,
units::unit_from_string(std::string(unitIn)),
units::unit_from_string(std::string(unitOut)));
},"value"_a,"unit_in"_a,"unit_out"_a,"generate a value represented by one unit in terms of another");
},
"value"_a,
"unit_in"_a,
"unit_out"_a,
"value represented by one unit in terms of another");
mod.def(
"convert",
[](double val, const char* unitIn, const char* unitOut) {
return units::convert(
val,
units::unit_from_string(std::string(unitIn)),
units::unit_from_string(std::string(unitOut)));
},
"value"_a,
"unit_in"_a,
"unit_out"_a,
"generate a value represented by one unit in terms of another");
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);
},"value"_a,"unit_in"_a,"unit_out"_a,"base_value"_a, "generate a value represented by one unit in terms of another if one of the units is in per-unit, the base_value is used in part of the conversion");
},
"value"_a,
"unit_in"_a,
"unit_out"_a,
"base_value"_a,
"generate a value represented by one unit in terms of another if one of the units is in per-unit, the base_value is used in part of the conversion");
mod.def(
"convert_pu",
[](double val,
Expand All @@ -306,6 +321,14 @@ NB_MODULE(units_llnl, mod)
units::unit_from_string(std::string(unitIn)),
units::unit_from_string(std::string(unitOut)),
base_value);
},"value"_a,"unit_in"_a,"unit_out"_a,"base_value"_a,"generate a value represented by one unit in terms of another if one of the units is in per-unit, the base_value is used in part of the conversion");
mod.def("default_unit",&units::default_unit,"get the default unit to use for a particular type of measurement");
},
"value"_a,
"unit_in"_a,
"unit_out"_a,
"base_value"_a,
"generate a value represented by one unit in terms of another if one of the units is in per-unit, the base_value is used in part of the conversion");
mod.def(
"default_unit",
&units::default_unit,
"get the default unit to use for a particular type of measurement");
}
10 changes: 5 additions & 5 deletions python/tests/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_conditions():
u1=u.unit('m')
u2=u.unit('error')
Expand All @@ -20,10 +20,10 @@ def test_conditions():
assert(u2.is_error())
assert(u1.is_normal())
assert(not u2.is_normal())

assert(not u2.is_default())

assert(u1.is_valid())

assert(u1.is_finite())
assert(not u3.is_finite())
assert(not u3.is_finite())

0 comments on commit 310091e

Please sign in to comment.