Skip to content

Commit f1f0b9e

Browse files
committed
Issue #195 #196 add text_concat support
1 parent 739aa71 commit f1f0b9e

File tree

4 files changed

+146
-21
lines changed

4 files changed

+146
-21
lines changed

openeo_driver/ProcessGraphDeserializer.py

+14
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,20 @@ def text_merge(
21452145
data: List[Union[str, int, float, bool, None]],
21462146
separator: Union[str, int, float, bool, None] = ""
21472147
) -> str:
2148+
# TODO #196 text_merge is deprecated if favor of test_concat
2149+
return str(separator).join(str(d) for d in data)
2150+
2151+
2152+
# TODO #195 #196 use official spec instead of custom openeo-processes/experimental/text_concat.json
2153+
@process_registry_100.add_function(spec=read_spec("openeo-processes/experimental/text_concat.json"))
2154+
def text_concat(
2155+
args: Dict,
2156+
env: EvalEnv
2157+
# data: List[Union[str, int, float, bool, None]],
2158+
# separator: Union[str, int, float, bool, None] = ""
2159+
) -> str:
2160+
data = extract_arg(args, "data")
2161+
separator = args.get("separator", "")
21482162
return str(separator).join(str(d) for d in data)
21492163

21502164

openeo_driver/processes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ def add_process(self, name: str, function: Callable = None, spec: dict = None, n
140140
if function and self._argument_names:
141141
sig = inspect.signature(function)
142142
arg_names = [n for n, p in sig.parameters.items() if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD]
143-
if arg_names[:len(self._argument_names)] != self._argument_names:
144-
raise ProcessRegistryException("Process {p!r} has invalid argument names: {a}".format(
145-
p=name, a=arg_names
146-
))
143+
if arg_names[: len(self._argument_names)] != self._argument_names:
144+
raise ProcessRegistryException(
145+
f"Process {name!r} has invalid argument names: {arg_names}. Expected {self._argument_names}"
146+
)
147147

148148
self._processes[self._key(name=name, namespace=namespace)] = ProcessData(function=function, spec=spec)
149149

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"id": "text_concat",
3+
"summary": "Concatenate elements to a single text",
4+
"description": "Merges text representations (also known as *string*) of a set of elements to a single text, having the separator between each element.",
5+
"categories": [
6+
"texts"
7+
],
8+
"parameters": [
9+
{
10+
"name": "data",
11+
"description": "A set of elements. Numbers, boolean values and null values get converted to their (lower case) string representation. For example: `1` (integer), `-1.5` (number), `true` / `false` (boolean values)",
12+
"schema": {
13+
"type": "array",
14+
"items": {
15+
"type": [
16+
"string",
17+
"number",
18+
"boolean",
19+
"null"
20+
]
21+
}
22+
}
23+
},
24+
{
25+
"name": "separator",
26+
"description": "A separator to put between each of the individual texts. Defaults to an empty string.",
27+
"schema": {
28+
"type": [
29+
"string",
30+
"number",
31+
"boolean",
32+
"null"
33+
]
34+
},
35+
"default": "",
36+
"optional": true
37+
}
38+
],
39+
"returns": {
40+
"description": "A string containing a string representation of all the array elements in the same order, with the separator between each element.",
41+
"schema": {
42+
"type": "string"
43+
}
44+
},
45+
"examples": [
46+
{
47+
"arguments": {
48+
"data": [
49+
"Hello",
50+
"World"
51+
],
52+
"separator": " "
53+
},
54+
"returns": "Hello World"
55+
},
56+
{
57+
"arguments": {
58+
"data": [
59+
1,
60+
2,
61+
3,
62+
4,
63+
5,
64+
6,
65+
7,
66+
8,
67+
9,
68+
0
69+
]
70+
},
71+
"returns": "1234567890"
72+
},
73+
{
74+
"arguments": {
75+
"data": [
76+
null,
77+
true,
78+
false,
79+
1,
80+
-1.5,
81+
"ß"
82+
],
83+
"separator": "\n"
84+
},
85+
"returns": "null\ntrue\nfalse\n1\n-1.5\nß"
86+
},
87+
{
88+
"arguments": {
89+
"data": [
90+
2,
91+
0
92+
],
93+
"separator": 1
94+
},
95+
"returns": "210"
96+
},
97+
{
98+
"arguments": {
99+
"data": []
100+
},
101+
"returns": ""
102+
}
103+
]
104+
}

