Skip to content

Commit 9a2296c

Browse files
committed
no longer require SliceSOurce instances, renamed slice_obj -> slice_item
1 parent 4111dad commit 9a2296c

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

tests/test_slice.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright © 2024 Norman Fomferra
22
# Permissions are hereby granted under the terms of the MIT License:
33
# https://opensource.org/licenses/MIT.
4+
45
import shutil
56
import unittest
67
import warnings
@@ -60,18 +61,18 @@ def test_slice_source_class_slice_source_fails(self):
6061
with pytest.raises(
6162
TypeError,
6263
match=(
63-
"expected an instance of SliceSource returned from"
64-
" 'false_slice_source_function', but got <class 'int'>"
64+
"slice_item must be a str, zappend.fsutil.FileObj,"
65+
" zappend.slice.SliceSource, xarray.Dataset, or a callable"
6566
),
6667
):
6768
open_slice_source(ctx, "test.nc")
6869

6970
def test_slice_source_slice_source(self):
7071
dataset = make_test_dataset()
7172
ctx = Context(dict(target_dir="memory://target.zarr"))
72-
slice_obj = MemorySliceSource(ctx, dataset, 0)
73-
slice_source = open_slice_source(ctx, slice_obj)
74-
self.assertIs(slice_obj, slice_source)
73+
slice_item = MemorySliceSource(ctx, dataset, 0)
74+
slice_source = open_slice_source(ctx, slice_item)
75+
self.assertIs(slice_item, slice_source)
7576

7677
def test_slice_factory_slice_source(self):
7778
dataset = make_test_dataset()
@@ -189,7 +190,7 @@ def test_it_raises_on_persistent_wait_fail(self):
189190
# noinspection PyMethodMayBeStatic
190191
def test_it_raises_on_invalid_type(self):
191192
ctx = Context(dict(target_dir="memory://target.zarr"))
192-
with pytest.raises(TypeError, match="slice_obj must be a str, "):
193+
with pytest.raises(TypeError, match="slice_item must be a str, "):
193194
# noinspection PyTypeChecker
194195
open_slice_source(ctx, 42)
195196

zappend/slice/factory.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
def open_slice_source(
2828
ctx: Context,
29-
slice_obj: SliceObj | SliceFactory | Any,
29+
slice_item: SliceObj | SliceFactory | Any,
3030
slice_index: int = 0,
3131
) -> SliceSource:
32-
"""Open the slice source for given slice object `slice_obj`.
32+
"""Open the slice source for given slice object `slice_item`.
3333
3434
The intended and only use of the returned slice source is as context
3535
manager. When used as context manager the slice source yields a slice
@@ -39,7 +39,7 @@ def open_slice_source(
3939
class derived from `zappend.slice.SliceSource` or a function that returns
4040
instances of `zappend.slice.SliceSource`. The retrieved slice source will
4141
be returned by this function. The class or function will receive positional
42-
and keyword arguments derived from `slice_obj` as follows:
42+
and keyword arguments derived from `slice_item` as follows:
4343
4444
* `tuple`: a pair of the form `(args, kwargs)`, where `args` is a list
4545
or tuple of positional arguments and `kwargs` is a dictionary of keyword
@@ -49,7 +49,7 @@ class derived from `zappend.slice.SliceSource` or a function that returns
4949
* Any other type is interpreted as single positional argument.
5050
5151
If `slice_source` is not specified in the configuration, the slice object
52-
`slice_obj` may have one of the following types:
52+
`slice_item` may have one of the following types:
5353
5454
* `str`: A local file path or URI pointing to a dataset file such as a
5555
Zarr or NetCDF. If it is a URI, the `ctx.slice_storage_options` apply.
@@ -63,54 +63,46 @@ class derived from `zappend.slice.SliceSource` or a function that returns
6363
6464
Args:
6565
ctx: The processing context
66-
slice_obj: A slice object
66+
slice_item: An slice item
6767
slice_index: Optional slice index, used for dataset identification
6868
6969
Returns:
7070
A new slice source instance
7171
"""
7272
_slice_source = ctx.config.slice_source
7373
if _slice_source is not None:
74-
slice_args, slice_kwargs = normalize_slice_arg(slice_obj)
75-
slice_factory = to_slice_factory(_slice_source, *slice_args, **slice_kwargs)
76-
slice_source = slice_factory(ctx)
77-
if not isinstance(slice_source, SliceSource):
78-
raise TypeError(
79-
f"expected an instance of SliceSource"
80-
f" returned from {_slice_source.__name__!r},"
81-
f" but got {type(slice_source)}"
82-
)
83-
return slice_source
74+
slice_args, slice_kwargs = normalize_slice_arg(slice_item)
75+
slice_item = to_slice_factory(_slice_source, *slice_args, **slice_kwargs)
8476

85-
return _get_slice_dataset_recursively(ctx, slice_obj, slice_index)
77+
return _get_slice_dataset_recursively(ctx, slice_item, slice_index)
8678

8779

8880
def _get_slice_dataset_recursively(
8981
ctx: Context,
90-
slice_obj: SliceObj | SliceFactory,
82+
slice_item: SliceObj | SliceFactory,
9183
slice_index: int,
9284
) -> SliceSource:
93-
if isinstance(slice_obj, SliceSource):
94-
return slice_obj
95-
if isinstance(slice_obj, (str, FileObj)):
96-
if isinstance(slice_obj, str):
85+
if isinstance(slice_item, SliceSource):
86+
return slice_item
87+
if isinstance(slice_item, (str, FileObj)):
88+
if isinstance(slice_item, str):
9789
slice_file = FileObj(
98-
slice_obj, storage_options=ctx.config.slice_storage_options
90+
slice_item, storage_options=ctx.config.slice_storage_options
9991
)
10092
else:
101-
slice_file = slice_obj
93+
slice_file = slice_item
10294
return PersistentSliceSource(ctx, slice_file)
103-
if isinstance(slice_obj, xr.Dataset):
95+
if isinstance(slice_item, xr.Dataset):
10496
if ctx.config.persist_mem_slices:
105-
return TemporarySliceSource(ctx, slice_obj, slice_index)
97+
return TemporarySliceSource(ctx, slice_item, slice_index)
10698
else:
107-
return MemorySliceSource(ctx, slice_obj, slice_index)
108-
if callable(slice_obj):
109-
slice_factory: SliceFactory = slice_obj
110-
slice_obj = slice_factory(ctx)
111-
return _get_slice_dataset_recursively(ctx, slice_obj, slice_index)
99+
return MemorySliceSource(ctx, slice_item, slice_index)
100+
if callable(slice_item):
101+
slice_factory: SliceFactory = slice_item
102+
slice_item = slice_factory(ctx)
103+
return _get_slice_dataset_recursively(ctx, slice_item, slice_index)
112104
raise TypeError(
113-
"slice_obj must be a"
105+
"slice_item must be a"
114106
" str,"
115107
" zappend.fsutil.FileObj,"
116108
" zappend.slice.SliceSource,"

0 commit comments

Comments
 (0)