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

Update samples to support latest Google Cloud Python #656

Merged
merged 13 commits into from
Nov 15, 2016
2 changes: 1 addition & 1 deletion appengine/flexible/analytics/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
gunicorn==19.6.0
requests[security]==2.11.1
requests[security]==2.12.0
1 change: 0 additions & 1 deletion appengine/flexible/datastore/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import datetime
import logging
import os
import socket

from flask import Flask, request
Expand Down
2 changes: 1 addition & 1 deletion appengine/flexible/datastore/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==0.11.1
google-cloud-datastore==0.20.1
google-cloud-datastore==0.21.0
gunicorn==19.6.0
oauth2client==4.0.0
2 changes: 1 addition & 1 deletion appengine/flexible/endpoints/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ flask-cors==3.0.2
gunicorn==19.6.0
six==1.10.0
pyyaml==3.12
requests==2.11.1
requests==2.12.0
2 changes: 1 addition & 1 deletion appengine/flexible/kinto/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
gunicorn==19.6.0
oauth2client==4.0.0
kinto==4.3.2
kinto==4.3.3
2 changes: 1 addition & 1 deletion appengine/flexible/mailgun/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
gunicorn==19.6.0
requests[security]==2.11.1
requests[security]==2.12.0
2 changes: 1 addition & 1 deletion appengine/flexible/mailjet/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==0.11.1
gunicorn==19.6.0
requests[security]==2.11.1
requests[security]==2.12.0
mailjet-rest==v1.2.2
2 changes: 1 addition & 1 deletion appengine/flexible/metadata/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
gunicorn==19.6.0
requests[security]==2.11.1
requests[security]==2.12.0
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
gunicorn==19.6.0
requests==2.11.1
requests==2.12.0
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
gunicorn==19.6.0
requests==2.11.1
requests==2.12.0
2 changes: 1 addition & 1 deletion appengine/flexible/pubsub/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==0.11.1
google-cloud-pubsub==0.20.0
google-cloud-pubsub==0.21.0
gunicorn==19.6.0
oauth2client==4.0.0
2 changes: 1 addition & 1 deletion appengine/flexible/sendgrid/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
sendgrid==3.6.2
sendgrid==3.6.3
gunicorn==19.6.0
2 changes: 1 addition & 1 deletion appengine/flexible/storage/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
google-cloud-storage==0.20.0
google-cloud-storage==0.21.0
gunicorn==19.6.0
2 changes: 1 addition & 1 deletion appengine/standard/analytics/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
requests==2.11.1
requests==2.12.0
requests-toolbelt==0.7.0
2 changes: 1 addition & 1 deletion appengine/standard/firebase/firetactoe/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
flask==0.11.1
requests==2.11.1
requests==2.12.0
requests_toolbelt==0.7.0
oauth2client==4.0.0
functools32==3.2.3-2; python_version < "3"
2 changes: 1 addition & 1 deletion appengine/standard/mailjet/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==0.11.1
requests==2.11.1
requests==2.12.0
requests-toolbelt==0.7.0
mailjet-rest==v1.2.2
2 changes: 1 addition & 1 deletion appengine/standard/sendgrid/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sendgrid==3.6.2
sendgrid==3.6.3
2 changes: 1 addition & 1 deletion appengine/standard/urlfetch/requests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask==0.11.1
requests==2.11.1
requests==2.12.0
requests-toolbelt==0.7.0
2 changes: 1 addition & 1 deletion bigquery/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-bigquery==0.20.0
google-cloud-bigquery==0.21.0
55 changes: 7 additions & 48 deletions bigquery/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@
def list_projects():
bigquery_client = bigquery.Client()

projects = []
page_token = None

while True:
results, page_token = bigquery_client.list_projects(
page_token=page_token)
projects.extend(results)

if not page_token:
break

for project in projects:
for project in bigquery_client.list_projects():
print(project.project_id)


