Skip to content

🔍 Examples of Generated Samples for Code Review [DO NOT MERGE] - #9514

Closed
beccasaurus wants to merge 1 commit into
googleapis:masterfrom
beccasaurus:generated-sample-syntax-review
Closed

beccasaurus wants to merge 1 commit into
googleapis:masterfrom
beccasaurus:generated-sample-syntax-review

Conversation

@beccasaurus

@beccasaurus beccasaurus commented Oct 22, 2019 •

Copy link
Copy Markdown
Contributor

Pull Request to facilitate code review of GAPIC SampleGen rendered syntax. Do not merge.

This Pull Request contains two code samples which were generated using the GAPIC generator:

  • Natural Language API – "Analyze Sentiment" (language_sentiment_text) (config YAML)
  • Speech-to-Text API – "Long-Running Transcribe" (speech_transcribe_async) (config YAML)

These code samples demonstrate a few features of the code sample generator:

  • Overall code sample structure (imports, layout, etc)
  • Calling a synchronous API endpoint and printing fields from the API response
  • Calling a long-running API, waiting for completion, and printing fields from API response
  • Using local files with code samples, specifically a file to use for a bytes request field

You can find the existing, published hand-written versions of these code samples here:

For Python, existing hand-written code samples can be found in the python-docs-samples repo.

The goal is for these generated code samples to meet the expectations of hand-written samples.


Language experts, please TAL! 🧐

/cc @busunkim96 @crwilcox @tswast


This is one of seven Pull Requests being sent for code review of rendered generated sample syntax.

Language Pull Request
#️⃣ C# googleapis/google-cloud-dotnet#3650
🐹 Go googleapis/google-cloud-go#1634
☕️ Java googleapis/google-cloud-java#6543
🚀Node.js googleapis/google-cloud-node#2953
🐘 PHP googleapis/google-cloud-php#2420
🐍 Python #9514
💎 Ruby googleapis/google-cloud-ruby#4264

📓 Internal doc for context


parser = argparse.ArgumentParser()
parser.add_argument(
"--local_file_path", type=str, default="resources/brooklyn_bridge.raw"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this resource file be added alongside the sample?

@busunkim96 busunkim96 added api: language Issues related to the Cloud Natural Language API API. api: speech Issues related to the Speech-to-Text API. do not merge Indicates a pull request not ready for merge, due to either quality or timing. labels Oct 22, 2019
@beccasaurus

Copy link
Copy Markdown
Contributor Author

/cc @googleapis/samplegen

Transcribe a long audio file using asynchronous speech recognition

Args:
local_file_path Path to local audio file, e.g. /path/audio.wav

@busunkim96 busunkim96 Oct 22, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit

Suggested change
local_file_path Path to local audio file, e.g. /path/audio.wav
local_file_path (str): Path to local audio file, e.g. /path/audio.wav

This is the docstring style used in the client libraries.
If it's tricky to get the type it would still be nice to have the : between the arg name and description. 😄

@beccasaurus

Copy link
Copy Markdown
Contributor Author

/cc @dizcology @nnegrey @engelke @gguuss

@gguuss gguuss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits on this style, I have become used to single-quote strings in Python and in general prefer to avoid comprehensively documenting arguments over giving variables meaningful names.

"""
Analyzing Sentiment in a String

Args:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC we generally have not been putting ARGS in python sample code.

To quote the Authoring GUIDE:

Generally, however, it's rarely necessary to exhaustively document the parameters this way. Lean towards unsurprising arguments with descriptive names, as having to resort to this kind of docstring might be extremely accurate but it comes at the cost of high redundancy, signal-to-noise ratio, and increased cognitive load.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd go one step further and say we can have the function definition for ease-of-testing, but when I see the snippet embedded in the docs, it's more a distraction than anything. I'd love to see the imports and region tags all inside the function definition.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--text_content", type=str, default="I am so happy and joyful.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the style guide:

Beyond PEP8, there are several idioms and style nits we prefer.

Use single quotes (') except for docstrings (which use """).

This is a hill I will die on, I have become very used to authoring Python strings preferring single-quotes over double-quotes and it is a convention both highlighted in our authoring guide as well as in older versions of the Google Python style guide (although they recently have loosened up this to just be consistent).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use black to format the code in all our library repos, which uses double-quotes unless the string contains a double-quote character.

At some point, I think we should start using black everywhere because I hate manually trying to format code to pass flake8.


# [START speech_transcribe_async]
from google.cloud import speech_v1
from google.cloud.speech_v1 import enums

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Authoring Guide:

Beyond PEP8, there are several idioms and style nits we prefer.

from gcloud import datastore 

instead of 

from gcloud.datastore import Client

But use your best judgement here, I am not too picky on this one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think enums is a module, so this does match the Authoring Guide.

# usage: python3 samples/v1/language_sentiment_text.py [--text_content "I am so happy and joyful."]

# [START language_sentiment_text]
from google.cloud import language_v1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from google.cloud import language_v1 --> from google.cloud import language_v1 as language
Makes it a lot easier to perform updates

@beccasaurus

Copy link
Copy Markdown
Contributor Author

301 Redirect => #9970 (which shows additional types of samples!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: language Issues related to the Cloud Natural Language API API. api: speech Issues related to the Speech-to-Text API. cla: yes This human has signed the Contributor License Agreement. do not merge Indicates a pull request not ready for merge, due to either quality or timing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants