From 6b492cbceb7564d26c21f0c88c46809b5de9a88b Mon Sep 17 00:00:00 2001 From: Abel Aoun Date: Mon, 26 Feb 2024 18:15:55 +0100 Subject: [PATCH] wip: fix _read_string_threshold for period_per and doy_per --- .../generic_indices/thresholds/factory.py | 31 ++++++++++--------- src/icclim/models/registry.py | 2 +- tests/test_generic_functions.py | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/icclim/generic_indices/thresholds/factory.py b/src/icclim/generic_indices/thresholds/factory.py index 8f941ae2..9f250640 100644 --- a/src/icclim/generic_indices/thresholds/factory.py +++ b/src/icclim/generic_indices/thresholds/factory.py @@ -1,6 +1,5 @@ from __future__ import annotations -import re from collections.abc import Sequence from typing import TYPE_CHECKING @@ -169,18 +168,23 @@ def build_threshold( raise NotImplementedError(msg) +def _get_operator(query: str) -> tuple[Operator | None, str]: + prefix = query[0:3] + ops = OperatorRegistry.values() + for op in ops: + for alias in op.aliases: + if alias in prefix: + return op, query.replace(alias, "", 1) + return None, query + + def _read_string_threshold(query: str) -> tuple[str, str, float]: - value = re.findall(r"-?\d+\.?\d*", query) - if len(value) == 0: - msg = f"Cannot build threshold from '{query}'" - raise InvalidIcclimArgumentError(msg) - value = value[0] - value_index = query.find(value) - operator = query[0:value_index].strip() - if operator == "": - operator = None - unit = None if query.endswith(value) else query[value_index + len(value) :].strip() - return operator, unit, float(value) + # find op, remove op from query, if doy_per and period_per not in query, parse the rest with pint + op, no_op_query = _get_operator(query) + q = xc_units.Quantity(no_op_query) + if op is not None: + return op.operand, str(q.units), q.m + return "", str(q.units), q.m def _build_quantity( @@ -202,8 +206,7 @@ def _build_quantity( if operator is not None and operator != "": msg = ( f"Cannot parse quantity '{quantity}'" - f" The operator {operator} is not needed here" - f" operator for this parameter." + f" The operator {operator} should be removed." ) raise InvalidIcclimArgumentError(msg) return xc_units.Quantity(value=value, units=unit) diff --git a/src/icclim/models/registry.py b/src/icclim/models/registry.py index 716d749a..0eba3842 100644 --- a/src/icclim/models/registry.py +++ b/src/icclim/models/registry.py @@ -55,4 +55,4 @@ def catalog(cls) -> dict[str, T]: @classmethod def values(cls) -> list[T]: - return [v for k, v in cls.__dict__.items() if isinstance(v, cls._item_class)] + return [v for v in cls.__dict__.values() if isinstance(v, cls._item_class)] diff --git a/tests/test_generic_functions.py b/tests/test_generic_functions.py index 65fdb79a..09f062bf 100644 --- a/tests/test_generic_functions.py +++ b/tests/test_generic_functions.py @@ -60,4 +60,4 @@ def test_percentile(): resample_freq=FrequencyRegistry.MONTH, is_compared_to_reference=False, ) - assert result[1] == -5 + assert result[0] == -5