Skip to content

Commit 5626b4e

Browse files
author
Val Brodsky
committed
Add method to CatalogSlice to get data row identifiers (both uids and global keys)
Also: Make return typed Add deprecation warning to get_data_row_ids
1 parent ee1d147 commit 5626b4e

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

labelbox/schema/slice.py

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from typing import Optional, List
2-
from labelbox.exceptions import ResourceNotFoundError
1+
from dataclasses import dataclass
2+
from typing import Optional
3+
import warnings
34
from labelbox.orm.db_object import DbObject, experimental
4-
from labelbox.orm.model import Entity, Field
5+
from labelbox.orm.model import Field
56
from labelbox.pagination import PaginatedCollection
67
from labelbox.schema.export_params import CatalogExportParams, validate_catalog_export_params
78
from labelbox.schema.export_task import ExportTask
9+
from labelbox.schema.identifiable import GlobalKey, UniqueId
810
from labelbox.schema.task import Task
9-
from labelbox.schema.user import User
1011

1112

1213
class Slice(DbObject):
@@ -34,13 +35,27 @@ class CatalogSlice(Slice):
3435
Represents a Slice used for filtering data rows in Catalog.
3536
"""
3637

38+
@dataclass
39+
class DataRowIdAndGlobalKey:
40+
id: UniqueId
41+
global_key: Optional[GlobalKey]
42+
43+
def __init__(self, id: str, global_key: Optional[str]):
44+
self.id = UniqueId(id)
45+
self.global_key = GlobalKey(global_key) if global_key else None
46+
3747
def get_data_row_ids(self) -> PaginatedCollection:
3848
"""
3949
Fetches all data row ids that match this Slice
4050
4151
Returns:
42-
A PaginatedCollection of data row ids
52+
A PaginatedCollection of mapping of data row ids to global keys
4353
"""
54+
55+
warnings.warn(
56+
"get_data_row_ids will be deprecated. Use get_data_row_identifiers instead"
57+
)
58+
4459
query_str = """
4560
query getDataRowIdsBySavedQueryPyApi($id: ID!, $from: String, $first: Int!) {
4661
getDataRowIdsBySavedQuery(input: {
@@ -65,6 +80,45 @@ def get_data_row_ids(self) -> PaginatedCollection:
6580
obj_class=lambda _, data_row_id: data_row_id,
6681
cursor_path=['getDataRowIdsBySavedQuery', 'pageInfo', 'endCursor'])
6782

83+
def get_data_row_identifiers(self) -> PaginatedCollection:
84+
"""
85+
Fetches all data row ids and global keys (where defined) that match this Slice
86+
87+
Returns:
88+
A PaginatedCollection of data row ids
89+
"""
90+
query_str = """
91+
query getDataRowIdenfifiersBySavedQueryPyApi($id: ID!, $from: String, $first: Int!) {
92+
getDataRowIdentifiersBySavedQuery(input: {
93+
savedQueryId: $id,
94+
after: $from
95+
first: $first
96+
}) {
97+
totalCount
98+
nodes
99+
{
100+
id
101+
globalKey
102+
}
103+
pageInfo {
104+
endCursor
105+
hasNextPage
106+
}
107+
}
108+
}
109+
"""
110+
return PaginatedCollection(
111+
client=self.client,
112+
query=query_str,
113+
params={'id': str(self.uid)},
114+
dereferencing=['getDataRowIdentifiersBySavedQuery', 'nodes'],
115+
obj_class=lambda _, data_row_id_and_gk: CatalogSlice.
116+
DataRowIdAndGlobalKey(data_row_id_and_gk.get('id'),
117+
data_row_id_and_gk.get('globalKey', None)),
118+
cursor_path=[
119+
'getDataRowIdentifiersBySavedQuery', 'pageInfo', 'endCursor'
120+
])
121+
68122
@experimental
69123
def export(self,
70124
task_name: Optional[str] = None,

0 commit comments

Comments
 (0)