Expand All @@ -57,18 +46,7 @@ def list_datasets(project=None):
"""
bigquery_client = bigquery.Client(project=project)

datasets = []
page_token = None

while True:
results, page_token = bigquery_client.list_datasets(
page_token=page_token)
datasets.extend(results)

if not page_token:
break

for dataset in datasets:
for dataset in bigquery_client.list_datasets():
print(dataset.name)


Expand Down Expand Up @@ -98,17 +76,7 @@ def list_tables(dataset_name, project=None):
print('Dataset {} does not exist.'.format(dataset_name))
return

tables = []
page_token = None

while True:
results, page_token = dataset.list_tables(page_token=page_token)
tables.extend(results)

if not page_token:
break

for table in tables:
for table in dataset.list_tables():
print(table.name)


Expand Down Expand Up @@ -157,19 +125,10 @@ def list_rows(dataset_name, table_name, project=None):
# Reload the table so that the schema is available.
table.reload()

rows = []
page_token = None

# Load at most 25 results. You can change this to `while True` and change
# the max_results argument to load more rows from BigQuery, but note
# that this can take some time. It's preferred to use a query.
while len(rows) < 25:
results, total_rows, page_token = table.fetch_data(
max_results=25, page_token=page_token)
rows.extend(results)

if not page_token:
break
# Load at most 25 results. You can change the max_results argument to load
# more rows from BigQuery, but note that this can take some time. It's
# preferred to use a query.
rows = table.fetch_data(max_results=25)
Copy link
Contributor

Choose a reason for hiding this comment

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

You may prefer to just consume the iterator right here:

rows = list(table.fetch_data(max_results=25))

There is too much suspense between this and the for row in rows (when the request(s) actually get made)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, done.


# Use format to create a simple table.
format_string = '{!s:<16} ' * len(table.schema)
Expand Down
4 changes: 2 additions & 2 deletions bigquery/dml/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
flake8==3.0.4
google-cloud-bigquery==0.20.0
flake8==3.2.0
google-cloud-bigquery==0.21.0
PyMySQL==0.7.9
six==1.10.0
SQLAlchemy==1.1.3
2 changes: 1 addition & 1 deletion bigtable/hello/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-bigtable==0.20.0
google-cloud-bigtable==0.21.0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-datastore==0.20.1
google-cloud-datastore==0.21.0
2 changes: 1 addition & 1 deletion compute/auth/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
requests==2.11.1
requests==2.12.0
google-api-python-client==1.5.5
2 changes: 1 addition & 1 deletion compute/encryption/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cryptography==1.5.3
requests==2.11.1
requests==2.12.0
2 changes: 1 addition & 1 deletion compute/metadata/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.11.1
requests==2.12.0
2 changes: 1 addition & 1 deletion compute/xmpp_wikibot/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==0.11.1
requests==2.11.1
requests==2.12.0
sleekxmpp==1.3.1
six==1.10.0
2 changes: 1 addition & 1 deletion dataproc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-api-python-client==1.5.5
google-cloud==0.20.0
google-cloud==0.21.0
2 changes: 1 addition & 1 deletion datastore/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-datastore==0.20.1
google-cloud-datastore==0.21.0
7 changes: 5 additions & 2 deletions datastore/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,12 @@ def cursor_paging(client):
def get_one_page_of_tasks(cursor=None):
query = client.query(kind='Task')
query_iter = query.fetch(start_cursor=cursor, limit=5)
tasks, _, cursor = query_iter.next_page()
page = next(query_iter.pages)

return tasks, cursor
tasks = list(page)
next_cusor = query_iter.next_page_token
Copy link
Contributor

Choose a reason for hiding this comment

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

next_cusor? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


return tasks, next_cusor
# [END cursor_paging]

page_one, cursor_one = get_one_page_of_tasks()
Expand Down
10 changes: 3 additions & 7 deletions dns/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_zone(project_id, name):
# [START list_zones]
def list_zones(project_id):
client = dns.Client(project=project_id)
zones, _ = client.list_zones()
zones = client.list_zones()
return [zone.name for zone in zones]
# [END list_zones]

Expand All @@ -63,11 +63,7 @@ def list_resource_records(project_id, zone_name):
client = dns.Client(project=project_id)
zone = client.zone(zone_name)

records, page_token = zone.list_resource_record_sets()
while page_token is not None:
next_batch, page_token = zone.list_resource_record_sets(
page_token=page_token)
records.extend(next_batch)
records = zone.list_resource_record_sets()

return [(record.name, record.record_type, record.ttl, record.rrdatas)
for record in records]
Expand All @@ -79,7 +75,7 @@ def list_changes(project_id, zone_name):
client = dns.Client(project=project_id)
zone = client.zone(zone_name)

changes, _ = zone.list_changes()
changes = zone.list_changes()

return [(change.started, change.status) for change in changes]
# [END changes]
Expand Down
2 changes: 1 addition & 1 deletion dns/api/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def client(cloud_config):
yield client

# Delete anything created during the test.
for zone in client.list_zones()[0]:
for zone in client.list_zones():
zone.delete()


Expand Down
2 changes: 1 addition & 1 deletion dns/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-dns==0.20.0
google-cloud-dns==0.21.0
2 changes: 1 addition & 1 deletion endpoints/getting-started/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ flask-cors==3.0.2
gunicorn==19.6.0
six==1.10.0
pyyaml==3.12
requests==2.11.1
requests==2.12.0
2 changes: 1 addition & 1 deletion language/movie_nl/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-api-python-client==1.5.5
requests==2.11.1
requests==2.12.0
8 changes: 1 addition & 7 deletions logging/cloud-client/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ def list_sinks():
"""Lists all sinks."""
logging_client = logging.Client()

sinks = []
token = None
while True:
new_sinks, token = logging_client.list_sinks(page_token=token)
sinks.extend(new_sinks)
if token is None:
break
sinks = list(logging_client.list_sinks())

if not sinks:
print('No sinks.')
Expand Down
2 changes: 1 addition & 1 deletion logging/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-logging==0.20.0
google-cloud-logging==0.21.0
11 changes: 1 addition & 10 deletions logging/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,7 @@ def list_entries(logger_name):

print('Listing entries for logger {}:'.format(logger.name))

entries = []
page_token = None

while True:
new_entries, page_token = logger.list_entries(page_token=page_token)
entries.extend(new_entries)
if not page_token:
break

for entry in entries:
for entry in logger.list_entries():
timestamp = entry.timestamp.isoformat()
print('* {}: {}'.format
(timestamp, entry.payload))
Expand Down
16 changes: 14 additions & 2 deletions nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

# Libraries that only work on Python 2.7
PY27_ONLY_LIBRARIES = ['mysql-python']
IGNORED_LIBRARIES = ['pylibmc']

# Whether we're running on Travis CI
ON_TRAVIS = os.environ.get('TRAVIS', False)
Expand Down Expand Up @@ -245,7 +246,7 @@ def session_lint(session):
sample_directories = (
sample_dir for subdir in subdirs if not subdir.startswith('.')
for sample_dir in collect_sample_dirs(
subdir, suffix='.py', blacklist='conftest.py'))
subdir, suffix='.py', blacklist='conftest.py'))

# On travis, only lint changed samples.
if ON_TRAVIS:
Expand Down Expand Up @@ -286,7 +287,7 @@ def session_reqcheck(session):
command = 'check-requirements'

reqfiles = list(list_files('.', 'requirements*.txt'))
reqfiles.append('requirements-dev.in')
reqfiles.append('testing/requirements-dev.in')

for reqfile in reqfiles:
session.run('gcprepotools', command, reqfile)
Expand All @@ -297,6 +298,7 @@ def session_reqrollup(session):

This does not test for uniqueness. pip itself will validate that.
"""
session.virtualenv = False
requirements = set()
requirements_files = list(list_files('.', 'requirements*.txt'))
requirements_files.append('./testing/requirements-dev.in')
Expand All @@ -317,9 +319,19 @@ def mark_if_necessary(requirement):
requirement.strip())
return requirement

