Skip to content

Commit 1eaa730

Browse files
author
Val Brodsky
committed
Add Identifiables as iterator
1 parent 0a5d824 commit 1eaa730

File tree

8 files changed

+65
-103
lines changed

8 files changed

+65
-103
lines changed

labelbox/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
from labelbox.schema.slice import Slice, CatalogSlice, ModelSlice
3434
from labelbox.schema.queue_mode import QueueMode
3535
from labelbox.schema.task_queue import TaskQueue
36-
from labelbox.schema.identifiable import UniqueIds, GlobalKeys
36+
from labelbox.schema.identifiables import UniqueIds, GlobalKeys

labelbox/schema/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
import labelbox.schema.batch
2222
import labelbox.schema.iam_integration
2323
import labelbox.schema.media_type
24-
import labelbox.schema.identifiable
24+
import labelbox.schema.identifiables

labelbox/schema/identifiable.py

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,29 @@
11
from abc import ABC, abstractmethod
2-
from enum import Enum
32
from typing import List, Union
43

54

6-
class IdType(str, Enum):
7-
DataRowId = "UID"
8-
GlobalKey = "GLOBAL_KEY"
9-
10-
115
class Identifiable(ABC):
126

13-
def __init__(self, keys: Union[str, List[str]], id_type: IdType):
14-
self._keys = keys
15-
if isinstance(keys, str):
16-
self._keys = [keys]
17-
self._id_type = id_type
7+
def __init__(self, key: str):
8+
self._key = key
189

1910
@property
20-
def keys(self):
21-
return self._keys
22-
23-
@keys.setter
24-
def keys(self, keys):
25-
self._keys = keys
26-
if isinstance(keys, str):
27-
self._keys = [keys]
28-
29-
@classmethod
30-
@abstractmethod
31-
def strings_to_identifiable(cls, keys: Union[str, List[str]]):
32-
pass
11+
def key(self):
12+
return self.key
3313

3414
def __eq__(self, other):
35-
return other.keys == self.keys
15+
return other.key == self.key
3616

3717
def __hash__(self):
38-
hash(self.keys)
18+
hash(self.key)
3919

4020
def __str__(self):
41-
return self.keys.__str__()
42-
43-
44-
class UniqueIds(Identifiable):
45-
46-
@classmethod
47-
def strings_to_identifiable(cls, keys: Union[str, List[str]]):
48-
return cls(keys)
49-
50-
def __init__(self, keys: Union[str, List[str]]):
51-
super().__init__(keys, IdType.DataRowId)
52-
53-
54-
class GlobalKeys(Identifiable):
21+
return self.key.__str__()
5522

56-
@classmethod
57-
def strings_to_identifiable(cls, keys: Union[str, List[str]]):
58-
return cls(keys)
5923

60-
def __init__(self, keys: Union[str, List[str]]):
61-
super().__init__(keys, IdType.GlobalKey)
24+
class UniqueId(Identifiable):
25+
pass
6226

6327

64-
DefaultIdentifiable = UniqueIds
65-
DataRowIdentifiers = Union[UniqueIds, GlobalKeys]
28+
class GlobalKey(Identifiable):
29+
pass

labelbox/schema/identifiables.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from enum import Enum
2+
from typing import List, Union
3+
4+
5+
class IdType(str, Enum):
6+
DataRowId = "UID"
7+
GlobalKey = "GLOBAL_KEY"
8+
9+
10+
class Identifiables:
11+
12+
def __init__(self, iterable, id_type: IdType):
13+
self._iterable = iterable
14+
self._index = 0
15+
self._id_type = id_type
16+
17+
def __iter__(self):
18+
return iter(self._iterable)
19+
20+
21+
class UniqueIds(Identifiables):
22+
23+
def __init__(self, iterable: List[str]):
24+
super().__init__(iterable, IdType.DataRowId)
25+
26+
27+
class GlobalKeys(Identifiables):
28+
29+
def __init__(self, iterable: List[str]):
30+
super().__init__(iterable, IdType.GlobalKey)
31+
32+
33+
DataRowIdentifiers = Union[UniqueIds, GlobalKeys]

labelbox/schema/project.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from labelbox.schema.export_filters import ProjectExportFilters, validate_datetime, build_filters
2929
from labelbox.schema.export_params import ProjectExportParams
3030
from labelbox.schema.export_task import ExportTask
31-
from labelbox.schema.identifiable import DataRowIdentifiers, UniqueIds
31+
from labelbox.schema.identifiables import DataRowIdentifiers, UniqueIds
3232
from labelbox.schema.media_type import MediaType
3333
from labelbox.schema.queue_mode import QueueMode
3434
from labelbox.schema.resource_tag import ResourceTag
@@ -1403,7 +1403,7 @@ def move_data_rows_to_task_queue(self, data_row_ids, task_queue_id: str):
14031403
14041404
"""
14051405
if isinstance(data_row_ids, list):
1406-
data_row_ids = UniqueIds.strings_to_identifiable(data_row_ids)
1406+
data_row_ids = UniqueIds(data_row_ids)
14071407
warnings.warn("Using data row ids will be deprecated. Please use "
14081408
"UniqueIds or GlobalKeys instead.")
14091409

@@ -1428,7 +1428,7 @@ def move_data_rows_to_task_queue(self, data_row_ids, task_queue_id: str):
14281428
"projectId": self.uid,
14291429
"queueId": task_queue_id,
14301430
"dataRowIdentifiers": {
1431-
"ids": data_row_ids.keys,
1431+
"ids": [id for id in data_row_ids],
14321432
"idType": data_row_ids._id_type,
14331433
},
14341434
},

tests/integration/test_task_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22

33
from labelbox import Project
4-
from labelbox.schema.identifiable import GlobalKeys
4+
from labelbox.schema.identifiables import GlobalKeys
55

66

77
def test_get_task_queue(project: Project):

tests/unit/test_unit_identifiable.py

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from labelbox.schema.identifiables import GlobalKeys, UniqueIds
2+
3+
4+
def test_unique_ids():
5+
ids = ["a", "b", "c"]
6+
identifiables = UniqueIds(ids)
7+
assert [i for i in identifiables] == ids
8+
assert identifiables._id_type == "UID"
9+
10+
11+
def test_global_keys():
12+
ids = ["a", "b", "c"]
13+
identifiables = GlobalKeys(ids)
14+
assert [i for i in identifiables] == ids
15+
assert identifiables._id_type == "GLOBAL_KEY"

0 commit comments

Comments
 (0)