From d440d6a7d1ed4d07e1e0dae59333fe7bb8b30756 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 16 Nov 2020 13:51:16 -0800 Subject: [PATCH 1/2] 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 | 28 ++++++++++++++ .../get_hyperparameter_tuning_job_sample.py | 38 +++++++++++++++++++ ...t_hyperparameter_tuning_job_sample_test.py | 28 ++++++++++++++ 4 files changed, 130 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..7ec817b30b --- /dev/null +++ b/samples/snippets/get_custom_job_sample_test.py @@ -0,0 +1,28 @@ +# 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 = "TODO" +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..47f46637b3 --- /dev/null +++ b/samples/snippets/get_hyperparameter_tuning_job_sample_test.py @@ -0,0 +1,28 @@ +# 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 = "TODO" +KNOWN_HYPERPARAMETER_TUNING_JOB = f"/locations/us-central1/hyperparameterTuningJobs/{CUSTOM_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=CUSTOM_JOB_ID) + out, _ = capsys.readouterr() + assert KNOWN_HYPERPARAMETER_TUNING_JOB in out From 43aa8ac722f9e067d5a67e5b5367cd4a90527034 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 16 Nov 2020 14:21:05 -0800 Subject: [PATCH 2/2] update sample tests --- samples/snippets/get_custom_job_sample_test.py | 6 ++++-- .../get_hyperparameter_tuning_job_sample_test.py | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/samples/snippets/get_custom_job_sample_test.py b/samples/snippets/get_custom_job_sample_test.py index 7ec817b30b..5e11f2d8da 100644 --- a/samples/snippets/get_custom_job_sample_test.py +++ b/samples/snippets/get_custom_job_sample_test.py @@ -18,11 +18,13 @@ import get_custom_job_sample PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") -CUSTOM_JOB_ID = "TODO" +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) + 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_test.py b/samples/snippets/get_hyperparameter_tuning_job_sample_test.py index 47f46637b3..839d056b43 100644 --- a/samples/snippets/get_hyperparameter_tuning_job_sample_test.py +++ b/samples/snippets/get_hyperparameter_tuning_job_sample_test.py @@ -18,11 +18,15 @@ import get_hyperparameter_tuning_job_sample PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") -HYPERPARAMETER_TUNING_JOB_ID = "TODO" -KNOWN_HYPERPARAMETER_TUNING_JOB = f"/locations/us-central1/hyperparameterTuningJobs/{CUSTOM_JOB_ID}" +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=CUSTOM_JOB_ID) + 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