-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds function/method enhancements, demo samples (#122)
* feat: adds function/method enhancements
- Loading branch information
Showing
17 changed files
with
331 additions
and
39 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from google.cloud.aiplatform.helpers import value_converter | ||
|
||
__all__ = (value_converter,) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from __future__ import absolute_import | ||
from google.cloud.aiplatform.helpers import value_converter | ||
|
||
from proto.marshal import Marshal | ||
from proto.marshal.rules.struct import ValueRule | ||
from google.protobuf.struct_pb2 import Value | ||
|
||
|
||
class ConversionValueRule(ValueRule): | ||
def to_python(self, value, *, absent: bool = None): | ||
return super().to_python(value, absent=absent) | ||
|
||
def to_proto(self, value): | ||
|
||
# Need to check whether value is an instance | ||
# of an enhanced type | ||
if callable(getattr(value, "to_value", None)): | ||
return value.to_value() | ||
else: | ||
return super().to_proto(value) | ||
|
||
|
||
def _add_methods_to_classes_in_package(pkg): | ||
classes = dict( | ||
[(name, cls) for name, cls in pkg.__dict__.items() if isinstance(cls, type)] | ||
) | ||
|
||
for class_name, cls in classes.items(): | ||
# Add to_value() method to class with docstring | ||
setattr(cls, "to_value", value_converter.to_value) | ||
cls.to_value.__doc__ = value_converter.to_value.__doc__ | ||
|
||
# Add from_value() method to class with docstring | ||
setattr(cls, "from_value", _add_from_value_to_class(cls)) | ||
cls.from_value.__doc__ = value_converter.from_value.__doc__ | ||
|
||
# Add from_map() method to class with docstring | ||
setattr(cls, "from_map", _add_from_map_to_class(cls)) | ||
cls.from_map.__doc__ = value_converter.from_map.__doc__ | ||
|
||
|
||
def _add_from_value_to_class(cls): | ||
def _from_value(value): | ||
return value_converter.from_value(cls, value) | ||
|
||
return _from_value | ||
|
||
|
||
def _add_from_map_to_class(cls): | ||
def _from_map(map_): | ||
return value_converter.from_map(cls, map_) | ||
|
||
return _from_map | ||
|
||
|
||
marshal = Marshal(name="google.cloud.aiplatform.v1beta1") | ||
marshal.register(Value, ConversionValueRule(marshal=marshal)) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from __future__ import absolute_import | ||
from google.protobuf.struct_pb2 import Value | ||
from google.protobuf import json_format | ||
from proto.marshal.collections.maps import MapComposite | ||
from proto.marshal import Marshal | ||
from proto import Message | ||
from proto.message import MessageMeta | ||
|
||
|
||
def to_value(self: Message) -> Value: | ||
"""Converts a message type to a :class:`~google.protobuf.struct_pb2.Value` object. | ||
Args: | ||
message: the message to convert | ||
Returns: | ||
the message as a :class:`~google.protobuf.struct_pb2.Value` object | ||
""" | ||
tmp_dict = json_format.MessageToDict(self._pb) | ||
return json_format.ParseDict(tmp_dict, Value()) | ||
|
||
|
||
def from_value(cls: MessageMeta, value: Value) -> Message: | ||
"""Creates instance of class from a :class:`~google.protobuf.struct_pb2.Value` object. | ||
Args: | ||
value: a :class:`~google.protobuf.struct_pb2.Value` object | ||
Returns: | ||
Instance of class | ||
""" | ||
value_dict = json_format.MessageToDict(value) | ||
return json_format.ParseDict(value_dict, cls()._pb) | ||
|
||
|
||
def from_map(cls: MessageMeta, map_: MapComposite) -> Message: | ||
"""Creates instance of class from a :class:`~proto.marshal.collections.maps.MapComposite` object. | ||
Args: | ||
map_: a :class:`~proto.marshal.collections.maps.MapComposite` object | ||
Returns: | ||
Instance of class | ||
""" | ||
marshal = Marshal(name="marshal") | ||
pb = marshal.to_proto(Value, map_) | ||
return from_value(cls, pb) |
This file contains 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.