-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR #200 finetune VectorCube.apply_dimension
- Loading branch information
Showing
4 changed files
with
217 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import dataclasses | ||
from typing import Optional | ||
|
||
|
||
class NotASingleRunUDFProcessGraph(ValueError): | ||
pass | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class SingleRunUDFProcessGraph: | ||
""" | ||
Container (and parser) for a callback process graph containing only a single `run_udf` node. | ||
""" | ||
|
||
data: dict | ||
udf: str | ||
runtime: str | ||
version: Optional[str] = None | ||
context: Optional[dict] = None | ||
|
||
@classmethod | ||
def parse(cls, process_graph: dict) -> "SingleRunUDFProcessGraph": | ||
try: | ||
(node,) = process_graph.values() | ||
assert node["process_id"] == "run_udf" | ||
assert node["result"] is True | ||
arguments = node["arguments"] | ||
assert {"data", "udf", "runtime"}.issubset(arguments.keys()) | ||
|
||
return cls( | ||
data=arguments["data"], | ||
udf=arguments["udf"], | ||
runtime=arguments["runtime"], | ||
version=arguments.get("version"), | ||
context=arguments.get("context") or {}, | ||
) | ||
except Exception as e: | ||
raise NotASingleRunUDFProcessGraph(str(e)) from e | ||
|
||
@classmethod | ||
def parse_or_none(cls, process_graph: dict) -> Optional["SingleNodeRunUDFProcessGraph"]: | ||
try: | ||
return cls.parse(process_graph=process_graph) | ||
except NotASingleRunUDFProcessGraph: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pytest | ||
|
||
from openeo_driver.util.pgparsing import SingleRunUDFProcessGraph, NotASingleRunUDFProcessGraph | ||
|
||
|
||
class TestSingleRunUDFProcessGraph: | ||
def test_parse_basic(self): | ||
pg = { | ||
"runudf1": { | ||
"process_id": "run_udf", | ||
"arguments": { | ||
"data": {"from_parameter": "data"}, | ||
"udf": "print('Hello world')", | ||
"runtime": "Python", | ||
}, | ||
"result": True, | ||
} | ||
} | ||
run_udf = SingleRunUDFProcessGraph.parse(pg) | ||
assert run_udf.data == {"from_parameter": "data"} | ||
assert run_udf.udf == "print('Hello world')" | ||
assert run_udf.runtime == "Python" | ||
assert run_udf.version is None | ||
assert run_udf.context == {} | ||
|
||
@pytest.mark.parametrize( | ||
"pg", | ||
[ | ||
{ | ||
"runudf1": { | ||
"process_id": "run_udffffffffffffffff", | ||
"arguments": {"data": {"from_parameter": "data"}, "udf": "x = 4", "runtime": "Python"}, | ||
"result": True, | ||
} | ||
}, | ||
{ | ||
"runudf1": { | ||
"process_id": "run_udf", | ||
"arguments": {"udf": "x = 4", "runtime": "Python"}, | ||
"result": True, | ||
} | ||
}, | ||
{ | ||
"runudf1": { | ||
"process_id": "run_udf", | ||
"arguments": {"data": {"from_parameter": "data"}, "runtime": "Python"}, | ||
"result": True, | ||
} | ||
}, | ||
{ | ||
"runudf1": { | ||
"process_id": "run_udf", | ||
"arguments": {"data": {"from_parameter": "data"}, "udf": "x = 4"}, | ||
"result": True, | ||
} | ||
}, | ||
{ | ||
"runudf1": { | ||
"process_id": "run_udf", | ||
"arguments": {"data": {"from_parameter": "data"}, "udf": "x = 4", "runtime": "Python"}, | ||
} | ||
}, | ||
{ | ||
"runudf1": { | ||
"process_id": "run_udf", | ||
"arguments": {"data": {"from_parameter": "data"}, "udf": "x = 4", "runtime": "Python"}, | ||
"result": True, | ||
}, | ||
"runudf2": { | ||
"process_id": "run_udf", | ||
"arguments": {"data": {"from_parameter": "data"}, "udf": "x = 4", "runtime": "Python"}, | ||
}, | ||
}, | ||
], | ||
) | ||
def test_parse_invalid(self, pg): | ||
with pytest.raises(NotASingleRunUDFProcessGraph): | ||
_ = SingleRunUDFProcessGraph.parse(pg) |