From 26da7a7d4c1f5db2d2c3b2faedccbd9899c14a47 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 16 Nov 2020 15:49:29 -0800 Subject: [PATCH] feat: add get_custom_job and get_hyperparameter_tuning_job samples (#68) * feat: add get_custom_job and get_hyperparameter_tuning_job samples --- samples/snippets/get_custom_job_sample.py | 36 ++++++++++++++++++ .../snippets/get_custom_job_sample_test.py | 30 +++++++++++++++ .../get_hyperparameter_tuning_job_sample.py | 38 +++++++++++++++++++ ...t_hyperparameter_tuning_job_sample_test.py | 32 ++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 samples/snippets/get_custom_job_sample.py create mode 100644 samples/snippets/get_custom_job_sample_test.py create mode 100644 samples/snippets/get_hyperparameter_tuning_job_sample.py create mode 100644 samples/snippets/get_hyperparameter_tuning_job_sample_test.py diff --git a/samples/snippets/get_custom_job_sample.py b/samples/snippets/get_custom_job_sample.py new file mode 100644 index 0000000000..4fcce9aa16 --- /dev/null +++ b/samples/snippets/get_custom_job_sample.py @@ -0,0 +1,36 @@ +# 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. + +# [START aiplatform_get_custom_job_sample] +from google.cloud import aiplatform + + +def get_custom_job_sample( + project: str, + custom_job_id: str, + location: str = "us-central1", + api_endpoint: str = "us-central1-aiplatform.googleapis.com", +): + client_options = {"api_endpoint": api_endpoint} + # Initialize client that will be used to create and send requests. + # This client only needs to be created once, and can be reused for multiple requests. + client = aiplatform.gapic.JobServiceClient(client_options=client_options) + name = client.custom_job_path( + project=project, location=location, custom_job=custom_job_id + ) + response = client.get_custom_job(name=name) + print("response:", response) + + +# [END aiplatform_get_custom_job_sample] diff --git a/samples/snippets/get_custom_job_sample_test.py b/samples/snippets/get_custom_job_sample_test.py new file mode 100644 index 0000000000..5e11f2d8da --- /dev/null +++ b/samples/snippets/get_custom_job_sample_test.py @@ -0,0 +1,30 @@ +# 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. + +import pytest +import os + +import get_custom_job_sample + +PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") +CUSTOM_JOB_ID = "7980906305281851392" +KNOWN_CUSTOM_JOB = f"/locations/us-central1/customJobs/{CUSTOM_JOB_ID}" + + +def test_ucaip_generated_get_custom_job_sample(capsys): + get_custom_job_sample.get_custom_job_sample( + project=PROJECT_ID, custom_job_id=CUSTOM_JOB_ID + ) + out, _ = capsys.readouterr() + assert KNOWN_CUSTOM_JOB in out diff --git a/samples/snippets/get_hyperparameter_tuning_job_sample.py b/samples/snippets/get_hyperparameter_tuning_job_sample.py new file mode 100644 index 0000000000..a9378533a1 --- /dev/null +++ b/samples/snippets/get_hyperparameter_tuning_job_sample.py @@ -0,0 +1,38 @@ +# 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. + +# [START aiplatform_get_hyperparameter_tuning_job_sample] +from google.cloud import aiplatform + + +def get_hyperparameter_tuning_job_sample( + project: str, + hyperparameter_tuning_job_id: str, + location: str = "us-central1", + api_endpoint: str = "us-central1-aiplatform.googleapis.com", +): + client_options = {"api_endpoint": api_endpoint} + # Initialize client that will be used to create and send requests. + # This client only needs to be created once, and can be reused for multiple requests. + client = aiplatform.gapic.JobServiceClient(client_options=client_options) + name = client.hyperparameter_tuning_job_path( + project=project, + location=location, + hyperparameter_tuning_job=hyperparameter_tuning_job_id, + ) + response = client.get_hyperparameter_tuning_job(name=name) + print("response:", response) + + +# [END aiplatform_get_hyperparameter_tuning_job_sample] diff --git a/samples/snippets/get_hyperparameter_tuning_job_sample_test.py b/samples/snippets/get_hyperparameter_tuning_job_sample_test.py new file mode 100644 index 0000000000..839d056b43 --- /dev/null +++ b/samples/snippets/get_hyperparameter_tuning_job_sample_test.py @@ -0,0 +1,32 @@ +# 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. + +import pytest +import os + +import get_hyperparameter_tuning_job_sample + +PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") +HYPERPARAMETER_TUNING_JOB_ID = "2216298782247616512" +KNOWN_HYPERPARAMETER_TUNING_JOB = ( + f"/locations/us-central1/hyperparameterTuningJobs/{HYPERPARAMETER_TUNING_JOB_ID}" +) + + +def test_ucaip_generated_get_hyperparameter_tuning_job_sample(capsys): + get_hyperparameter_tuning_job_sample.get_hyperparameter_tuning_job_sample( + project=PROJECT_ID, hyperparameter_tuning_job_id=HYPERPARAMETER_TUNING_JOB_ID + ) + out, _ = capsys.readouterr() + assert KNOWN_HYPERPARAMETER_TUNING_JOB in out