From f3f569e3690e11b44d9652390a98e8f348334f64 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 17 Sep 2015 10:52:17 -0700 Subject: [PATCH] Standardizing docstrings across bigquery samples. --- bigquery/README.md | 22 ++++++++ bigquery/api/async_query.py | 16 +++++- bigquery/api/export_data_to_cloud_storage.py | 19 ++++++- bigquery/api/getting_started.py | 16 ++++-- bigquery/api/list_datasets_projects.py | 58 +++++--------------- bigquery/api/load_data_by_post.py | 16 +++++- bigquery/api/load_data_from_csv.py | 18 +++++- bigquery/api/streaming.py | 16 +++++- bigquery/api/sync_query.py | 16 +++++- 9 files changed, 135 insertions(+), 62 deletions(-) mode change 100644 => 100755 bigquery/api/async_query.py mode change 100644 => 100755 bigquery/api/export_data_to_cloud_storage.py mode change 100644 => 100755 bigquery/api/getting_started.py mode change 100644 => 100755 bigquery/api/list_datasets_projects.py mode change 100644 => 100755 bigquery/api/load_data_by_post.py mode change 100644 => 100755 bigquery/api/load_data_from_csv.py mode change 100644 => 100755 bigquery/api/streaming.py mode change 100644 => 100755 bigquery/api/sync_query.py diff --git a/bigquery/README.md b/bigquery/README.md index 75f7b9a6897..ec0d694b2e9 100644 --- a/bigquery/README.md +++ b/bigquery/README.md @@ -2,6 +2,28 @@ This section contains samples for [Google BigQuery](https://cloud.google.com/bigquery). +## Running the samples + +In order to run it, your environment must be setup with [authentication +information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running it in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running: + + $ gcloud auth login + +## Additional resources + +For more information on BigQuery you can visit: + +> https://developers.google.com/bigquery + +For more information on the BigQuery API Python library surface you +can visit: + +> https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/ + +For information on the Python Client Library visit: + +> https://developers.google.com/api-client-library/python + ## Other Samples * [Using BigQuery from Google App Engine](../appengine/bigquery). diff --git a/bigquery/api/async_query.py b/bigquery/api/async_query.py old mode 100644 new mode 100755 index ff50c03cb17..4df547159cd --- a/bigquery/api/async_query.py +++ b/bigquery/api/async_query.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,7 +12,16 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +"""Command-line application to perform an asynchronous query in BigQuery. + +This sample is used on this page: + + https://cloud.google.com/bigquery/querying-data#asyncqueries + +For more information, see the README.md under /bigquery. +""" + import argparse import json import time @@ -103,7 +114,8 @@ def main(project_id, query_string, batch, num_retries, interval): # [START main] if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Loads data into BigQuery.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud project ID.') parser.add_argument('query', help='BigQuery SQL Query.') parser.add_argument( diff --git a/bigquery/api/export_data_to_cloud_storage.py b/bigquery/api/export_data_to_cloud_storage.py old mode 100644 new mode 100755 index bd0319f0613..84def572caf --- a/bigquery/api/export_data_to_cloud_storage.py +++ b/bigquery/api/export_data_to_cloud_storage.py @@ -1,4 +1,6 @@ -# Copyright 2015, Google, Inc. +#!/usr/bin/env python + +# Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -10,7 +12,17 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +"""Command-line application to export a table from BigQuery to Google Cloud +Storage. + +This sample is used on this page: + + https://cloud.google.com/bigquery/exporting-data-from-bigquery + +For more information, see the README.md under /bigquery. +""" + import argparse import time import uuid @@ -113,7 +125,8 @@ def main(cloud_storage_path, project_id, dataset_id, table_id, # [START main] if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Exports data from BigQuery to Google Cloud Storage.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud project ID.') parser.add_argument('dataset_id', help='BigQuery dataset to export.') parser.add_argument('table_id', help='BigQuery table to export.') diff --git a/bigquery/api/getting_started.py b/bigquery/api/getting_started.py old mode 100644 new mode 100755 index 8c8780028e0..777131f76de --- a/bigquery/api/getting_started.py +++ b/bigquery/api/getting_started.py @@ -13,11 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Sample for making BigQuery queries using the python sdk. +"""Command-line application that demonstrates basic BigQuery API usage. -This is a command-line script that queries a public shakespeare dataset, and -displays the 10 of Shakespeare's works with the greatest number of distinct -words. +This sample queries a public shakespeare dataset and displays the 10 of +Shakespeare's works with the greatest number of distinct words. + +This sample is used on this page: + + https://cloud.google.com/bigquery/bigquery-api-quickstart + +For more information, see the README.md under /bigquery. """ # [START all] import argparse @@ -63,7 +68,8 @@ def main(project_id): if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Queries the public BigQuery Shakespeare dataset.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud Project ID.') args = parser.parse_args() diff --git a/bigquery/api/list_datasets_projects.py b/bigquery/api/list_datasets_projects.py old mode 100644 new mode 100755 index d8c22c73b79..487b8384ded --- a/bigquery/api/list_datasets_projects.py +++ b/bigquery/api/list_datasets_projects.py @@ -1,4 +1,5 @@ -# -*- coding: utf-8 -*- +#!/usr/bin/env python + # Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,44 +12,14 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# -"""Command-line skeleton application for BigQuery API. - -This is the sample for this page: - - https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects - -In order to run it, your environment must be setup with authentication -information [1]. If you're running it in your local development environment and -you have the Google Cloud SDK [2] installed, you can do this easily by running: - - $ gcloud auth login -Usage: +"""Command-line application to list all projects and datasets in BigQuery. - $ python list_datasets_projects.py +This sample is used on this page: -where is the id of the developers console [3] project you'd like -to list the bigquery datasets and projects for. - -[1] https://developers.google.com/identity/protocols/\ - application-default-credentials#howtheywork -[2] https://cloud.google.com/sdk/ -[3] https://console.developers.google.com - -For more information on the BigQuery API you can visit: - - https://developers.google.com/bigquery/docs/overview - -For more information on the BigQuery API Python library surface you -can visit: - - https://developers.google.com/resources/api-libraries/documentation/ - bigquery/v2/python/latest/ - -For information on the Python Client Library visit: + https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects - https://developers.google.com/api-client-library/python/start/get_started +For more information, see the README.md under /bigquery. """ import argparse @@ -60,9 +31,9 @@ # [START list_datasets] -def list_datasets(service, project): +def list_datasets(bigquery, project): try: - datasets = service.datasets() + datasets = bigquery.datasets() list_reply = datasets.list(projectId=project).execute() print('Dataset list:') pprint(list_reply) @@ -74,10 +45,10 @@ def list_datasets(service, project): # [START list_projects] -def list_projects(service): +def list_projects(bigquery): try: # Start training on a data set - projects = service.projects() + projects = bigquery.projects() list_reply = projects.list().execute() print('Project list:') @@ -92,14 +63,15 @@ def list_projects(service): def main(project_id): credentials = GoogleCredentials.get_application_default() # Construct the service object for interacting with the BigQuery API. - service = discovery.build('bigquery', 'v2', credentials=credentials) + bigquery = discovery.build('bigquery', 'v2', credentials=credentials) - list_datasets(service, project_id) - list_projects(service) + list_datasets(bigquery, project_id) + list_projects(bigquery) if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Lists BigQuery datasets and projects.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='the project id to list.') args = parser.parse_args() diff --git a/bigquery/api/load_data_by_post.py b/bigquery/api/load_data_by_post.py old mode 100644 new mode 100755 index 08d2bd2bc3b..5527a055176 --- a/bigquery/api/load_data_by_post.py +++ b/bigquery/api/load_data_by_post.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,7 +12,16 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +"""Command-line application that loads data into BigQuery via HTTP POST. + +This sample is used on this page: + + https://cloud.google.com/bigquery/loading-data-post-request + +For more information, see the README.md under /bigquery. +""" + import argparse import json import time @@ -124,7 +135,8 @@ def main(project_id, dataset_id, table_name, schema_path, data_path): if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Loads data into BigQuery.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud project ID.') parser.add_argument('dataset_id', help='A BigQuery dataset ID.') parser.add_argument( diff --git a/bigquery/api/load_data_from_csv.py b/bigquery/api/load_data_from_csv.py old mode 100644 new mode 100755 index 084eecca63d..eee8dbf8cc0 --- a/bigquery/api/load_data_from_csv.py +++ b/bigquery/api/load_data_from_csv.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,7 +12,17 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +"""Command-line application that loads data into BigQuery from a CSV file in +Google Cloud Storage. + +This sample is used on this page: + + https://cloud.google.com/bigquery/loading-data-into-bigquery#loaddatagcs + +For more information, see the README.md under /bigquery. +""" + import argparse import json import time @@ -119,8 +131,8 @@ def main(project_id, dataset_id, table_name, schema_file, data_path, # [START main] if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Loads data into BigQuery from a CSV file in Google ' - 'Cloud Storage.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud project ID.') parser.add_argument('dataset_id', help='A BigQuery dataset ID.') parser.add_argument( diff --git a/bigquery/api/streaming.py b/bigquery/api/streaming.py old mode 100644 new mode 100755 index 06c1cfca052..c1549d735aa --- a/bigquery/api/streaming.py +++ b/bigquery/api/streaming.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,7 +12,16 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +"""Command-line application that streams data into BigQuery. + +This sample is used on this page: + + https://cloud.google.com/bigquery/streaming-data-into-bigquery + +For more information, see the README.md under /bigquery. +""" + import argparse import ast import json @@ -66,7 +77,8 @@ def get_rows(): # [START main] if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Streams data into BigQuery from the command line.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud project ID.') parser.add_argument('dataset_id', help='A BigQuery dataset ID.') parser.add_argument( diff --git a/bigquery/api/sync_query.py b/bigquery/api/sync_query.py old mode 100644 new mode 100755 index 5216e9aca68..db64acd9df8 --- a/bigquery/api/sync_query.py +++ b/bigquery/api/sync_query.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # Copyright 2015, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -10,7 +12,16 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# + +"""Command-line application to perform an synchronous query in BigQuery. + +This sample is used on this page: + + https://cloud.google.com/bigquery/querying-data#syncqueries + +For more information, see the README.md under /bigquery. +""" + import argparse import json @@ -67,7 +78,8 @@ def main(project_id, query, timeout, num_retries): # [START main] if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Loads data into BigQuery.') + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('project_id', help='Your Google Cloud project ID.') parser.add_argument('query', help='BigQuery SQL Query.') parser.add_argument(