|
5 | 5 | import os |
6 | 6 | import time |
7 | 7 |
|
8 | | -from labelbox.exceptions import InvalidQueryError |
| 8 | +from labelbox.exceptions import InvalidQueryError, ResourceNotFoundError |
9 | 9 | from labelbox.orm import query |
10 | 10 | from labelbox.orm.db_object import (DbObject, Updateable, Deletable, |
11 | 11 | BulkDeletable) |
@@ -205,9 +205,11 @@ def create_data_rows(self, items): |
205 | 205 |
|
206 | 206 | Args: |
207 | 207 | items (iterable of (dict or str)): See above for details. |
| 208 | +
|
208 | 209 | Return: |
209 | 210 | Task representing the data import on the server side. The Task |
210 | 211 | can be used for inspecting task progress and waiting until it's done. |
| 212 | +
|
211 | 213 | Raise: |
212 | 214 | InvalidQueryError: if the `items` parameter does not conform to |
213 | 215 | the specification above. |
@@ -278,6 +280,32 @@ def convert_item(item): |
278 | 280 | task._user = user |
279 | 281 | return task |
280 | 282 |
|
| 283 | + def data_row_for_external_id(self, external_id): |
| 284 | + """ Convenience method for getting a single `DataRow` belonging to this |
| 285 | + `Dataset` that has the given `external_id`. |
| 286 | +
|
| 287 | + Args: |
| 288 | + external_id (str): External ID of the sought `DataRow`. |
| 289 | +
|
| 290 | + Returns: |
| 291 | + A single `DataRow` with the given ID. |
| 292 | +
|
| 293 | + Raises: |
| 294 | + labelbox.exceptions.ResourceNotFoundError: if there is no `DataRow` |
| 295 | + in this `DataSet` with the given external ID, or if there are |
| 296 | + multiple `DataRows` for it. |
| 297 | + """ |
| 298 | + where = DataRow.external_id==external_id |
| 299 | + |
| 300 | + data_rows = self.data_rows(where=where) |
| 301 | + # Get at most two data_rows. |
| 302 | + data_rows = [row for row, _ in zip(data_rows, range(2))] |
| 303 | + |
| 304 | + if len(data_rows) != 1: |
| 305 | + raise ResourceNotFoundError(DataRow, where) |
| 306 | + |
| 307 | + return data_rows[0] |
| 308 | + |
281 | 309 |
|
282 | 310 | class DataRow(DbObject, Updateable, BulkDeletable): |
283 | 311 | external_id = Field.String("external_id") |
|
0 commit comments