From 615c08e7b4078e17cfb7ecd9fda6d01c351e767d Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 25 Oct 2019 16:36:51 -0600 Subject: [PATCH] Update v3 samples to use default library --- translate/cloud-client/beta_snippets_test.py | 4 +--- translate/cloud-client/quickstart.py | 2 +- translate/cloud-client/requirements.txt | 2 +- translate/cloud-client/snippets.py | 4 ++-- translate/cloud-client/translate_v3_batch_translate_text.py | 4 ++-- .../translate_v3_batch_translate_text_with_glossary.py | 6 +++--- ...slate_v3_batch_translate_text_with_glossary_and_model.py | 6 +++--- .../translate_v3_batch_translate_text_with_model.py | 4 ++-- translate/cloud-client/translate_v3_create_glossary.py | 2 +- translate/cloud-client/translate_v3_delete_glossary.py | 2 +- translate/cloud-client/translate_v3_detect_language.py | 4 ++-- translate/cloud-client/translate_v3_get_glossary.py | 4 ++-- .../cloud-client/translate_v3_get_supported_languages.py | 4 ++-- .../translate_v3_get_supported_languages_for_target.py | 4 ++-- translate/cloud-client/translate_v3_list_glossary.py | 4 ++-- translate/cloud-client/translate_v3_translate_text.py | 4 ++-- .../translate_v3_translate_text_with_glossary.py | 2 +- .../translate_v3_translate_text_with_glossary_and_model.py | 6 +++--- .../cloud-client/translate_v3_translate_text_with_model.py | 4 ++-- 19 files changed, 35 insertions(+), 37 deletions(-) diff --git a/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py index f7099a27cae1..0cad93642b55 100644 --- a/translate/cloud-client/beta_snippets_test.py +++ b/translate/cloud-client/beta_snippets_test.py @@ -63,7 +63,7 @@ def unique_glossary_id(): def test_translate_text(capsys): beta_snippets.translate_text(PROJECT_ID, 'Hello world') out, _ = capsys.readouterr() - assert 'Zdravo svet' in out + assert 'Zdravo svet' in out or 'Pozdrav svijetu' in out def test_batch_translate_text(capsys, bucket): @@ -99,7 +99,6 @@ def test_create_glossary(capsys, unique_glossary_id): beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) out, _ = capsys.readouterr() assert 'Created' in out - assert PROJECT_ID in out assert unique_glossary_id in out assert 'gs://cloud-samples-data/translation/glossary.csv' in out @@ -129,6 +128,5 @@ def test_delete_glossary(capsys, unique_glossary_id): beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) beta_snippets.delete_glossary(PROJECT_ID, unique_glossary_id) out, _ = capsys.readouterr() - assert PROJECT_ID in out assert 'us-central1' in out assert unique_glossary_id in out diff --git a/translate/cloud-client/quickstart.py b/translate/cloud-client/quickstart.py index c84ea892edae..888b6691c404 100644 --- a/translate/cloud-client/quickstart.py +++ b/translate/cloud-client/quickstart.py @@ -18,7 +18,7 @@ def run_quickstart(): # [START translate_quickstart] # Imports the Google Cloud client library - from google.cloud import translate + from google.cloud import translate_v2 as translate # Instantiates a client translate_client = translate.Client() diff --git a/translate/cloud-client/requirements.txt b/translate/cloud-client/requirements.txt index f2eeffaa033c..f8a1e7fe5e67 100644 --- a/translate/cloud-client/requirements.txt +++ b/translate/cloud-client/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-translate==1.7.0 +google-cloud-translate==2.0.0 google-cloud-storage==1.19.1 diff --git a/translate/cloud-client/snippets.py b/translate/cloud-client/snippets.py index b5719d232125..13f864d5fcad 100644 --- a/translate/cloud-client/snippets.py +++ b/translate/cloud-client/snippets.py @@ -23,7 +23,7 @@ import argparse -from google.cloud import translate +from google.cloud import translate_v2 as translate import six @@ -70,7 +70,7 @@ def list_languages_with_target(target): # [END translate_list_language_names] -def translate_text_with_model(target, text, model=translate.NMT): +def translate_text_with_model(target, text, model='nmt'): # [START translate_text_with_model] """Translates text into the target language. diff --git a/translate/cloud-client/translate_v3_batch_translate_text.py b/translate/cloud-client/translate_v3_batch_translate_text.py index 897f5809e508..acb43fc1509e 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text.py +++ b/translate/cloud-client/translate_v3_batch_translate_text.py @@ -25,14 +25,14 @@ # usage: python3 translate_v3_batch_translate_text.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project_id "[Google Cloud Project ID]"] [--location "us-central1"] [--source_lang en] [--target_lang ja] # [START translate_v3_batch_translate_text] -from google.cloud import translate_v3 +from google.cloud import translate def sample_batch_translate_text( input_uri, output_uri, project_id, location, source_lang, target_lang ): """Batch translate text""" - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # input_uri = 'gs://cloud-samples-data/text.txt' diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary.py index 33030e85c5ae..689005784b66 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary.py @@ -25,14 +25,14 @@ # usage: python3 translate_v3_batch_translate_text_with_glossary.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--glossary_id "[YOUR_GLOSSARY_ID]"] [--target_language en] [--source_language de] # [START translate_v3_batch_translate_text_with_glossary] -from google.cloud import translate_v3 +from google.cloud import translate def sample_batch_translate_text_with_glossary( input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id ): """Batch translate text with Glossary""" - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # input_uri = 'gs://cloud-samples-data/text.txt' @@ -59,7 +59,7 @@ def sample_batch_translate_text_with_glossary( 'us-central1', # The location of the glossary glossary_id) - glossary_config = translate_v3.types.TranslateTextGlossaryConfig( + glossary_config = translate.types.TranslateTextGlossaryConfig( glossary=glossary_path) glossaries = {"ja": glossary_config} #target lang as key diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_and_model.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_and_model.py index ed4b541da0cf..39728aa93359 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_and_model.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_and_model.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_batch_translate_text_with_glossary_and_model.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--target_language en] [--source_language de] [--model_id "{your-model-id}"] [--glossary_id "{your-glossary-id}"] # [START translate_v3_batch_translate_text_with_glossary_and_model] -from google.cloud import translate_v3 +from google.cloud import translate def sample_batch_translate_text_with_glossary_and_model( input_uri, @@ -41,7 +41,7 @@ def sample_batch_translate_text_with_glossary_and_model( Batch translate text with Glossary and Translation model """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # input_uri = 'gs://cloud-samples-data/text.txt' @@ -70,7 +70,7 @@ def sample_batch_translate_text_with_glossary_and_model( 'us-central1', # The location of the glossary glossary_id) - glossary_config = translate_v3.types.TranslateTextGlossaryConfig( + glossary_config = translate.types.TranslateTextGlossaryConfig( glossary=glossary_path) glossaries = {"ja": glossary_config} #target lang as key diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_model.py b/translate/cloud-client/translate_v3_batch_translate_text_with_model.py index ef9680b4cfe5..3b09cbac93a5 100644 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_batch_translate_text_with_model.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_batch_translate_text_with_model.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--target_language en] [--source_language de] [--model_id "{your-model-id}"] # [START translate_v3_batch_translate_text_with_model] -from google.cloud import translate_v3 +from google.cloud import translate def sample_batch_translate_text_with_model( @@ -47,7 +47,7 @@ def sample_batch_translate_text_with_model( code. """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # input_uri = 'gs://cloud-samples-data/text.txt' diff --git a/translate/cloud-client/translate_v3_create_glossary.py b/translate/cloud-client/translate_v3_create_glossary.py index a2e51c32e62b..32c1c6afcf7c 100644 --- a/translate/cloud-client/translate_v3_create_glossary.py +++ b/translate/cloud-client/translate_v3_create_glossary.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_create_glossary.py [--project "[Google Cloud Project ID]"] [--glossary_id "my_glossary_id_123"] # [START translate_v3_create_glossary] -from google.cloud import translate_v3 as translate +from google.cloud import translate def sample_create_glossary(project_id, input_uri, glossary_id): """Create Glossary""" diff --git a/translate/cloud-client/translate_v3_delete_glossary.py b/translate/cloud-client/translate_v3_delete_glossary.py index 25015b92451a..36d7e48cbbc0 100644 --- a/translate/cloud-client/translate_v3_delete_glossary.py +++ b/translate/cloud-client/translate_v3_delete_glossary.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_delete_glossary.py [--project_id "[Google Cloud Project ID]"] [--glossary_id "[Glossary ID]"] # [START translate_v3_delete_glossary] -from google.cloud import translate_v3 as translate +from google.cloud import translate def sample_delete_glossary(project_id, glossary_id): """Delete Glossary""" diff --git a/translate/cloud-client/translate_v3_detect_language.py b/translate/cloud-client/translate_v3_detect_language.py index 20c2147de0a4..ca5a72c1caad 100644 --- a/translate/cloud-client/translate_v3_detect_language.py +++ b/translate/cloud-client/translate_v3_detect_language.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_detect_language.py [--text "Hello, world!"] [--project_id "[Google Cloud project_id ID]"] # [START translate_v3_detect_language] -from google.cloud import translate_v3 +from google.cloud import translate def sample_detect_language(text, project_id): @@ -36,7 +36,7 @@ def sample_detect_language(text, project_id): text The text string for performing language detection """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # text = 'Hello, world!' diff --git a/translate/cloud-client/translate_v3_get_glossary.py b/translate/cloud-client/translate_v3_get_glossary.py index f2d9039955ff..40706a03b8c6 100644 --- a/translate/cloud-client/translate_v3_get_glossary.py +++ b/translate/cloud-client/translate_v3_get_glossary.py @@ -25,12 +25,12 @@ # usage: python3 translate_v3_get_glossary.py [--project_id "[Google Cloud Project ID]"] [--glossary_id "[Glossary ID]"] # [START translate_v3_get_glossary] -from google.cloud import translate_v3 +from google.cloud import translate def sample_get_glossary(project_id, glossary_id): """Get Glossary""" - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # project = '[Google Cloud Project ID]' diff --git a/translate/cloud-client/translate_v3_get_supported_languages.py b/translate/cloud-client/translate_v3_get_supported_languages.py index 3e6ab119119f..9a55b598c434 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages.py +++ b/translate/cloud-client/translate_v3_get_supported_languages.py @@ -25,12 +25,12 @@ # usage: python3 translate_v3_get_supported_languages.py [--project_id "[Google Cloud Project ID]"] # [START translate_v3_get_supported_languages] -from google.cloud import translate_v3 +from google.cloud import translate def sample_get_supported_languages(project_id): """Getting a list of supported language codes""" - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # project = '[Google Cloud Project ID]' diff --git a/translate/cloud-client/translate_v3_get_supported_languages_for_target.py b/translate/cloud-client/translate_v3_get_supported_languages_for_target.py index 30e6a4d4c836..b037baaee52d 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_for_target.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_for_target.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_get_supported_languages_for_target.py [--language_code en] [--project_id "[Google Cloud Project ID]"] # [START translate_v3_get_supported_languages_for_target] -from google.cloud import translate_v3 +from google.cloud import translate def sample_get_supported_languages_with_target(language_code, project_id): @@ -37,7 +37,7 @@ def sample_get_supported_languages_with_target(language_code, project_id): human readable names of supported languages. """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # language_code = 'en' diff --git a/translate/cloud-client/translate_v3_list_glossary.py b/translate/cloud-client/translate_v3_list_glossary.py index 356f12ce2cd2..da448212a7b7 100644 --- a/translate/cloud-client/translate_v3_list_glossary.py +++ b/translate/cloud-client/translate_v3_list_glossary.py @@ -25,12 +25,12 @@ # usage: python3 translate_v3_list_glossary.py [--project_id "[Google Cloud Project ID]"] # [START translate_v3_list_glossary] -from google.cloud import translate_v3 +from google.cloud import translate def sample_list_glossaries(project_id): """List Glossaries""" - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # project_id = '[Google Cloud Project ID]' diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py index 187c773c25bb..ded7947b7e7e 100644 --- a/translate/cloud-client/translate_v3_translate_text.py +++ b/translate/cloud-client/translate_v3_translate_text.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_translate_text.py [--text "Hello, world!"] [--target_language fr] [--project_id "[Google Cloud Project ID]"] # [START translate_v3_translate_text] -from google.cloud import translate_v3 +from google.cloud import translate def sample_translate_text(text, target_language, project_id): @@ -37,7 +37,7 @@ def sample_translate_text(text, target_language, project_id): target_language Required. The BCP-47 language code to use for translation. """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # text = 'Text you wish to translate' diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary.py b/translate/cloud-client/translate_v3_translate_text_with_glossary.py index 209798533057..1a4a0715ed25 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_translate_text_with_glossary.py [--text "Hello, world!"] [--source_language en] [--target_language fr] [--project_id "[Google Cloud Project ID]"] [--glossary_id "[YOUR_GLOSSARY_ID]"] # [START translate_v3_translate_text_with_glossary] -from google.cloud import translate_v3 as translate +from google.cloud import translate def sample_translate_text_with_glossary( text, source_language, target_language, project_id, glossary_id diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_and_model.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_and_model.py index 0ca2d203ef2b..5287903d905e 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_and_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary_and_model.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_translate_text_with_glossary_and_model.py [--model_id "[MODEL ID]"] [--glossary_id "[YOUR_GLOSSARY_ID]"] [--text "Hello, world!"] [--target_language fr] [--source_language en] [--project_id "[Google Cloud Project ID]"] [--location global] # [START translate_v3_translate_text_with_glossary_and_model] -from google.cloud import translate_v3 +from google.cloud import translate def sample_translate_text_glossary_and_model( @@ -42,7 +42,7 @@ def sample_translate_text_glossary_and_model( source_language Optional. The BCP-47 language code of the input text. """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # model_id = '[MODEL ID]' @@ -61,7 +61,7 @@ def sample_translate_text_glossary_and_model( 'us-central1', # The location of the glossary glossary_id) - glossary_config = translate_v3.types.TranslateTextGlossaryConfig( + glossary_config = translate.types.TranslateTextGlossaryConfig( glossary=glossary) response = client.translate_text( diff --git a/translate/cloud-client/translate_v3_translate_text_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py index 242caa704a63..0dee0e4539af 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model.py @@ -25,7 +25,7 @@ # usage: python3 translate_v3_translate_text_with_model.py [--model_id "[MODEL ID]"] [--text "Hello, world!"] [--target_language fr] [--source_language en] [--project_id "[Google Cloud Project ID]"] [--location global] # [START translate_v3_translate_text_with_model] -from google.cloud import translate_v3 +from google.cloud import translate def sample_translate_text_with_model( @@ -41,7 +41,7 @@ def sample_translate_text_with_model( source_language Optional. The BCP-47 language code of the input text. """ - client = translate_v3.TranslationServiceClient() + client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # model_id = '[MODEL ID]'