From 5a02edf0816ec8e80d159db43fcec978bedc849b Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Fri, 4 Feb 2022 17:28:17 +0100 Subject: [PATCH 1/3] Register percent and permille units Signed-off-by: F.N. Claessen --- flexmeasures/utils/unit_utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/flexmeasures/utils/unit_utils.py b/flexmeasures/utils/unit_utils.py index 8f70bfbec..d634bc53a 100644 --- a/flexmeasures/utils/unit_utils.py +++ b/flexmeasures/utils/unit_utils.py @@ -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 = [ From 61f032a7f06f72e5fd168025730c732ceaf3871d Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Fri, 4 Feb 2022 17:31:30 +0100 Subject: [PATCH 2/3] Changelog entry Signed-off-by: F.N. Claessen --- documentation/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/documentation/changelog.rst b/documentation/changelog.rst index 0bf37f6ba..f68ac3c12 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -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 `_] * Add CLI commands for showing data [see `PR #339 `_] * Add CLI command for attaching annotations to assets: ``flexmeasures add holidays`` adds public holidays [see `PR #343 `_] +* Support for percent (%) and permille (‰) sensor units [see `PR #359 `_] Bugfixes ----------- From 89db3917844caadcf014d45a4add935233867d48 Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Tue, 8 Feb 2022 17:26:08 +0100 Subject: [PATCH 3/3] Add test case Signed-off-by: F.N. Claessen --- flexmeasures/utils/tests/test_unit_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flexmeasures/utils/tests/test_unit_utils.py b/flexmeasures/utils/tests/test_unit_utils.py index 642255eab..eaa6c97ed 100644 --- a/flexmeasures/utils/tests/test_unit_utils.py +++ b/flexmeasures/utils/tests/test_unit_utils.py @@ -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),