-
Notifications
You must be signed in to change notification settings - Fork 68
[AL-2496] [AL-2498] Rename custom_metadata to metadata_fields for DataRow #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,40 +52,57 @@ class Dataset(DbObject, Updateable, Deletable): | |
| iam_integration = Relationship.ToOne("IAMIntegration", False, | ||
| "iam_integration", "signer") | ||
|
|
||
| def create_data_row(self, **kwargs) -> "DataRow": | ||
| def create_data_row(self, items=None, **kwargs) -> "DataRow": | ||
| """ Creates a single DataRow belonging to this dataset. | ||
|
|
||
| >>> dataset.create_data_row(row_data="http://my_site.com/photos/img_01.jpg") | ||
|
|
||
| Args: | ||
| items: Dictionary containing new `DataRow` data. At a minimum, | ||
| must contain `row_data` or `DataRow.row_data`. | ||
| **kwargs: Key-value arguments containing new `DataRow` data. At a minimum, | ||
| must contain `row_data`. | ||
|
|
||
| Raises: | ||
| InvalidQueryError: If both dictionary and `kwargs` are provided as inputs | ||
| InvalidQueryError: If `DataRow.row_data` field value is not provided | ||
| in `kwargs`. | ||
| InvalidAttributeError: in case the DB object type does not contain | ||
| any of the field names given in `kwargs`. | ||
|
|
||
| """ | ||
| invalid_argument_error = "Argument to create_data_row() must be either a dictionary, or kwargs containing `row_data` at minimum" | ||
|
|
||
| def convert_field_keys(items): | ||
| if not isinstance(items, dict): | ||
| raise InvalidQueryError(invalid_argument_error) | ||
| return { | ||
| key.name if isinstance(key, Field) else key: value | ||
| for key, value in items.items() | ||
| } | ||
|
|
||
| if items is not None and len(kwargs) > 0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| raise InvalidQueryError(invalid_argument_error) | ||
|
|
||
| DataRow = Entity.DataRow | ||
| if DataRow.row_data.name not in kwargs: | ||
| args = convert_field_keys(items) if items is not None else kwargs | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if |
||
|
|
||
| if DataRow.row_data.name not in args: | ||
| raise InvalidQueryError( | ||
| "DataRow.row_data missing when creating DataRow.") | ||
|
|
||
| # If row data is a local file path, upload it to server. | ||
| row_data = kwargs[DataRow.row_data.name] | ||
| row_data = args[DataRow.row_data.name] | ||
| if os.path.exists(row_data): | ||
| kwargs[DataRow.row_data.name] = self.client.upload_file(row_data) | ||
| kwargs[DataRow.dataset.name] = self | ||
| args[DataRow.row_data.name] = self.client.upload_file(row_data) | ||
| args[DataRow.dataset.name] = self | ||
|
|
||
| # Parse metadata fields, if they are provided | ||
| if DataRow.custom_metadata.name in kwargs: | ||
| if DataRow.metadata_fields.name in args: | ||
| mdo = self.client.get_data_row_metadata_ontology() | ||
| kwargs[DataRow.custom_metadata.name] = mdo.parse_upsert_metadata( | ||
| kwargs[DataRow.custom_metadata.name]) | ||
|
|
||
| return self.client._create(DataRow, kwargs) | ||
| args[DataRow.metadata_fields.name] = mdo.parse_upsert_metadata( | ||
| args[DataRow.metadata_fields.name]) | ||
| return self.client._create(DataRow, args) | ||
|
|
||
| def create_data_rows_sync(self, items) -> None: | ||
| """ Synchronously bulk upload data rows. | ||
|
|
@@ -264,10 +281,10 @@ def validate_attachments(item): | |
| return attachments | ||
|
|
||
| def parse_metadata_fields(item): | ||
| metadata_fields = item.get('custom_metadata') | ||
| metadata_fields = item.get('metadata_fields') | ||
| if metadata_fields: | ||
| mdo = self.client.get_data_row_metadata_ontology() | ||
| item['custom_metadata'] = mdo.parse_upsert_metadata( | ||
| item['metadata_fields'] = mdo.parse_upsert_metadata( | ||
| metadata_fields) | ||
|
|
||
| def format_row(item): | ||
|
|
@@ -413,9 +430,9 @@ def export_data_rows(self, timeout_seconds=120) -> Generator: | |
| response = requests.get(download_url) | ||
| response.raise_for_status() | ||
| reader = ndjson.reader(StringIO(response.text)) | ||
| # TODO: Update result to parse customMetadata when resolver returns | ||
| # TODO: Update result to parse metadataFields when resolver returns | ||
| return (Entity.DataRow(self.client, { | ||
| **result, 'customMetadata': [] | ||
| **result, 'metadataFields': [] | ||
| }) for result in reader) | ||
| elif res["status"] == "FAILED": | ||
| raise LabelboxError("Data row export failed.") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I maybe move this out of the function scope and into module scope because this seems like a more general utility function type thing and doesn't need to be re-defined every time this method is called