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 Jan 5, 2025
1 parent 359e3cc commit d4bbbce
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Python package release, documentation update, continued addition of new units an
- Added commodity_conversion_map file containing some additional commoditity strings [#355][]
- In python library added dunder methods for floor, ceil, round, hash, floordiv [#357][]
- Added format specifiers for measurement to allow conversion in the format string and removal of the unit string [#357][]
- Added operators for `float` and `bool` in python [#357][]
- Added mod (`%`) and `//` operator in python for both other `Measurement` and `float` [#357][]
- Added operators for `float` and `bool` in python [#357][]
- Added mod (`%`) and `//` operator in python for both other `Measurement` and `float` [#357][]
- Added negation operator `-` in python [#357][]

### Removed

- removed specific python method `inv` - now just use inversion operator `~` [#357][]
- remove isolated `to_string` method on Unit and Measurement python classes, use `str()` [#357][]
- remove isolated `to_string` method on Unit and Measurement python classes, use `str()` [#357][]

[#355]: https://github.com/LLNL/units/pull/355
[#356]: https://github.com/LLNL/units/pull/356
Expand All @@ -48,7 +48,6 @@ Python package release, documentation update, continued addition of new units an
- updated the r20 units to be mostly operational [#314][]
- Updated third party libraries, and some new CI builders [#335][],[#336][]


### Fixed

- fixed some issues with windows.h header conflicts [#345][]
Expand Down
6 changes: 3 additions & 3 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ These operations apply the `Units` object in Python. It maps to a `precise_unit`
- `Unit(unit_str:str)` construct from a string
- `Unit(unit_str:str,commodity_str:str)` construct a unit from a unit string and commodity string
- `Unit(float multiplier, unit:Unit)` construct a unit using another unit as a base along with a multiplier

#### Methods

- `is_exactly_the_same(other:Unit)->bool` compare two units and check for exact equivalence in both the unit_data and the multiplier.
Expand Down Expand Up @@ -126,7 +126,7 @@ These operations apply the `Units` object in Python. It maps to a `precise_unit`
- `*`, `/` with a floating point generates a `Measurement`
- `==` and `!=` produce the appropriate comparison operators
- f string formatting also works with units and returns the string representation of the unit. This string is guaranteed to produce the same unit as the current unit, but may not be the same string as was used to create it.
- `str`,`bool` are defined, `bool` indicates that the unit is valid, and non-zero
- `str`,`bool` are defined, `bool` indicates that the unit is valid, and non-zero
- `Units` may also be used as the indexing element in a dictionary

### Measurements
Expand Down Expand Up @@ -163,7 +163,7 @@ These operations apply the `Units` object in Python. It maps to a `precise_unit`
- `*`, `/`,`%` with a floating point generates a `Measurement`
- `//` produces the floor of the resulting unit of division
- `==`,`!=`,`>`,`<`,`>=`,`<=` produce the appropriate comparison operators
- `str`,`float`,`bool` are defined, `bool` indicates that the measurement is non zero and is valid
- `str`,`float`,`bool` are defined, `bool` indicates that the measurement is non zero and is valid
- `round`, `math.ceil`,`math.floor`, and `math.trunc` work as expected
- f string formatting also works with measurement. Some special formatters are available `f"{m1:-}"` will remove the unit and just display the value. `f"{m1:new_unit}"` will convert to a new unit before displaying. `f"{m1:-new_unit}"` will do the conversion but just display the numerical value after the conversion.

Expand Down
22 changes: 15 additions & 7 deletions python/units_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ NB_MODULE(units_llnl_ext, mod)
})
.def(
"__init__",
[](units::precise_unit* type, double multiplier, const units::precise_unit &base) {
new (type) units::precise_unit(
multiplier,base);
[](units::precise_unit* type,
double multiplier,
const units::precise_unit& base) {
new (type) units::precise_unit(multiplier, base);
})
.def(
"__init__",
Expand Down Expand Up @@ -188,9 +189,13 @@ NB_MODULE(units_llnl_ext, mod)
[](const units::precise_unit& unit) {
return units::to_string(unit);
})
.def("__bool__", [](const units::precise_unit& unit) {
return (is_valid(unit) && !is_error(unit) && unit.multiplier() != 0);
})
.def(
"__bool__",
[](const units::precise_unit& unit) {
return (
is_valid(unit) && !is_error(unit) &&
unit.multiplier() != 0);
})
.def("__hash__", [](const units::precise_unit& unit) {
return std::hash<units::precise_unit>()(unit);
});
Expand Down Expand Up @@ -413,7 +418,10 @@ NB_MODULE(units_llnl_ext, mod)
return measurement.value();
})
.def("__bool__", [](const units::precise_measurement& measurement) {
return (is_valid(measurement.units())&&(measurement.value() != 0.0)&&(measurement.units().multiplier()!=0.0) && !is_error(measurement.units()));
return (
is_valid(measurement.units()) && (measurement.value() != 0.0) &&
(measurement.units().multiplier() != 0.0) &&
!is_error(measurement.units()));
});

mod.def(
Expand Down
10 changes: 6 additions & 4 deletions test/python/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,18 @@ def test_negation():
m3 = -m1
assert m3.value == -15.0


def test_conditions():
m1=u.Measurement(34.5,"fq2te1tg1fe")
m1 = u.Measurement(34.5, "fq2te1tg1fe")
assert not m1
assert not bool(m1)
m2=u.Measurement(0,"m")

m2 = u.Measurement(0, "m")
assert not m2
assert not bool(m2)
assert m2.is_normal()



def test_mod():
m1 = u.Measurement("18 seconds")
m2 = u.Measurement("1 min")
Expand Down
25 changes: 15 additions & 10 deletions test/python/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ def test_basic_unit():
u4 = u.Unit("mph")
assert u3.is_convertible_to(u4)


def test_unit_constructor():
u1 = u.Unit("cm")
u2 = u.Unit(100,u1)
assert u2==u.Unit('m')
u2 = u.Unit(100, u1)

assert u2 == u.Unit("m")
assert u2



def test_basic_multiplication():
u1 = u.Unit("m")
u2 = u.Unit("s")
Expand Down Expand Up @@ -48,17 +50,19 @@ def test_conditions():
u4 = u.Unit("puMW")
assert u4.is_per_unit()
assert not u3.is_per_unit()



def test_bad_unit():
ue=u.Unit("qdfgqtegqgqg")
ue = u.Unit("qdfgqtegqgqg")
assert not ue
assert ue.is_error()


def test_zero():
u1=u.Unit("0")
u2=u.Unit("nan")
u3=u.Unit("m/s")
u4=u.Unit(0,u3)
u1 = u.Unit("0")
u2 = u.Unit("nan")
u3 = u.Unit("m/s")
u4 = u.Unit(0, u3)
assert not bool(u1)
assert not u1
assert not bool(u2)
Expand All @@ -68,6 +72,7 @@ def test_zero():
assert not bool(u4)
assert not u4


def test_root():
u1 = u.Unit("m^6 per second squared")
u2 = u1.root(2)
Expand Down

0 comments on commit d4bbbce

Please sign in to comment.