Skip to content

Commit

Permalink
Register percent and permille units (#359)
Browse files Browse the repository at this point in the history
* Register percent and permille units

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Add test case

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Feb 8, 2022
1 parent d247f86 commit e1a8870
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -15,6 +15,7 @@ New features
* Add CLI commands ``flexmeasures add sensor``, ``flexmeasures add asset-type``, ``flexmeasures add beliefs`` (which were experimental features before). [see `PR #337 <http://www.github.com/FlexMeasures/flexmeasures/pull/337>`_]
* Add CLI commands for showing data [see `PR #339 <http://www.github.com/FlexMeasures/flexmeasures/pull/339>`_]
* Add CLI command for attaching annotations to assets: ``flexmeasures add holidays`` adds public holidays [see `PR #343 <http://www.github.com/FlexMeasures/flexmeasures/pull/343>`_]
* Support for percent (%) and permille (‰) sensor units [see `PR #359 <http://www.github.com/FlexMeasures/flexmeasures/pull/359>`_]

Bugfixes
-----------
Expand Down
1 change: 1 addition & 0 deletions flexmeasures/utils/tests/test_unit_utils.py
Expand Up @@ -20,6 +20,7 @@
@pytest.mark.parametrize(
"from_unit, to_unit, expected_multiplier, expected_values",
[
("%", "‰", 10, None),
("m/s", "km/h", 3.6, None),
("m³/h", "l/h", 1000, None),
("m³", "m³/h", 4, None),
Expand Down
10 changes: 9 additions & 1 deletion flexmeasures/utils/unit_utils.py
Expand Up @@ -31,8 +31,16 @@
)

# Set up UnitRegistry with abbreviated scientific format
ur = pint.UnitRegistry(full_template)
ur = pint.UnitRegistry(
full_template,
preprocessors=[
lambda s: s.replace("%", " percent "),
lambda s: s.replace("‰", " permille "),
],
)
ur.default_format = "~P" # short pretty
ur.define("percent = 1 / 100 = %")
ur.define("permille = 1 / 1000 = ‰")


PREFERRED_UNITS = [
Expand Down

0 comments on commit e1a8870

Please sign in to comment.