Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Fix #14: add support for simple processes to util.get_process() #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/openeo_processes/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -51,6 +53,7 @@ def pi():
# Constant
########################################################################################################################

@simple_process
def constant(x):
"""
Define a constant value.
Expand Down
6 changes: 6 additions & 0 deletions src/openeo_processes/texts.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/openeo_processes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"], [
Expand All @@ -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)
Expand Down