Skip to content

Commit 6ba9541

Browse files
Extra python units (#434)
* add python methods for generating units from strings for the extra unit standard r20, x12,dod * add some tests for the x12_unit_string and r20_unit_string methods * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update clang-tidy to clang 19 * fix clang tidy issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more clang-tidy fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more clang-tidy fixes and cppcheck suppressions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more clang tidy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more clang-tidy updates * clang-tidy fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * some additional clang-tidy fixes * ignore clang-tidy line * update r20_conv headers for clang-tidy * more clang-tidy * cleanup clang-tidy for tests * try to figure out why the test .clang-tidy isn't being used * .clang-tidy fixes * remove misc category from test clang-tidy * more clang-tidy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more clang-tidy * more clang-tidy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more clang-tidy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove modernize type traits since it is not compatible with C++11 * more clang-tidy fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9a7e062 commit 6ba9541

25 files changed

+360
-208
lines changed

.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Checks: "
2121
-bugprone-easily-swappable-parameters,
2222
misc-*,
2323
-misc-no-recursion,
24+
-misc-use-anonymous-namespace,
2425
cppcoreguidelines-*,
2526
-cppcoreguidelines-narrowing-conversions,
2627
-cppcoreguidelines-avoid-magic-numbers,
@@ -37,6 +38,7 @@ Checks: "
3738
-readability-magic-numbers,
3839
-readability-identifier-length,
3940
-readability-function-cognitive-complexity,
41+
-readability-avoid-nested-conditional-operator,
4042
performance-*
4143
"
4244

@@ -58,6 +60,7 @@ WarningsAsErrors: "
5860
-bugprone-easily-swappable-parameters,
5961
misc-*,
6062
-misc-no-recursion,
63+
-misc-use-anonymous-namespace,
6164
cppcoreguidelines-*,
6265
-cppcoreguidelines-narrowing-conversions,
6366
-cppcoreguidelines-avoid-magic-numbers,
@@ -71,6 +74,7 @@ WarningsAsErrors: "
7174
-readability-magic-numbers,
7275
-readability-identifier-length,
7376
-readability-function-cognitive-complexity,
77+
-readability-avoid-nested-conditional-operator,
7478
performance-*
7579
"
7680

.github/actions/quick_cmake/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ runs:
1313
using: composite
1414
steps:
1515
- name: CMake ${{ inputs.cmake-version }}
16-
uses: jwlawson/actions-setup-cmake@v1.13
16+
uses: jwlawson/actions-setup-cmake@v2.1.1
1717
with:
1818
cmake-version: "${{ inputs.cmake-version }}"
1919
- run: |

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
CMAKE_BUILD_PARALLEL_LEVEL: 1
2727
pool:
2828
vmImage: 'ubuntu-latest'
29-
container: helics/buildenv:clang14-builder
29+
container: helics/buildenv:clang19-builder
3030
steps:
3131
- template: .ci/azure-build.yml
3232
- script: git diff --exit-code --color

config/cppcheck_suppressions.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
unusedFunction:units/x12_conv.cpp:1024
22
unusedFunction:units/x12_conv.cpp:1009
3-
passedByValue:units/units.cpp:224
3+
passedByValue:units/units.cpp:234
44
passedByValue:units/units.cpp:1160
55
passedByValue:units/units.cpp:1177
6-
passedByValue:units/units.cpp:3069
6+
passedByValue:units/units.cpp:3082
77
passedByValue:units/units.hpp:413
8-
passedByValue:units/units.hpp:590
9-
passedByValue:units/units.hpp:1020
10-
passedByValue:units/units.hpp:1032
8+
passedByValue:units/units.hpp:593
9+
passedByValue:units/units.hpp:1026
10+
passedByValue:units/units.hpp:1038
1111
returnByReference:units/units.hpp:363
12-
returnByReference:units/units.hpp:576
13-
returnByReference:units/units.hpp:1017
14-
returnByReference:units/units.hpp:1246
15-
returnByReference:units/units.hpp:1485
12+
returnByReference:units/units.hpp:579
13+
returnByReference:units/units.hpp:1023
14+
returnByReference:units/units.hpp:1252
15+
returnByReference:units/units.hpp:1494

docs/details/commodities.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Normal Commodity with Form Factor
9090
=============================
9191
Frequently commodities come in a specific form factor. With a form factor code in place this can represent a form factor independent of the actual commodity material. For example a drum of oil vs a drum of gasoline.
9292
the container is wrapped in a 8-bit code bits 21-28. The commodity itself is contained in bits 0-20.
93-
The bit codes for packaging is 001[FFFFFFFF][CCCCCCC][CCCCCCC][CCCCCCC]. To the extent possible the form factor codes in use are those used in recommendation 20 of international trade for use in conjunction with harmonized code. This covers the trade of goods but in general is insufficient to cover all the required packaging modes necessary for general description so it is not used exactly. The codes 0-99 if used correspond to codes used in recommendation 20. The way this is a encoded is the lowest 7 bits correspond to the recommendation if the value < 99 since that is a 2 digit decimal numerical code. Numbers 100-127 and 228-255 are local user definitions defined as required for other purposes. Numbers 128 to 227 correspond to alternate names for recommendation 20 codes, this is to disambiguate strings when converting to and from string representations. In Rec 20 codes 70-79 are reserved for future use but may be used in the units library as needed.
93+
The bit codes for packaging is 001[FFFFFFFF][CCCCCCC][CCCCCCC][CCCCCCC]. To the extent possible the form factor codes in use are those used in recommendation 21 of international trade for use in conjunction with harmonized code. This covers the trade of goods but in general is insufficient to cover all the required packaging modes necessary for general description so it is not used exactly. The codes 0-99 if used correspond to codes used in recommendation 21. The way this is a encoded is the lowest 7 bits correspond to the recommendation if the value < 99 since that is a 2 digit decimal numerical code. Numbers 100-127 and 228-255 are local user definitions defined as required for other purposes. Numbers 128 to 227 correspond to alternate names for recommendation 21 codes, this is to disambiguate strings when converting to and from string representations. In Rec 21 codes 70-79 are reserved for future use but may be used in the units library as needed.
9494

9595
Normal Commodity
9696
============================

python/units_llnl/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
convert,
77
convert_pu,
88
default_unit,
9+
x12_unit,
10+
x12_unit_string,
11+
dod_unit,
12+
r20_unit,
13+
r20_unit_string,
914
add_user_defined_unit,
1015
defined_units_from_file,
1116
__doc__,

python/units_python.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,12 @@ NB_MODULE(units_llnl_ext, mod)
732732
units::precise::generate_custom_count_unit(
733733
static_cast<std::uint16_t>(value));
734734
} else {
735-
def = def * (units::default_unit(key).pow(value));
735+
const auto base_unit = units::default_unit(key);
736+
if (!units::is_valid(base_unit)) {
737+
throw std::invalid_argument(
738+
"Invalid dimension key in composition: " + key);
739+
}
740+
def = def * (base_unit.pow(value));
736741
}
737742
}
738743
new (dim) Dimension{def};
@@ -934,6 +939,28 @@ NB_MODULE(units_llnl_ext, mod)
934939
"default_unit",
935940
&units::default_unit,
936941
"get the default unit to use for a particular type of measurement");
942+
#ifndef UNITS_DISABLE_EXTRA_UNIT_STANDARDS
943+
mod.def(
944+
"x12_unit",
945+
&units::x12_unit,
946+
"generate a Unit from an ANSI X12 unit code string");
947+
mod.def(
948+
"x12_unit_string",
949+
&units::x12_unit_string,
950+
"generate an ANSI X12 unit code string from a matching Unit");
951+
mod.def(
952+
"dod_unit",
953+
&units::dod_unit,
954+
"generate a Unit from a US DOD unit code string");
955+
mod.def(
956+
"r20_unit",
957+
&units::r20_unit,
958+
"generate a Unit from an UN recommendation 20 unit code string");
959+
mod.def(
960+
"r20_unit_string",
961+
&units::r20_unit_string,
962+
"generate an UN recommendation 20 unit code string from a matching Unit");
963+
#endif
937964
mod.def(
938965
"add_user_defined_unit",
939966
[](const char* unit_name, const units::precise_unit& unit_definition) {

test/.clang-tidy

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Checks: "
1414
modernize*,
1515
-modernize-pass-by-value,
1616
-modernize-use-trailing-return-type,
17+
-modernize-type-traits,
1718
clang-analyzer-*,
1819
bugprone-*,
1920
-bugprone-narrowing-conversions,
@@ -24,20 +25,22 @@ Checks: "
2425
readability-*,
2526
-readability-magic-numbers,
2627
-readability-identifier-length,
27-
performance-*
28+
-readability-avoid-nested-conditional-operator,
29+
performance-*,
2830
"
2931

3032
WarningsAsErrors: "
3133
-*,
3234
google-*,
3335
-google-runtime-int,
3436
-google-runtime-references,
35-
-google-build-using-namespace
37+
-google-build-using-namespace,
3638
llvm-include-order,
3739
llvm-namespace-comment,
3840
modernize*,
3941
-modernize-pass-by-value,
4042
-modernize-use-trailing-return-type,
43+
-modernize-type-traits,
4144
clang-analyzer-*,
4245
bugprone-*,
4346
-bugprone-narrowing-conversions,
@@ -46,12 +49,12 @@ WarningsAsErrors: "
4649
readability-*,
4750
-readability-magic-numbers,
4851
-readability-identifier-length,
49-
performance-*
52+
-readability-avoid-nested-conditional-operator,
53+
performance-*,
5054
"
5155

52-
HeaderFilterRegex: "*test.*cpp"
56+
HeaderFilterRegex: "test.*cpp"
5357

54-
AnalyzeTemporaryDtors: false
5558
CheckOptions:
5659
- key: cert-dcl59-cpp.HeaderFileExtensions
5760
value: h,hh,hpp,hxx

test/fuzz_issue_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ TEST(fuzzFailures, rtripSingleProblems)
263263
auto u1 = unit_from_string(cdata);
264264
if (!is_error(u1)) {
265265
auto str = to_string(u1);
266-
std::cout << str << std::endl;
266+
std::cout << str << '\n';
267267
auto u2 = unit_from_string(str);
268268
EXPECT_FALSE(is_error(u2));
269269
if (u2 == u1) {

test/python/test_units.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,17 @@ class val1:
273273
assert v2.value1 == v.value1
274274
v.value1 = u.Unit("50 m")
275275
assert v2.value1 == u.Unit("10 m")
276+
277+
278+
def test_x12_unit_string_round_trip():
279+
code = u.x12_unit_string(u.x12_unit("17"))
280+
assert code != ""
281+
assert u.x12_unit(code).is_exactly_the_same(u.x12_unit("17"))
282+
assert u.x12_unit_string(u.Unit("error")) == ""
283+
284+
285+
def test_r20_unit_string_round_trip():
286+
code = u.r20_unit_string(u.r20_unit("A65"))
287+
assert code != ""
288+
assert u.r20_unit(code).is_exactly_the_same(u.r20_unit("A65"))
289+
assert u.r20_unit_string(u.Unit("error")) == ""

0 commit comments

Comments
 (0)