Skip to content

Commit 31b9aae

Browse files
committed
Add slice operator
1 parent b676de3 commit 31b9aae

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

terminusdb_client/woqlquery/woql_query.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,49 @@ def length(self, var_list, var_len):
27872787
self._cursor["length"] = self._varj(var_len)
27882788
return self
27892789

2790+
def slice(self, input_list, result_var, start, end=None):
2791+
"""Slice
2792+
Extracts a sublist from a list given start and end indices.
2793+
2794+
Parameters
2795+
----------
2796+
input_list : list or str
2797+
The input list or variable containing a list
2798+
result_var : str
2799+
Variable to bind the resulting slice
2800+
start : int or str
2801+
Start index (0-based, supports negative indices)
2802+
end : int or str, optional
2803+
End index (exclusive). If not provided, slices to end of list.
2804+
2805+
Returns
2806+
-------
2807+
WOQLQuery object
2808+
query object that can be chained and/or execute
2809+
2810+
Examples
2811+
--------
2812+
>>> WOQLQuery().slice(["a", "b", "c", "d"], "v:result", 1, 3) # result = ["b", "c"]
2813+
>>> WOQLQuery().slice("v:my_list", "v:result", 0, 2) # first two elements
2814+
"""
2815+
if input_list and input_list == "args":
2816+
return ["list", "result", "start", "end"]
2817+
if self._cursor.get("@type"):
2818+
self._wrap_cursor_with_and()
2819+
self._cursor["@type"] = "Slice"
2820+
self._cursor["list"] = self._data_list(input_list)
2821+
self._cursor["result"] = self._clean_data_value(result_var)
2822+
if isinstance(start, int):
2823+
self._cursor["start"] = self._clean_object(start, "xsd:integer")
2824+
else:
2825+
self._cursor["start"] = self._clean_data_value(start)
2826+
if end is not None:
2827+
if isinstance(end, int):
2828+
self._cursor["end"] = self._clean_object(end, "xsd:integer")
2829+
else:
2830+
self._cursor["end"] = self._clean_data_value(end)
2831+
return self
2832+
27902833
def woql_not(self, query=None):
27912834
"""Creates a logical NOT of the arguments
27922835

0 commit comments

Comments
 (0)