Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds snippets and tests for documentation tutorial. #729

Merged
merged 7 commits into from
Dec 20, 2016

Conversation

gguuss
Copy link
Contributor

@gguuss gguuss commented Dec 20, 2016

Adds code to back up documentation. Note that on my machine, there are issues with generating the documentation so that still should be added before merging.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Dec 20, 2016


def authenticate():
'''Authenticates the client library using default application
Copy link
Contributor

Choose a reason for hiding this comment

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

Use double quotes for docstrings, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return service


def getResponse(filename):
Copy link
Contributor

Choose a reason for hiding this comment

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

snake_case for everything but class names, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

# [END import_libraries]


def authenticate():
Copy link
Contributor

Choose a reason for hiding this comment

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

We almost universally avoid having a separate function construct the client. Indirection can be confusing for users. This is two lines of code, would you mind just in-lining this in the relevant functions?

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought since this is part of a cohesive tutorial and not snippets this can stay. But can you rename it to something like make_client?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I ended up removing the separate function, it may actually make sense to flatten the whole sample into a single helper that accepts a filename.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would be for that.

# [START parsing_the_response]
score = response['documentSentiment']['score']
magnitude = response['documentSentiment']['magnitude']
for i, sentence in enumerate(response['sentences']):
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: use n when using enumerate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

def test_neutral():
result = tutorial.getResponse('reviews/bladerunner-neutral.txt')
assert result['language'] == 'en'
assert (result['documentSentiment']['score'] > -1 and
Copy link
Contributor

Choose a reason for hiding this comment

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

Python trick for you:

assert -1 < result['documentSentiment']['score'] < 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! Done.

import argparse

from googleapiclient import discovery

Copy link
Contributor

Choose a reason for hiding this comment

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

No empty line between these two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

service = discovery.build('language', 'v1', credentials=credentials)
# [END authenticating_to_the_api]
# [START constructing_the_request]
with open(filename, 'r') as review_file:
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Put an empty newline above control statements
  2. You didn't limit the scope of the with statement as I mentioned in the last round. :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

# [START parsing_the_response]
score = response['documentSentiment']['score']
magnitude = response['documentSentiment']['magnitude']
for n, sentence in enumerate(response['sentences']):
Copy link
Contributor

Choose a reason for hiding this comment

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

blank newline above control statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

sentence_sentiment = sentence['sentiment']['score']
print('Sentence {} has a sentiment score of {}'.format(n,
sentence_sentiment))
print('Overall Sentiment: score of {} with magnitude of {}'.format(
Copy link
Contributor

Choose a reason for hiding this comment

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

Blank newline above this statement, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.



# [START running_your_application]
def main(filename):
Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove this function now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@theacodes theacodes merged commit b7d2b50 into master Dec 20, 2016
@theacodes theacodes deleted the language-docs-tutorial branch December 20, 2016 22:27
parthea pushed a commit to googleapis/google-cloud-python that referenced this pull request Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants