|
1 | 1 | from typing import Generator |
2 | 2 | from labelbox.orm.db_object import DbObject, experimental |
| 3 | +from labelbox.orm import query |
3 | 4 | from labelbox.orm.model import Entity, Field, Relationship |
4 | | -from labelbox.exceptions import LabelboxError |
| 5 | +from labelbox.exceptions import LabelboxError, ResourceNotFoundError |
5 | 6 | from io import StringIO |
6 | 7 | import ndjson |
7 | 8 | import requests |
@@ -30,9 +31,49 @@ class Batch(DbObject): |
30 | 31 | size = Field.Int("size") |
31 | 32 |
|
32 | 33 | # Relationships |
33 | | - project = Relationship.ToOne("Project") |
34 | 34 | created_by = Relationship.ToOne("User") |
35 | 35 |
|
| 36 | + def __init__(self, client, project_id, *args, **kwargs): |
| 37 | + super().__init__(client, *args, **kwargs) |
| 38 | + self.project_id = project_id |
| 39 | + |
| 40 | + def project(self) -> Entity.Project: |
| 41 | + """ Returns Project which this Batch belongs to |
| 42 | +
|
| 43 | + Raises: |
| 44 | + LabelboxError: if the project is not found |
| 45 | + """ |
| 46 | + query_str = """query getProjectPyApi($projectId: ID!) { |
| 47 | + project( |
| 48 | + where: {id: $projectId}){ |
| 49 | + %s |
| 50 | + }}""" % query.results_query_part(Entity.Project) |
| 51 | + params = {"projectId": self.project_id} |
| 52 | + response = self.client.execute(query_str, params) |
| 53 | + |
| 54 | + if response is None: |
| 55 | + raise ResourceNotFoundError(Entity.Project, params) |
| 56 | + |
| 57 | + return Entity.Project(self.client, response["project"]) |
| 58 | + |
| 59 | + def remove_queued_data_rows(self) -> None: |
| 60 | + """ Removes remaining queued data rows from the batch and labeling queue. |
| 61 | +
|
| 62 | + Args: |
| 63 | + batch (Batch): Batch to remove queued data rows from |
| 64 | + """ |
| 65 | + |
| 66 | + project_id_param = "projectId" |
| 67 | + batch_id_param = "batchId" |
| 68 | + self.client.execute("""mutation ArchiveBatchPyApi($%s: ID!, $%s: ID!) { |
| 69 | + project(where: {id: $%s}) { archiveBatch(batchId: $%s) { id archivedAt } } |
| 70 | + }""" % (project_id_param, batch_id_param, project_id_param, |
| 71 | + batch_id_param), { |
| 72 | + project_id_param: self.project_id, |
| 73 | + batch_id_param: self.uid |
| 74 | + }, |
| 75 | + experimental=True) |
| 76 | + |
36 | 77 | def export_data_rows(self, timeout_seconds=120) -> Generator: |
37 | 78 | """ Returns a generator that produces all data rows that are currently |
38 | 79 | in this batch. |
|
0 commit comments