Skip to content

Commit

Permalink
feat: add to_api_repr method to Model (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
HemangChothani authored Oct 14, 2020
1 parent 3be78b7 commit fb401bd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions google/cloud/bigquery/model.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit fb401bd

Please sign in to comment.