diff --git a/src/openeo_processes/math.py b/src/openeo_processes/math.py index da24c993..b2acd80e 100644 --- a/src/openeo_processes/math.py +++ b/src/openeo_processes/math.py @@ -10,7 +10,7 @@ except ImportError: xar_addons = None -from openeo_processes.utils import process, keep_attrs +from openeo_processes.utils import process, keep_attrs, simple_process from openeo_processes.comparison import is_empty from openeo_processes.errors import QuantilesParameterConflict @@ -21,6 +21,7 @@ # Argumentless Functions/Constants ######################################################################################################################## +@simple_process def e(): """ The real number e is a mathematical constant that is the base of the natural logarithm such that ln(e) = 1. @@ -34,6 +35,7 @@ def e(): return np.e +@simple_process def pi(): """ The real number Pi (π) is a mathematical constant that is the ratio of the circumference of a circle to its @@ -51,6 +53,7 @@ def pi(): # Constant ######################################################################################################################## +@simple_process def constant(x): """ Define a constant value. diff --git a/src/openeo_processes/texts.py b/src/openeo_processes/texts.py index 2e39c7d9..d82302de 100644 --- a/src/openeo_processes/texts.py +++ b/src/openeo_processes/texts.py @@ -1,4 +1,7 @@ +from openeo_processes.utils import simple_process + +@simple_process def text_begins(text, pattern, case_sensitive=True): """ Checks whether the text (also known as `text`) contains the text specified for `pattern` at the beginning. @@ -28,6 +31,7 @@ def text_begins(text, pattern, case_sensitive=True): return text.lower().startswith(pattern.lower()) +@simple_process def text_ends(text, pattern, case_sensitive=True): """ Checks whether the text (also known as `text`) contains the text specified for `pattern` at the end. @@ -57,6 +61,7 @@ def text_ends(text, pattern, case_sensitive=True): return text.lower().endswith(pattern.lower()) +@simple_process def text_contains(text, pattern, case_sensitive=True): """ Checks whether the text (also known as `text`) contains the text specified for `pattern`. @@ -86,6 +91,7 @@ def text_contains(text, pattern, case_sensitive=True): return pattern.lower() in text.lower() +@simple_process def text_merge(data, separator=''): """ Merges string representations of a set of elements together to a single string, with the separator diff --git a/src/openeo_processes/utils.py b/src/openeo_processes/utils.py index a88ef40d..57c1c258 100644 --- a/src/openeo_processes/utils.py +++ b/src/openeo_processes/utils.py @@ -121,6 +121,25 @@ def fun_wrapper(*args, **kwargs): return fun_wrapper +def simple_process(process): + """ + This function serves as a decorator for simple processes that are implemented as straightforward python function. + + Parameters + ---------- + process : function + Function implementing an openEO process. + + Returns + ------- + object : + original function + """ + process_id = process.__name__.rstrip('_') + _processes[process_id] = process + return process + + def has_process(process_id: str) -> bool: """ Check if the given process is defined diff --git a/tests/test_utils.py b/tests/test_utils.py index 1b74e2ed..a0a299c6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -38,6 +38,10 @@ def test_has_process(): assert not has_process("or_") assert has_process("if") assert not has_process("if_") + assert has_process("e") + assert has_process("pi") + assert has_process("text_begins") + assert has_process("text_merge") @pytest.mark.parametrize(["pid", "args", "expected"], [ @@ -47,6 +51,9 @@ def test_has_process(): ("median", ([2, 5, 3, 8, 11],), 5), ("and", (False, True), False), ("or", (False, True), True), + ("e", (), np.e), + ("pi", (), np.pi), + ("text_begins", ("foobar", "foo"), True), ]) def test_get_process(pid, args, expected): fun = get_process(pid)