Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit b2e9343

Browse files
kurtisvgDoug Mahugh
authored and
Doug Mahugh
committed
Update datalabeling samples to hit test endpoint. [(#2641)](GoogleCloudPlatform/python-docs-samples#2641)
1 parent 92150bb commit b2e9343

13 files changed

+148
-2
lines changed

samples/snippets/create_annotation_spec_set.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import os
19+
from google.api_core.client_options import ClientOptions
1820

1921

2022
# [START datalabeling_create_annotation_spec_set_beta]
@@ -24,6 +26,13 @@ def create_annotation_spec_set(project_id):
2426
"""
2527
from google.cloud import datalabeling_v1beta1 as datalabeling
2628
client = datalabeling.DataLabelingServiceClient()
29+
# [END datalabeling_create_annotation_spec_set_beta]
30+
# If provided, use a provided test endpoint - this will prevent tests on
31+
# this snippet from triggering any action by a real human
32+
if 'DATALABELING_ENDPOINT' in os.environ:
33+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
34+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
35+
# [START datalabeling_create_annotation_spec_set_beta]
2736

2837
project_path = client.project_path(project_id)
2938

samples/snippets/create_annotation_spec_set_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import create_annotation_spec_set
2020
from google.cloud import datalabeling_v1beta1 as datalabeling
21+
from google.api_core.client_options import ClientOptions
2122
import pytest
2223

2324
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
@@ -33,4 +34,11 @@ def test_create_annotation_spec_set(capsys):
3334
# Delete the created annotation spec set.
3435
annotation_spec_set_name = response.name
3536
client = datalabeling.DataLabelingServiceClient()
37+
38+
# If provided, use a provided test endpoint - this will prevent tests on
39+
# this snippet from triggering any action by a real human
40+
if 'DATALABELING_ENDPOINT' in os.environ:
41+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
42+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
43+
3644
client.delete_annotation_spec_set(annotation_spec_set_name)

samples/snippets/create_instruction.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import os
19+
from google.api_core.client_options import ClientOptions
1820

1921

2022
# [START datalabeling_create_instruction_beta]
@@ -25,6 +27,13 @@ def create_instruction(project_id, data_type, instruction_gcs_uri):
2527
"""
2628
from google.cloud import datalabeling_v1beta1 as datalabeling
2729
client = datalabeling.DataLabelingServiceClient()
30+
# [END datalabeling_create_instruction_beta]
31+
# If provided, use a provided test endpoint - this will prevent tests on
32+
# this snippet from triggering any action by a real human
33+
if 'DATALABELING_ENDPOINT' in os.environ:
34+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
35+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
36+
# [START datalabeling_create_instruction_beta]
2837

2938
project_path = client.project_path(project_id)
3039

samples/snippets/create_instruction_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818

1919
import create_instruction
20+
from google.api_core.client_options import ClientOptions
2021
from google.cloud import datalabeling_v1beta1 as datalabeling
2122
import pytest
2223

@@ -38,4 +39,11 @@ def test_create_instruction(capsys):
3839
# Delete the created instruction.
3940
instruction_name = result.name
4041
client = datalabeling.DataLabelingServiceClient()
42+
43+
# If provided, use a provided test endpoint - this will prevent tests on
44+
# this snippet from triggering any action by a real human
45+
if 'DATALABELING_ENDPOINT' in os.environ:
46+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
47+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
48+
4149
client.delete_instruction(instruction_name)

samples/snippets/export_data.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import os
19+
from google.api_core.client_options import ClientOptions
1820

1921

2022
# [START datalabeling_export_data_beta]
@@ -23,6 +25,12 @@ def export_data(dataset_resource_name, annotated_dataset_resource_name,
2325
"""Exports a dataset from the given Google Cloud project."""
2426
from google.cloud import datalabeling_v1beta1 as datalabeling
2527
client = datalabeling.DataLabelingServiceClient()
28+
# [END datalabeling_export_data_beta]
29+
# If provided, use a provided test endpoint - this will prevent tests on
30+
# this snippet from triggering any action by a real human
31+
if 'DATALABELING_ENDPOINT' in os.environ:
32+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
33+
# [START datalabeling_export_data_beta]
2634

2735
gcs_destination = datalabeling.types.GcsDestination(
2836
output_uri=export_gcs_uri, mime_type='text/csv')

samples/snippets/import_data.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import os
19+
from google.api_core.client_options import ClientOptions
1820

1921

2022
# [START datalabeling_import_data_beta]
2123
def import_data(dataset_resource_name, data_type, input_gcs_uri):
2224
"""Imports data to the given Google Cloud project and dataset."""
2325
from google.cloud import datalabeling_v1beta1 as datalabeling
2426
client = datalabeling.DataLabelingServiceClient()
27+
# [END datalabeling_import_data_beta]
28+
# If provided, use a provided test endpoint - this will prevent tests on
29+
# this snippet from triggering any action by a real human
30+
if 'DATALABELING_ENDPOINT' in os.environ:
31+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
32+
# [START datalabeling_import_data_beta]
2533

2634
gcs_source = datalabeling.types.GcsSource(
2735
input_uri=input_gcs_uri, mime_type='text/csv')

samples/snippets/label_image.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import os
19+
from google.api_core.client_options import ClientOptions
1820

1921

2022
# [START datalabeling_label_image_beta]
@@ -23,6 +25,12 @@ def label_image(dataset_resource_name, instruction_resource_name,
2325
"""Labels an image dataset."""
2426
from google.cloud import datalabeling_v1beta1 as datalabeling
2527
client = datalabeling.DataLabelingServiceClient()
28+
# [END datalabeling_label_image_beta]
29+
# If provided, use a provided test endpoint - this will prevent tests on
30+
# this snippet from triggering any action by a real human
31+
if 'DATALABELING_ENDPOINT' in os.environ:
32+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
33+
# [START datalabeling_label_image_beta]
2634

2735
basic_config = datalabeling.types.HumanAnnotationConfig(
2836
instruction=instruction_resource_name,

samples/snippets/label_image_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import create_annotation_spec_set
2020
import create_instruction
21+
from google.api_core.client_options import ClientOptions
2122
from google.cloud import datalabeling_v1beta1 as datalabeling
2223
import import_data
2324
import label_image
@@ -52,6 +53,13 @@ def annotation_spec_set():
5253

5354
# tear down
5455
client = datalabeling.DataLabelingServiceClient()
56+
57+
# If provided, use a provided test endpoint - this will prevent tests on
58+
# this snippet from triggering any action by a real human
59+
if 'DATALABELING_ENDPOINT' in os.environ:
60+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
61+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
62+
5563
client.delete_annotation_spec_set(response.name)
5664

5765

@@ -66,6 +74,13 @@ def instruction():
6674

6775
# tear down
6876
client = datalabeling.DataLabelingServiceClient()
77+
78+
# If provided, use a provided test endpoint - this will prevent tests on
79+
# this snippet from triggering any action by a real human
80+
if 'DATALABELING_ENDPOINT' in os.environ:
81+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
82+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
83+
6984
client.delete_instruction(instruction.name)
7085

7186

@@ -89,5 +104,12 @@ def test_label_image(capsys, annotation_spec_set, instruction, dataset):
89104
assert response.cancelled() is True
90105

91106
client = datalabeling.DataLabelingServiceClient()
107+
108+
# If provided, use a provided test endpoint - this will prevent tests on
109+
# this snippet from triggering any action by a real human
110+
if 'DATALABELING_ENDPOINT' in os.environ:
111+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
112+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
113+
92114
client.transport._operations_client.cancel_operation(
93115
operation_name)

samples/snippets/label_text.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
# limitations under the License.
1616

1717
import argparse
18-
18+
import os
19+
from google.api_core.client_options import ClientOptions
1920

2021
# [START datalabeling_label_text_beta]
2122
def label_text(dataset_resource_name, instruction_resource_name,
2223
annotation_spec_set_resource_name):
2324
"""Labels a text dataset."""
2425
from google.cloud import datalabeling_v1beta1 as datalabeling
2526
client = datalabeling.DataLabelingServiceClient()
27+
# [END datalabeling_export_data_beta]
28+
# If provided, use a provided test endpoint - this will prevent tests on
29+
# this snippet from triggering any action by a real human
30+
if 'DATALABELING_ENDPOINT' in os.environ:
31+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
32+
# [START datalabeling_export_data_beta]
2633

2734
basic_config = datalabeling.types.HumanAnnotationConfig(
2835
instruction=instruction_resource_name,

samples/snippets/label_text_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import create_annotation_spec_set
2020
import create_instruction
2121
from google.cloud import datalabeling_v1beta1 as datalabeling
22+
from google.api_core.client_options import ClientOptions
2223
import import_data
2324
import label_text
2425
import manage_dataset
@@ -52,6 +53,13 @@ def annotation_spec_set():
5253

5354
# tear down
5455
client = datalabeling.DataLabelingServiceClient()
56+
57+
# If provided, use a provided test endpoint - this will prevent tests on
58+
# this snippet from triggering any action by a real human
59+
if 'DATALABELING_ENDPOINT' in os.environ:
60+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
61+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
62+
5563
client.delete_annotation_spec_set(response.name)
5664

5765

@@ -66,6 +74,13 @@ def instruction():
6674

6775
# tear down
6876
client = datalabeling.DataLabelingServiceClient()
77+
78+
# If provided, use a provided test endpoint - this will prevent tests on
79+
# this snippet from triggering any action by a real human
80+
if 'DATALABELING_ENDPOINT' in os.environ:
81+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
82+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
83+
6984
client.delete_instruction(instruction.name)
7085

7186

@@ -89,5 +104,12 @@ def test_label_text(capsys, annotation_spec_set, instruction, dataset):
89104
assert response.cancelled() is True
90105

91106
client = datalabeling.DataLabelingServiceClient()
107+
108+
# If provided, use a provided test endpoint - this will prevent tests on
109+
# this snippet from triggering any action by a real human
110+
if 'DATALABELING_ENDPOINT' in os.environ:
111+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
112+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
113+
92114
client.transport._operations_client.cancel_operation(
93115
operation_name)

samples/snippets/label_video.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
# limitations under the License.
1616

1717
import argparse
18-
18+
import os
19+
from google.api_core.client_options import ClientOptions
1920

2021
# [START datalabeling_label_video_beta]
2122
def label_video(dataset_resource_name, instruction_resource_name,
2223
annotation_spec_set_resource_name):
2324
"""Labels a video dataset."""
2425
from google.cloud import datalabeling_v1beta1 as datalabeling
2526
client = datalabeling.DataLabelingServiceClient()
27+
# [END datalabeling_export_data_beta]
28+
# If provided, use a provided test endpoint - this will prevent tests on
29+
# this snippet from triggering any action by a real human
30+
if 'DATALABELING_ENDPOINT' in os.environ:
31+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
32+
# [START datalabeling_export_data_beta]
2633

2734
basic_config = datalabeling.types.HumanAnnotationConfig(
2835
instruction=instruction_resource_name,

samples/snippets/label_video_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import create_annotation_spec_set
2020
import create_instruction
21+
from google.api_core.client_options import ClientOptions
2122
from google.cloud import datalabeling_v1beta1 as datalabeling
2223
import import_data
2324
import label_video
@@ -52,6 +53,13 @@ def annotation_spec_set():
5253

5354
# tear down
5455
client = datalabeling.DataLabelingServiceClient()
56+
57+
# If provided, use a provided test endpoint - this will prevent tests on
58+
# this snippet from triggering any action by a real human
59+
if 'DATALABELING_ENDPOINT' in os.environ:
60+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
61+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
62+
5563
client.delete_annotation_spec_set(response.name)
5664

5765

@@ -66,6 +74,13 @@ def instruction():
6674

6775
# tear down
6876
client = datalabeling.DataLabelingServiceClient()
77+
78+
# If provided, use a provided test endpoint - this will prevent tests on
79+
# this snippet from triggering any action by a real human
80+
if 'DATALABELING_ENDPOINT' in os.environ:
81+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
82+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
83+
6984
client.delete_instruction(instruction.name)
7085

7186

@@ -89,5 +104,12 @@ def test_label_video(capsys, annotation_spec_set, instruction, dataset):
89104
assert response.cancelled() is True
90105

91106
client = datalabeling.DataLabelingServiceClient()
107+
108+
# If provided, use a provided test endpoint - this will prevent tests on
109+
# this snippet from triggering any action by a real human
110+
if 'DATALABELING_ENDPOINT' in os.environ:
111+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
112+
client = datalabeling.DataLabelingServiceClient(client_options=opts)
113+
92114
client.transport._operations_client.cancel_operation(
93115
operation_name)

samples/snippets/manage_dataset.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
import os
19+
from google.api_core.client_options import ClientOptions
1820

1921

2022
# [START datalabeling_create_dataset_beta]
2123
def create_dataset(project_id):
2224
"""Creates a dataset for the given Google Cloud project."""
2325
from google.cloud import datalabeling_v1beta1 as datalabeling
2426
client = datalabeling.DataLabelingServiceClient()
27+
# [END datalabeling_create_dataset_beta]
28+
# If provided, use a provided test endpoint - this will prevent tests on
29+
# this snippet from triggering any action by a real human
30+
if 'DATALABELING_ENDPOINT' in os.environ:
31+
opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT'))
32+
# [START datalabeling_create_dataset_beta]
2533

2634
formatted_project_name = client.project_path(project_id)
2735

0 commit comments

Comments
 (0)