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 24, 2024
1 parent 18070d1 commit b84b039
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Python package release, and other updates and fixes
- Added some additional equation units and tests [#315][]
- Added a number of additional units string [#322][],[#331][]
- added unary operation to measurement classes [#327][]

[#314]: https://github.com/LLNL/units/pull/314
[#315]: https://github.com/LLNL/units/pull/315
[#316]: https://github.com/LLNL/units/pull/316
Expand Down Expand Up @@ -125,8 +125,6 @@ A few user suggested tweaks, and support additional unit string conversions supp
[#298]: https://github.com/LLNL/units/pull/298
[#299]: https://github.com/LLNL/units/pull/299
[#303]: https://github.com/LLNL/units/pull/303


[0.9.0]: https://github.com/LLNL/units/releases/tag/v0.9.0
[0.9.1]: https://github.com/LLNL/units/releases/tag/v0.9.1
[0.9.2]: https://github.com/LLNL/units/releases/tag/v0.9.2
Expand Down
38 changes: 20 additions & 18 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

[Documentation](https://units.readthedocs.io/en/latest/)

The Units library provides a means of working with units of measurement at runtime, including conversion to and from strings. It provides a small number of types for working with units and measurements and operations necessary for user input and output with units. For additional description and discussion see [Readme](https://github.com/LLNL/units/blob/main/README.md). The Python library is a wrapper around the C++ library using [nanobind](https://github.com/wjakob/nanobind).
The Units library provides a means of working with units of measurement at runtime, including conversion to and from strings. It provides a small number of types for working with units and measurements and operations necessary for user input and output with units. For additional description and discussion see [Readme](https://github.com/LLNL/units/blob/main/README.md). The Python library is a wrapper around the C++ library using [nanobind](https://github.com/wjakob/nanobind).

## Table of contents

Expand Down Expand Up @@ -81,13 +81,15 @@ For more details see the [documentation](https://units.readthedocs.io/en/latest/

### Unit methods

These operations apply the `Units` object in Python. It maps to a `precise_unit` in C++. The Unit object is immutable like a python string so a new one is created for methods that modify the unit in some way.
These operations apply the `Units` object in Python. It maps to a `precise_unit` in C++. The Unit object is immutable like a python string so a new one is created for methods that modify the unit in some way.

#### Constructors

- `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

#### Methods

- `inv()->Unit` generate a new unit containing the inverse unit `Unit('m').inv()== Unit('1/m')`
- `pow(int power)->Unit` take a unit to power(NOTE: beware of limits on power representations of some units, things will always wrap so it is defined but may not produce what you expect). `power` can be negative.
- `is_exactly_the_same(other:Unit)->bool` compare two units and check for exact equivalence in both the unit_data and the multiplier
Expand All @@ -102,47 +104,48 @@ These operations apply the `Units` object in Python. It maps to a `precise_unit
- `is_error()->bool` true if the unit is an error unit (e.g invalid conversion)
- `isfinite()->bool` true if the unit does not have an infinite multiplier
- `isinf()->bool` true if the unit does have an infinite multiplier
- `root(power:int)->Unit` return a new unit taken to the root power
- `sqrt()->Unit` returns a new unit which is the square root of the current unit
- `to_string()->str` 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.
- `root(power:int)->Unit` return a new unit taken to the root power
- `sqrt()->Unit` returns a new unit which is the square root of the current unit
- `to_string()->str` 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.
- `multiplier()->float` return the unit multiplier as a floating point number
- `set_multiplier(mult:float)->Unit` generate a new Unit with the set multiplier
- `set_multiplier(mult:float)->Unit` generate a new Unit with the set multiplier
- `commodity()->str` get the commodity of the unit
- `set_commodity(int commodity)` generate a new unit with the assigned commodity.
#### Operators

#### Operators

- `*`,`/` with other units produces a new unit
- `**` is an exponentiation operator and produces a new unit
- `*`, `/` with a floating point generates a `Measurement`
- `==` and `!=` produce the appropriate comparison operators
- f string formatting also works with units


### Measurements

#### Constructors

- `Measurement(measurement_str:str)` construct from a string
- `Measurement(value:float, unit:Unit|str)` construct a `Measurement` from a value and a `Unit` or string representing a `Unit`

#### Methods

- `inv()->Unit` generate a new unit containing the inverse unit `Unit('m').inv()== Unit('1/m')`
- `pow(int power)->Unit` take a unit to power(NOTE: beware of limits on power representations of some units, things will always wrap so it is defined but may not produce what you expect). `power` can be negative.
- `is_normal()->bool` true if the unit is a normal unit (not error, nan, or subnormal)
- `is_valid()->bool` true if the `Measurement` is a valid Measurement (not error)
- `root(power:int)->Measurement` return a new unit taken to the root power
- `sqrt()->Unit` returns a new unit which is the square root of the current unit
- `to_string()->str` returns the string representation of the `Measurement`. This string is guaranteed to produce the equivalent `Measurement` as the current `Measurement`, but may not be the same string as was used to create it.
- `root(power:int)->Measurement` return a new unit taken to the root power
- `sqrt()->Unit` returns a new unit which is the square root of the current unit
- `to_string()->str` returns the string representation of the `Measurement`. This string is guaranteed to produce the equivalent `Measurement` as the current `Measurement`, but may not be the same string as was used to create it.
- `value()->float` return the numerical portion of a `Measurement`
- `set_value(value:float)->Measurement` generate a new `Measurement` with the new Value
- `set_value(value:float)->Measurement` generate a new `Measurement` with the new Value
- `units()->Unit` get the `Unit` associated with a `Measurement`
- `set_units(unit:Unit|str)` generate a new `Measurment` with the new units
- `set_units(unit:Unit|str)` generate a new `Measurement` with the new units
- `value_as(unit:Unit|str)->float` convert the value of the `Measurement` to a new `Unit`
- `convert_to(unit:Unit|str)->Measurement` create a new `Measurement` with the new units and the value converted to those units
- `convert_to_base()->Measurement` create a new `Measurement` with the units as the base measurement units
- `is_close(other:Measurement)->bool` return true if the two measurements are close (both converted to non precise measurement and compared)
#### Operators

#### Operators

- `*`,`/` with other `Measurements` produces a new Measurement
- `+`,`-` with other `Measurements` ensures the units are in the same base unit and performs the appropriate action
Expand All @@ -151,7 +154,6 @@ These operations apply the `Units` object in Python. It maps to a `precise_unit
- `==`,`!=`,`>`,`<`,`>=`,`<=` produce the appropriate comparison operators
- f string formatting also works with units


### Other library methods

- `convert(value:float,unit_in:Unit|str,unit_out:Unit|str)->float` generate a value represented by one unit in terms of another
Expand All @@ -162,7 +164,7 @@ These operations apply the `Units` object in Python. It maps to a `precise_unit

### Future plans

Uncertain measurements will likely be added, along with some math operations on measurements (floor, ceil, round, etc). Also some more commodity operations, and x12 and r20 unit types.
Uncertain measurements will likely be added, along with some math operations on measurements (floor, ceil, round, etc). Also some more commodity operations, and x12 and r20 unit types.

## Contributions

Expand Down
27 changes: 16 additions & 11 deletions python/units_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,16 @@ NB_MODULE(units_llnl_ext, mod)
[](const units::precise_measurement& measurement,
const units::precise_unit& unit) {
return measurement.convert_to(unit);
},"create a new `Measurement` with the new units and the value converted to those units")
},
"create a new `Measurement` with the new units and the value converted to those units")
.def(
"convert_to",
[](const units::precise_measurement& measurement,
const char* units) {
return measurement.convert_to(
units::unit_from_string(std::string(units)));
},"create a new `Measurement` with the new units and the value converted to those units")
},
"create a new `Measurement` with the new units and the value converted to those units")
.def(
"convert_to_base",
&units::precise_measurement::convert_to_base,
Expand Down Expand Up @@ -277,12 +279,14 @@ NB_MODULE(units_llnl_ext, mod)
"is_valid",
[](const units::precise_measurement& measurement) {
return units::is_valid(measurement);
},"true if the `Measurement` is a valid Measurement (not error)")
},
"true if the `Measurement` is a valid Measurement (not error)")
.def(
"is_normal",
[](const units::precise_measurement& measurement) {
return units::isnormal(measurement);
},"true if the unit is a normal unit(not error, nan, or subnormal)")
},
"true if the unit is a normal unit(not error, nan, or subnormal)")
.def(
"root",
[](const units::precise_measurement& measurement, int root) {
Expand All @@ -299,7 +303,8 @@ NB_MODULE(units_llnl_ext, mod)
const units::precise_measurement& measurement2) {
return units::measurement_cast(measurement1) ==
units::measurement_cast(measurement2);
},"return true if the two measurements are close (both converted to non precise measurement and compared)")
},
"return true if the two measurements are close (both converted to non precise measurement and compared)")
.def(
"__repr__",
[](const units::precise_measurement& measurement) {
Expand Down Expand Up @@ -368,11 +373,10 @@ NB_MODULE(units_llnl_ext, mod)
"get the default unit to use for a particular type of measurement");
mod.def(
"add_user_defined_unit",
[](const char* unit_name, const units::precise_unit &unit_definition) {
units::addUserDefinedUnit(
std::string(unit_name),
unit_definition);
}, "unit_name"_a,
[](const char* unit_name, const units::precise_unit& unit_definition) {
units::addUserDefinedUnit(std::string(unit_name), unit_definition);
},
"unit_name"_a,
"unit_definition"_a,
"add a custom string to represent a user defined unit");
mod.def(
Expand All @@ -381,7 +385,8 @@ NB_MODULE(units_llnl_ext, mod)
units::addUserDefinedUnit(
std::string(unit_name),
units::unit_from_string(std::string(unit_definition)));
}, "unit_name"_a,
},
"unit_name"_a,
"unit_definition"_a,
"add a custom string to represent a user defined unit");
mod.def(
Expand Down

0 comments on commit b84b039

Please sign in to comment.