tests/test_views_execute.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -2134,23 +2134,30 @@ def test_execute_no_cube_logic(api100, process_graph, expected):
21342134
assert api100.result(process_graph).assert_status_code(200).json == expected
21352135

21362136

2137-
@pytest.mark.parametrize(["process_id", "arguments", "expected"], [
2138-
("text_begins", {"data": "FooBar", "pattern": "Foo"}, True),
2139-
("text_begins", {"data": "FooBar", "pattern": "Bar"}, False),
2140-
("text_begins", {"data": "FooBar", "pattern": "fOo"}, False),
2141-
("text_begins", {"data": "FooBar", "pattern": "fOo", "case_sensitive": False}, True),
2142-
("text_contains", {"data": "FooBar", "pattern": "oB"}, True),
2143-
("text_contains", {"data": "FooBar", "pattern": "ob"}, False),
2144-
("text_contains", {"data": "FooBar", "pattern": "ob", "case_sensitive": False}, True),
2145-
("text_ends", {"data": "FooBar", "pattern": "Bar"}, True),
2146-
("text_ends", {"data": "FooBar", "pattern": "Foo"}, False),
2147-
("text_ends", {"data": "FooBar", "pattern": "bar"}, False),
2148-
("text_ends", {"data": "FooBar", "pattern": "bar", "case_sensitive": False}, True),
2149-
("text_merge", {"data": ["foo", "bar"]}, "foobar"),
2150-
("text_merge", {"data": ["foo", "bar"], "separator": "--"}, "foo--bar"),
2151-
("text_merge", {"data": [1, 2, 3], "separator": "/"}, "1/2/3"),
2152-
("text_merge", {"data": [1, "b"], "separator": 0}, "10b"),
2153-
])
2137+
@pytest.mark.parametrize(
2138+
["process_id", "arguments", "expected"],
2139+
[
2140+
("text_begins", {"data": "FooBar", "pattern": "Foo"}, True),
2141+
("text_begins", {"data": "FooBar", "pattern": "Bar"}, False),
2142+
("text_begins", {"data": "FooBar", "pattern": "fOo"}, False),
2143+
("text_begins", {"data": "FooBar", "pattern": "fOo", "case_sensitive": False}, True),
2144+
("text_contains", {"data": "FooBar", "pattern": "oB"}, True),
2145+
("text_contains", {"data": "FooBar", "pattern": "ob"}, False),
2146+
("text_contains", {"data": "FooBar", "pattern": "ob", "case_sensitive": False}, True),
2147+
("text_ends", {"data": "FooBar", "pattern": "Bar"}, True),
2148+
("text_ends", {"data": "FooBar", "pattern": "Foo"}, False),
2149+
("text_ends", {"data": "FooBar", "pattern": "bar"}, False),
2150+
("text_ends", {"data": "FooBar", "pattern": "bar", "case_sensitive": False}, True),
2151+
("text_merge", {"data": ["foo", "bar"]}, "foobar"),
2152+
("text_merge", {"data": ["foo", "bar"], "separator": "--"}, "foo--bar"),
2153+
("text_merge", {"data": [1, 2, 3], "separator": "/"}, "1/2/3"),
2154+
("text_merge", {"data": [1, "b"], "separator": 0}, "10b"),
2155+
("text_concat", {"data": ["foo", "bar"]}, "foobar"),
2156+
("text_concat", {"data": ["foo", "bar"], "separator": "--"}, "foo--bar"),
2157+
("text_concat", {"data": [1, 2, 3], "separator": "/"}, "1/2/3"),
2158+
("text_concat", {"data": [1, "b"], "separator": 0}, "10b"),
2159+
],
2160+
)
21542161
def test_text_processes(api100, process_id, arguments, expected):
21552162
# TODO: null propagation (`text_begins(data=null,...) -> null`) can not be tested at the moment
21562163
pg = {"t": {"process_id": process_id, "arguments": arguments, "result":True}}

0 commit comments

Comments
 (0)