File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
11
11
12
12
### Changed
13
13
14
+ - ` openeo.UDF() ` : automatically un-indent given UDF code ([ #782 ] ( https://github.com/Open-EO/openeo-python-client/issues/782 ) )
15
+
14
16
### Removed
15
17
16
18
### Fixed
Original file line number Diff line number Diff line change 3
3
import logging
4
4
import pathlib
5
5
import re
6
+ import textwrap
6
7
import typing
7
8
import uuid
8
9
import warnings
@@ -142,6 +143,10 @@ class UDF:
142
143
Specifying the ``data`` argument is not necessary anymore, and actually deprecated.
143
144
Added :py:meth:`from_file` to simplify loading UDF code from a file.
144
145
See :ref:`old_udf_api` for more background about the changes.
146
+
147
+ .. versionchanged:: 0.43.0
148
+ Automatically un-indent given UDF code,
149
+ to simplify writing valid and properly formatted inline UDF code.
145
150
"""
146
151
147
152
# TODO: eliminate dependency on `openeo.rest.connection` and move to somewhere under `openeo.internal`?
@@ -155,7 +160,9 @@ def __init__(
155
160
data = None , # TODO #181 remove `data` argument
156
161
version : Optional [str ] = None ,
157
162
context : Optional [dict ] = None ,
163
+ * ,
158
164
_source = None ,
165
+ auto_dedent : bool = True ,
159
166
):
160
167
"""
161
168
Construct a UDF object from given code string and other argument related to the ``run_udf`` process.
@@ -167,7 +174,8 @@ def __init__(
167
174
:param context: optional additional UDF context data
168
175
:param _source: (for internal use) source identifier
169
176
"""
170
- # TODO: automatically dedent code (when literal string) ?
177
+ if auto_dedent :
178
+ code = textwrap .dedent (code )
171
179
self .code = code
172
180
self ._runtime = runtime
173
181
self .version = version
Original file line number Diff line number Diff line change @@ -3841,6 +3841,46 @@ def test_apply_udf_basic(self, con100):
3841
3841
},
3842
3842
}
3843
3843
3844
+ def test_apply_udf_auto_dedent (self , con100 ):
3845
+ udf = openeo .UDF (
3846
+ """
3847
+ import xarray
3848
+
3849
+ def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:
3850
+ cube.values = 0.0001 * cube.values
3851
+ return cube
3852
+ """
3853
+ )
3854
+ cube = con100 .load_collection ("S2" )
3855
+ res = cube .apply (udf )
3856
+
3857
+ assert res .flat_graph ()["apply1" ]["arguments" ] == {
3858
+ "data" : {"from_node" : "loadcollection1" },
3859
+ "process" : {
3860
+ "process_graph" : {
3861
+ "runudf1" : {
3862
+ "process_id" : "run_udf" ,
3863
+ "arguments" : {
3864
+ "data" : {"from_parameter" : "x" },
3865
+ "runtime" : "Python" ,
3866
+ "udf" : "\n " .join (
3867
+ [
3868
+ "" ,
3869
+ "import xarray" ,
3870
+ "" ,
3871
+ "def apply_datacube(cube: xarray.DataArray, context: dict) -> xarray.DataArray:" ,
3872
+ " cube.values = 0.0001 * cube.values" ,
3873
+ " return cube" ,
3874
+ "" ,
3875
+ ]
3876
+ ),
3877
+ },
3878
+ "result" : True ,
3879
+ }
3880
+ },
3881
+ },
3882
+ }
3883
+
3844
3884
def test_apply_udf_runtime_detection (self , con100 , requests_mock ):
3845
3885
udf = UDF ("def foo(x):\n return x\n " )
3846
3886
cube = con100 .load_collection ("S2" )
You can’t perform that action at this time.
0 commit comments