Skip to content

Commit b5504db

Browse files
author
Val Brodsky
committed
Implement Identifiables support for indexing and slicing
1 parent 8584745 commit b5504db

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

labelbox/schema/identifiables.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@ def __init__(self, iterable, id_type: IdType):
1212
id_type: The type of id used to identify a data row.
1313
"""
1414
self._iterable = iterable
15-
self._index = 0
1615
self._id_type = id_type
1716

1817
def __iter__(self):
1918
return iter(self._iterable)
2019

20+
def __getitem__(self, index):
21+
if isinstance(index, slice):
22+
ids = self._iterable[index]
23+
return self.__class__(ids) # type: ignore
24+
return self._iterable[index]
25+
26+
def __len__(self):
27+
return len(self._iterable)
28+
2129
def __repr__(self) -> str:
2230
return f"{self.__class__.__name__}({self._iterable})"
2331

32+
def __eq__(self, other: 'Identifiables') -> bool:
33+
if not isinstance(other, Identifiables):
34+
return False
35+
return self._iterable == other._iterable and self._id_type == other._id_type
36+
2437

2538
class UniqueIds(Identifiables):
2639
"""

tests/unit/test_unit_identifiables.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ def test_unique_ids():
66
identifiables = UniqueIds(ids)
77
assert [i for i in identifiables] == ids
88
assert identifiables._id_type == "ID"
9+
assert len(identifiables) == 3
910

1011

1112
def test_global_keys():
1213
ids = ["a", "b", "c"]
1314
identifiables = GlobalKeys(ids)
1415
assert [i for i in identifiables] == ids
1516
assert identifiables._id_type == "GKEY"
17+
assert len(identifiables) == 3
18+
19+
20+
def test_index_access():
21+
ids = ["a", "b", "c"]
22+
identifiables = GlobalKeys(ids)
23+
assert identifiables[0] == "a"
24+
assert identifiables[1:3] == GlobalKeys(["b", "c"])
1625

1726

1827
def test_repr():

0 commit comments

Comments
 (0)