Skip to content

Commit 3816b86

Browse files
committed
Fix string lazyexpr with list args
1 parent c1d15d0 commit 3816b86

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/blosc2/lazyexpr.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -812,17 +812,20 @@ def convert_to_slice(expression):
812812
if expr_i == "[":
813813
k = expression[i:].find("]") # start checking from after [
814814
slice_convert = expression[i : i + k + 1] # include [ and ]
815-
slicer = eval(f"np.s_{slice_convert}")
816-
slicer = (slicer,) if not isinstance(slicer, tuple) else slicer # standardise to tuple
817-
if any(isinstance(el, str) for el in slicer): # handle fields
818-
raise ValueError("Cannot handle fields for slicing lazy expressions.")
819-
slicer = str(slicer)
820-
# use slice so that lazyexpr uses blosc arrays internally
821-
# (and doesn't decompress according to getitem syntax)
822-
new_expr += f".slice({slicer})"
823-
skip_to_char = i + k + 1
824-
else:
825-
new_expr += expr_i
815+
try:
816+
slicer = eval(f"np.s_{slice_convert}")
817+
slicer = (slicer,) if not isinstance(slicer, tuple) else slicer # standardise to tuple
818+
if any(isinstance(el, str) for el in slicer): # handle fields
819+
raise ValueError("Cannot handle fields for slicing lazy expressions.")
820+
slicer = str(slicer)
821+
# use slice so that lazyexpr uses blosc arrays internally
822+
# (and doesn't decompress according to getitem syntax)
823+
new_expr += f".slice({slicer})"
824+
skip_to_char = i + k + 1
825+
continue
826+
except Exception:
827+
pass
828+
new_expr += expr_i # if slice_convert is e.g. a list, not a slice, do nothing
826829
return new_expr
827830

828831

tests/ndarray/test_lazyexpr.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,17 @@ def test_sort():
13431343
expr.sort().compute()
13441344

13451345

1346+
def test_listargs():
1347+
# lazyexpr tries to convert [] to slice, but could
1348+
# have problems for argumetns which are lists
1349+
shape = (20,)
1350+
na = np.arange(shape[0])
1351+
a = blosc2.asarray(na)
1352+
b = blosc2.asarray(na)
1353+
expr = blosc2.lazyexpr("stack([a, b])")
1354+
np.testing.assert_array_equal(expr[:], np.stack([a[:], b[:]]))
1355+
1356+
13461357
@pytest.mark.parametrize(
13471358
"obj",
13481359
[

0 commit comments

Comments
 (0)