Skip to content

Commit

Permalink
♻️ Refactor code to use empty tuple instead of
Browse files Browse the repository at this point in the history
empty list
  • Loading branch information
fbriol committed Nov 23, 2023
1 parent 004e700 commit 12b6b57
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion zcollection/collection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _infer_callable(
try:
one_partition: str = next(collection.partitions(filters=filters))
except StopIteration:
return tuple()
return ()

with collection.synchronizer:
zds: dataset.Dataset = storage.open_zarr_group(
Expand Down
3 changes: 1 addition & 2 deletions zcollection/collection/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,5 +472,4 @@ def _load_and_apply_indexer(
partition_handler.join(partition_scheme, fs.sep))
zds: dataset.Dataset = open_zarr_group(
partition, fs, delayed=delayed, selected_variables=selected_variables)
return list(
zds.isel({partition_properties.dim: indexer}) for indexer in items)
return [zds.isel({partition_properties.dim: indexer}) for indexer in items]
2 changes: 1 addition & 1 deletion zcollection/collection/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def test_insert_with_missing_variable(
created by an algorithm.
"""
tested_fs = request.getfixturevalue(arg)
zds = next(create_test_dataset_with_fillvalue()).to_xarray()
zds = next(create_test_dataset_with_fillvalue())
zcollection = convenience.create_collection(
axis='time',
ds=zds,
Expand Down
13 changes: 5 additions & 8 deletions zcollection/indexing/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,10 @@ def _table_2_indexer(self, table: pyarrow.Table,
# Finally, we build the list of indexes of the different chunks found.
chunks = numpy.unique(numpy.concatenate(chunks))

return (
tuple( # type:ignore
(tuple(
(name, data[name][ix0])
for name in column_names), slice(start[ix0],
stop[ix1 - 1])), )
for ix0, ix1 in tuple(zip(chunks[:-1], chunks[1:])))
return ( # force yapf to respect the line break for flake8
(tuple((name, data[name][ix0])
for name in column_names), slice(start[ix0], stop[ix1 - 1]))
for ix0, ix1 in zip(chunks[:-1], chunks[1:]))

def query(
self,
Expand All @@ -425,7 +422,7 @@ def query(
Indexer.
"""
if len(self._partition_keys) == 0:
return tuple()
return ()

logical_op = logical_op or 'and'
if logical_op not in ('and', 'and_not', 'invert', 'or', 'xor'):
Expand Down
2 changes: 1 addition & 1 deletion zcollection/indexing/tests/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ def test_indexer(
)

indexer = HalfOrbitIndexer('', filesystem=fsspec.filesystem('memory'))
assert indexer.query({'cycle_number': [2, 4]}) == tuple()
assert indexer.query({'cycle_number': [2, 4]}) == ()
2 changes: 1 addition & 1 deletion zcollection/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(self,
compressor: numcodecs.abc.Codec | None = None,
fill_value: Any | None = None,
filters: Sequence[numcodecs.abc.Codec] | None = None) -> None:
attrs = attrs or tuple()
attrs = attrs or ()

#: Attributes of the variable.
self.attrs = tuple(attrs)
Expand Down
2 changes: 1 addition & 1 deletion zcollection/partitioning/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def encode(
>>> partitioning.encode(fields)
(numpy.datetime64('2020-01-01'),)
"""
return tuple((numpy.datetime64(self._stringify(partition)), ))
return (numpy.datetime64(self._stringify(partition)), )

def decode(
self,
Expand Down
26 changes: 13 additions & 13 deletions zcollection/tests/test_compressed_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#: Functions to test
functions = [
lambda x: x,
lambda x: dask.array.ufunc.expm1(x),
dask.array.ufunc.expm1,
lambda x: 2 * x,
lambda x: x / 2,
lambda x: x**2,
Expand All @@ -36,17 +36,17 @@
lambda x: x[:1, :, 1:3],
lambda x: x.T,
lambda x: dask.array.routines.transpose(x, (1, 2, 0)),
lambda x: dask.array.reductions.nanmean(x),
dask.array.reductions.nanmean,
lambda x: dask.array.reductions.nanmean(x, axis=1),
lambda x: dask.array.reductions.nanmax(x),
lambda x: dask.array.reductions.nanmin(x),
lambda x: dask.array.reductions.nanprod(x),
lambda x: dask.array.reductions.nanstd(x),
lambda x: dask.array.reductions.nanvar(x),
lambda x: dask.array.reductions.nansum(x),
dask.array.reductions.nanmax,
dask.array.reductions.nanmin,
dask.array.reductions.nanprod,
dask.array.reductions.nanstd,
dask.array.reductions.nanvar,
dask.array.reductions.nansum,
lambda x: dask.array.reductions.median(x, axis=0),
lambda x: dask.array.reductions.nanargmax(x),
lambda x: dask.array.reductions.nanargmin(x),
dask.array.reductions.nanargmax,
dask.array.reductions.nanargmin,
lambda x: dask.array.reductions.nancumprod(x, axis=0),
lambda x: dask.array.reductions.nancumsum(x, axis=0),
lambda x: x.sum(),
Expand Down Expand Up @@ -74,12 +74,12 @@
lambda x: x * 2, depth=0, trim=False, boundary='none'),
lambda x: x.round(1),
lambda x: x.reshape((x.shape[0] * x.shape[1], x.shape[2])),
lambda x: abs(x),
abs,
lambda x: x > 0.5,
lambda x: x.rechunk((4, 4, 4)),
lambda x: x.rechunk((2, 2, 1)),
lambda x: numpy.isneginf(x),
lambda x: numpy.isposinf(x),
numpy.isneginf,
numpy.isposinf,
]
# pylint: enable=unnecessary-lambda

Expand Down
4 changes: 2 additions & 2 deletions zcollection/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,11 @@ def test_dataset_select_variables_by_dims(
assert list(selected.variables) == ['var1', 'var3', 'var4', 'var6']
assert selected.dimensions == {'x': 10, 'y': 10, 'a': 20, 'b': 20}

selected = zds.select_variables_by_dims(tuple())
selected = zds.select_variables_by_dims(())
assert list(selected.variables) == ['var7']
assert selected.dimensions == {}

selected = zds.select_variables_by_dims(tuple(), predicate=False)
selected = zds.select_variables_by_dims((), predicate=False)
assert list(selected.variables) == [
'var1', 'var2', 'var3', 'var4', 'var5', 'var6'
]
Expand Down
2 changes: 1 addition & 1 deletion zcollection/variable/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def set_for_insertion(self: T) -> T:
name=self.name,
array=self.array,
dimensions=self.dimensions,
attrs=tuple(),
attrs=(),
compressor=self.compressor,
fill_value=self.fill_value,
filters=self.filters)
Expand Down

0 comments on commit 12b6b57

Please sign in to comment.