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 append(self, data): if self._list_like: self.ids.append(data)
[docs] def extend(self, data): if self._list_like: self.ids.extend(data)
[docs] def update(self, position, data_id): if self._list_like: self.ids[position] = data_id
[docs] def delete_by_id(self, _id): if self._list_like: del self.ids[self.ids.index(_id)]
[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_offset(self, position): if self._list_like: del self.ids[position]
[docs] def insert(self, position, data_id): if self._list_like: self.ids.insert(position, data_id)
[docs] def clear(self): if self._list_like: self.ids.clear()
[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]]
[docs] def save(self): pass
[docs] def load(self): pass
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)