Source code for docarray.array.storage.base.helper
from typing import Iterator, Dict
[docs]class Offset2ID:
def __init__(self, ids=None, list_like=True):
self.ids = ids or []
self._list_like = list_like
[docs] def get_id(self, idx):
if not self._list_like:
raise ValueError(
"The offset2id is not enabled for list-like indexes. To avoid this error, configure the "
"`list_like` to True"
)
return self.ids[idx]
[docs] def index(self, _id):
if not self._list_like:
raise ValueError(
"The offset2id is not enabled for list-like indexes. To avoid this error, configure the "
"`list_like` to True"
)
return self.ids.index(_id)
[docs] def delete_by_ids(self, ids):
if self._list_like:
ids = set(ids)
self.ids = list(filter(lambda _id: _id not in ids, self.ids))
[docs] def update_ids(self, _ids_map: Dict[str, str]):
if self._list_like:
for i in range(len(self.ids)):
if self.ids[i] in _ids_map:
self.ids[i] = _ids_map[self.ids[i]]
def __iter__(self) -> Iterator['str']:
yield from self.ids
def __eq__(self, other):
return self.ids == other.ids
def __len__(self):
return len(self.ids)