def is_ignored(requirement):
"""Ignores certain libraries."""
for library in IGNORED_LIBRARIES:
if requirement.startswith(library):
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

Could do this more succinctly:

return any(requirement.startswith(l) for l in IGNORED_LIBRARIES)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like it, but it's almost too clever. I'm going to leave as-is for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't care either way but don't think it's too clever, a bit more English-esque if anything.


requirements = [
mark_if_necessary(requirement) for requirement in requirements]

requirements = [
requirement for requirement in requirements if not
is_ignored(requirement)]

Copy link
Contributor

Choose a reason for hiding this comment

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

Could put this conditional in the previous comprehension, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather not.

with open('testing/requirements-dev.txt', 'w') as f:
f.write('# This file is generated by nox -s reqrollup. Do not edit.\n')
for requirement in sorted(requirements, key=lambda s: s.lower()):
Expand Down
10 changes: 1 addition & 9 deletions pubsub/cloud-client/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ def list_topics():
"""Lists all Pub/Sub topics in the current project."""
pubsub_client = pubsub.Client()

topics = []
next_page_token = None
while True:
page, next_page_token = pubsub_client.list_topics()
topics.extend(page)
if not next_page_token:
break

for topic in topics:
for topic in pubsub_client.list_topics():
print(topic.name)


Expand Down
2 changes: 1 addition & 1 deletion pubsub/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-pubsub==0.20.0
google-cloud-pubsub==0.21.0
Loading