Skip to content

Commit 25c46dd

Browse files
feat: Migrate API to use python micro-generator (#41)
* migrate API to use micro-generator * migrate API to use micro-generator * update * doc changes * add samples * add samples * add samples and readme * Update README.md * Update README.md * Update UPGRADING.md file * update synth.py Co-authored-by: arithmetic1728 <[email protected]>
1 parent 498813c commit 25c46dd

23 files changed

+144
-152
lines changed

language/snippets/api/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ This directory contains samples for Google Cloud Natural Language API. The `Goog
1414

1515
.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/
1616

17+
18+
19+
20+
1721
Setup
1822
-------------------------------------------------------------------------------
1923

language/snippets/api/analyze_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def test_analyze_sentiment(capsys):
3737
assert sentiment["magnitude"] < 1
3838

3939
result = analyze.analyze_sentiment(
40-
"cheerio, mate - I greatly admire the pallor of your visage, and your "
41-
"angle of repose leaves little room for improvement."
40+
"cheerio, mate - I greatly admire the pallor of your visage, and your angle of repose leaves little room for improvement."
4241
)
4342

4443
sentiment = result["documentSentiment"]

language/snippets/api/noxfile.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@
3737

3838
TEST_CONFIG = {
3939
# You can opt out from the test for specific Python versions.
40-
"ignored_versions": ["2.7"],
40+
'ignored_versions': ["2.7"],
41+
4142
# An envvar key for determining the project id to use. Change it
4243
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4344
# build specific Cloud project. You can also use your own string
4445
# to use your own Cloud project.
45-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
46+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
4647
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
48+
4749
# A dictionary you want to inject into your test. Don't put any
4850
# secrets here. These values will override predefined values.
49-
"envs": {},
51+
'envs': {},
5052
}
5153

5254

5355
try:
5456
# Ensure we can import noxfile_config in the project's directory.
55-
sys.path.append(".")
57+
sys.path.append('.')
5658
from noxfile_config import TEST_CONFIG_OVERRIDE
5759
except ImportError as e:
5860
print("No user noxfile_config found: detail: {}".format(e))
@@ -67,12 +69,12 @@ def get_pytest_env_vars():
6769
ret = {}
6870

6971
# Override the GCLOUD_PROJECT and the alias.
70-
env_key = TEST_CONFIG["gcloud_project_env"]
72+
env_key = TEST_CONFIG['gcloud_project_env']
7173
# This should error out if not set.
72-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
74+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
7375

7476
# Apply user supplied envs.
75-
ret.update(TEST_CONFIG["envs"])
77+
ret.update(TEST_CONFIG['envs'])
7678
return ret
7779

7880

@@ -81,7 +83,7 @@ def get_pytest_env_vars():
8183
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8284

8385
# Any default versions that should be ignored.
84-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
86+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
8587

8688
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8789

@@ -136,7 +138,7 @@ def lint(session):
136138
args = FLAKE8_COMMON_ARGS + [
137139
"--application-import-names",
138140
",".join(local_names),
139-
".",
141+
"."
140142
]
141143
session.run("flake8", *args)
142144

@@ -180,9 +182,9 @@ def py(session):
180182
if session.python in TESTED_VERSIONS:
181183
_session_tests(session)
182184
else:
183-
session.skip(
184-
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
185-
)
185+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
186+
session.python
187+
))
186188

187189

188190
#

language/snippets/classify_text/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ This tutorial demostrates how to use the `classify_text` method to classify cont
1818

1919
.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/
2020

21+
22+
23+
24+
2125
Setup
2226
-------------------------------------------------------------------------------
2327

language/snippets/classify_text/classify_text_tutorial.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import json
2727
import os
2828

29-
from google.cloud import language
29+
from google.cloud import language_v1
3030
import numpy
3131
import six
3232

@@ -37,12 +37,12 @@
3737
def classify(text, verbose=True):
3838
"""Classify the input text into categories. """
3939

40-
language_client = language.LanguageServiceClient()
40+
language_client = language_v1.LanguageServiceClient()
4141

42-
document = language.types.Document(
43-
content=text, type=language.enums.Document.Type.PLAIN_TEXT
42+
document = language_v1.Document(
43+
content=text, type_=language_v1.Document.Type.PLAIN_TEXT
4444
)
45-
response = language_client.classify_text(document)
45+
response = language_client.classify_text(request={'document': document})
4646
categories = response.categories
4747

4848
result = {}

language/snippets/classify_text/noxfile.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@
3737

3838
TEST_CONFIG = {
3939
# You can opt out from the test for specific Python versions.
40-
"ignored_versions": ["2.7"],
40+
'ignored_versions': ["2.7"],
41+
4142
# An envvar key for determining the project id to use. Change it
4243
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4344
# build specific Cloud project. You can also use your own string
4445
# to use your own Cloud project.
45-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
46+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
4647
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
48+
4749
# A dictionary you want to inject into your test. Don't put any
4850
# secrets here. These values will override predefined values.
49-
"envs": {},
51+
'envs': {},
5052
}
5153

5254

5355
try:
5456
# Ensure we can import noxfile_config in the project's directory.
55-
sys.path.append(".")
57+
sys.path.append('.')
5658
from noxfile_config import TEST_CONFIG_OVERRIDE
5759
except ImportError as e:
5860
print("No user noxfile_config found: detail: {}".format(e))
@@ -67,12 +69,12 @@ def get_pytest_env_vars():
6769
ret = {}
6870

6971
# Override the GCLOUD_PROJECT and the alias.
70-
env_key = TEST_CONFIG["gcloud_project_env"]
72+
env_key = TEST_CONFIG['gcloud_project_env']
7173
# This should error out if not set.
72-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
74+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
7375

7476
# Apply user supplied envs.
75-
ret.update(TEST_CONFIG["envs"])
77+
ret.update(TEST_CONFIG['envs'])
7678
return ret
7779

7880

@@ -81,7 +83,7 @@ def get_pytest_env_vars():
8183
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8284

8385
# Any default versions that should be ignored.
84-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
86+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
8587

8688
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8789

@@ -136,7 +138,7 @@ def lint(session):
136138
args = FLAKE8_COMMON_ARGS + [
137139
"--application-import-names",
138140
",".join(local_names),
139-
".",
141+
"."
140142
]
141143
session.run("flake8", *args)
142144

@@ -180,9 +182,9 @@ def py(session):
180182
if session.python in TESTED_VERSIONS:
181183
_session_tests(session)
182184
else:
183-
session.skip(
184-
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
185-
)
185+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
186+
session.python
187+
))
186188

187189

188190
#

language/snippets/cloud-client/v1/noxfile.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@
3737

3838
TEST_CONFIG = {
3939
# You can opt out from the test for specific Python versions.
40-
"ignored_versions": ["2.7"],
40+
'ignored_versions': ["2.7"],
41+
4142
# An envvar key for determining the project id to use. Change it
4243
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4344
# build specific Cloud project. You can also use your own string
4445
# to use your own Cloud project.
45-
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
46+
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
4647
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
48+
4749
# A dictionary you want to inject into your test. Don't put any
4850
# secrets here. These values will override predefined values.
49-
"envs": {},
51+
'envs': {},
5052
}
5153

5254

5355
try:
5456
# Ensure we can import noxfile_config in the project's directory.
55-
sys.path.append(".")
57+
sys.path.append('.')
5658
from noxfile_config import TEST_CONFIG_OVERRIDE
5759
except ImportError as e:
5860
print("No user noxfile_config found: detail: {}".format(e))
@@ -67,12 +69,12 @@ def get_pytest_env_vars():
6769
ret = {}
6870

6971
# Override the GCLOUD_PROJECT and the alias.
70-
env_key = TEST_CONFIG["gcloud_project_env"]
72+
env_key = TEST_CONFIG['gcloud_project_env']
7173
# This should error out if not set.
72-
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
74+
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
7375

7476
# Apply user supplied envs.
75-
ret.update(TEST_CONFIG["envs"])
77+
ret.update(TEST_CONFIG['envs'])
7678
return ret
7779

7880

@@ -81,7 +83,7 @@ def get_pytest_env_vars():
8183
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]
8284

8385
# Any default versions that should be ignored.
84-
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
86+
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
8587

8688
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
8789

@@ -136,7 +138,7 @@ def lint(session):
136138
args = FLAKE8_COMMON_ARGS + [
137139
"--application-import-names",
138140
",".join(local_names),
139-
".",
141+
"."
140142
]
141143
session.run("flake8", *args)
142144

@@ -180,9 +182,9 @@ def py(session):
180182
if session.python in TESTED_VERSIONS:
181183
_session_tests(session)
182184
else:
183-
session.skip(
184-
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
185-
)
185+
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
186+
session.python
187+
))
186188

187189

188190
#

language/snippets/cloud-client/v1/quickstart.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,21 @@ def run_quickstart():
1919
# [START language_quickstart]
2020
# Imports the Google Cloud client library
2121
# [START language_python_migration_imports]
22-
from google.cloud import language
23-
from google.cloud.language import enums
24-
from google.cloud.language import types
22+
from google.cloud import language_v1
2523

2624
# [END language_python_migration_imports]
2725

2826
# Instantiates a client
2927
# [START language_python_migration_client]
30-
client = language.LanguageServiceClient()
28+
client = language_v1.LanguageServiceClient()
3129
# [END language_python_migration_client]
3230

3331
# The text to analyze
3432
text = u"Hello, world!"
35-
document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT)
33+
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
3634

3735
# Detects the sentiment of the text
38-
sentiment = client.analyze_sentiment(document=document).document_sentiment
36+
sentiment = client.analyze_sentiment(request={'document': document}).document_sentiment
3937

4038
print("Text: {}".format(text))
4139
print("Sentiment: {}, {}".format(sentiment.score, sentiment.magnitude))

language/snippets/cloud-client/v1/set_endpoint.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ def set_endpoint():
1717
"""Change your endpoint"""
1818
# [START language_set_endpoint]
1919
# Imports the Google Cloud client library
20-
from google.cloud import language
20+
from google.cloud import language_v1
2121

2222
client_options = {"api_endpoint": "eu-language.googleapis.com:443"}
2323

2424
# Instantiates a client
25-
client = language.LanguageServiceClient(client_options=client_options)
25+
client = language_v1.LanguageServiceClient(client_options=client_options)
2626
# [END language_set_endpoint]
2727

2828
# The text to analyze
29-
document = language.types.Document(
30-
content="Hello, world!", type=language.enums.Document.Type.PLAIN_TEXT
29+
document = language_v1.Document(
30+
content="Hello, world!", type_=language_v1.Document.Type.PLAIN_TEXT
3131
)
3232

3333
# Detects the sentiment of the text
34-
sentiment = client.analyze_sentiment(document=document).document_sentiment
34+
sentiment = client.analyze_sentiment(request={'document': document}).document_sentiment
3535

3636
print("Sentiment: {}, {}".format(sentiment.score, sentiment.magnitude))
3737

language/snippets/generated-samples/v1/language_sentiment_text.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
# [START language_sentiment_text]
2525

2626
from google.cloud import language_v1
27-
from google.cloud.language_v1 import enums
2827
import six
2928

3029

@@ -37,10 +36,10 @@ def sample_analyze_sentiment(content):
3736
if isinstance(content, six.binary_type):
3837
content = content.decode("utf-8")
3938

40-
type_ = enums.Document.Type.PLAIN_TEXT
41-
document = {"type": type_, "content": content}
39+
type_ = language_v1.Document.Type.PLAIN_TEXT
40+
document = {"type_": type_, "content": content}
4241

43-
response = client.analyze_sentiment(document)
42+
response = client.analyze_sentiment(request={'document': document})
4443
sentiment = response.document_sentiment
4544
print("Score: {}".format(sentiment.score))
4645
print("Magnitude: {}".format(sentiment.magnitude))

0 commit comments

Comments
 (0)