Skip to content
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

feat: add to_api_repr method to Model #326

Merged
merged 1 commit into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions google/cloud/bigquery/model.py
Expand Up @@ -317,6 +317,14 @@ def _build_resource(self, filter_fields):
def __repr__(self):
return "Model(reference={})".format(repr(self.reference))

def to_api_repr(self):
"""Construct the API resource representation of this model.

Returns:
Dict[str, object]: Model reference represented as an API resource
"""
return json_format.MessageToDict(self._proto)


class ModelReference(object):
"""ModelReferences are pointers to models.
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/model/test_model.py
Expand Up @@ -318,3 +318,47 @@ def test_repr(target_class):
"Model(reference=ModelReference("
"project_id='my-proj', dataset_id='my_dset', model_id='my_model'))"
)


def test_to_api_repr(target_class):
from google.protobuf import json_format

model = target_class("my-proj.my_dset.my_model")
resource = {
"etag": "abcdefg",
"modelReference": {
"projectId": "my-project",
"datasetId": "my_dataset",
"modelId": "my_model",
},
"creationTime": "1274284800000",
"lastModifiedTime": "1317484800000",
"modelType": "LOGISTIC_REGRESSION",
"trainingRuns": [
{
"trainingOptions": {"initialLearnRate": 1.0},
"startTime": "2010-05-19T16:00:00Z",
},
{
"trainingOptions": {"initialLearnRate": 0.5},
"startTime": "2011-10-01T16:00:00Z",
},
{
"trainingOptions": {"initialLearnRate": 0.25},
"startTime": "2012-12-21T16:00:00Z",
},
],
"description": "A friendly description.",
"location": "US",
"friendlyName": "A friendly name.",
"labels": {"greeting": "こんにちは"},
"expirationTime": "1356105600000",
"encryptionConfiguration": {
"kmsKeyName": "projects/1/locations/us/keyRings/1/cryptoKeys/1"
},
}
model._proto = json_format.ParseDict(
resource, types.Model()._pb, ignore_unknown_fields=True
)
got = model.to_api_repr()
assert got == resource