Skip to content

Commit 21cf56e

Browse files
emar-kartswast
authored andcommitted
docs(bigquery): standardize comments and formatting in existing code samples (#9212)
* update samples * comments rephrasing * docs: comments rephrasing
1 parent 6e6cd39 commit 21cf56e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+200
-95
lines changed

bigquery/samples/add_empty_column.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ def add_empty_column(client, table_id):
2121
# TODO(developer): Construct a BigQuery client object.
2222
# client = bigquery.Client()
2323

24-
# TODO(developer): Set table_id to the ID of the table to add an empty column.
24+
# TODO(developer): Set table_id to the ID of the table
25+
# to add an empty column.
2526
# table_id = "your-project.your_dataset.your_table_name"
2627

27-
table = client.get_table(table_id)
28+
table = client.get_table(table_id) # Make an API request.
2829

2930
original_schema = table.schema
3031
new_schema = original_schema[:] # creates a copy of the schema
3132
new_schema.append(bigquery.SchemaField("phone", "STRING"))
3233

3334
table.schema = new_schema
34-
table = client.update_table(table, ["schema"]) # API request
35+
table = client.update_table(table, ["schema"]) # Make an API request.
3536

3637
if len(table.schema) == len(original_schema) + 1 == len(new_schema):
3738
print("A new column has been added.")

bigquery/samples/browse_table_data.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def browse_table_data(client, table_id):
2626
# table_id = "your-project.your_dataset.your_table_name"
2727

2828
# Download all rows from a table.
29-
rows_iter = client.list_rows(table_id)
29+
rows_iter = client.list_rows(table_id) # Make an API request.
3030

3131
# Iterate over rows to make the API requests to fetch row data.
3232
rows = list(rows_iter)
@@ -38,10 +38,18 @@ def browse_table_data(client, table_id):
3838
print("Downloaded {} rows from table {}".format(len(rows), table_id))
3939

4040
# Specify selected fields to limit the results to certain columns.
41-
table = client.get_table(table_id)
41+
table = client.get_table(table_id) # Make an API request.
4242
fields = table.schema[:2] # first two columns
4343
rows_iter = client.list_rows(table_id, selected_fields=fields, max_results=10)
4444
rows = list(rows_iter)
4545
print("Selected {} columns from table {}.".format(len(rows_iter.schema), table_id))
4646
print("Downloaded {} rows from table {}".format(len(rows), table_id))
47+
48+
# Print row data in tabular format.
49+
rows = client.list_rows(table, max_results=10)
50+
format_string = "{!s:<16} " * len(rows.schema)
51+
field_names = [field.name for field in rows.schema]
52+
print(format_string.format(*field_names)) # Prints column headers.
53+
for row in rows:
54+
print(format_string.format(*row)) # Prints row data.
4755
# [END bigquery_browse_table]

bigquery/samples/create_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ def create_dataset(client, dataset_id):
3333
# Send the dataset to the API for creation.
3434
# Raises google.api_core.exceptions.Conflict if the Dataset already
3535
# exists within the project.
36-
dataset = client.create_dataset(dataset) # API request
36+
dataset = client.create_dataset(dataset) # Make an API request.
3737
print("Created dataset {}.{}".format(client.project, dataset.dataset_id))
3838
# [END bigquery_create_dataset]

bigquery/samples/create_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create_job(client):
3333
# The client libraries automatically generate a job ID. Override the
3434
# generated ID with either the job_id_prefix or job_id parameters.
3535
job_id_prefix="code_sample_",
36-
) # API request
36+
) # Make an API request.
3737

3838
print("Started job: {}".format(query_job.job_id))
3939
# [END bigquery_create_job]

bigquery/samples/create_routine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def create_routine(client, routine_id):
4040
],
4141
)
4242

43-
routine = client.create_routine(routine)
43+
routine = client.create_routine(routine) # Make an API request.
4444

4545
print("Created routine {}".format(routine.reference))
4646
# [END bigquery_create_routine]

bigquery/samples/create_routine_ddl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def create_routine_ddl(client, routine_id):
3636
)
3737

3838
# Initiate the query to create the routine.
39-
query_job = client.query(sql)
39+
query_job = client.query(sql) # Make an API request.
4040

4141
# Wait for the query to complete.
42-
query_job.result()
42+
query_job.result() # Waits for the job to complete.
4343

4444
print("Created routine {}".format(query_job.ddl_target_routine))
4545
# [END bigquery_create_routine_ddl]

bigquery/samples/create_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create_table(client, table_id):
3030
]
3131

3232
table = bigquery.Table(table_id, schema=schema)
33-
table = client.create_table(table) # API request
33+
table = client.create_table(table) # Make an API request.
3434
print(
3535
"Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id)
3636
)

bigquery/samples/dataset_exists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def dataset_exists(client, dataset_id):
2222
# dataset_id = "your-project.your_dataset"
2323

2424
try:
25-
client.get_dataset(dataset_id)
25+
client.get_dataset(dataset_id) # Make an API request.
2626
print("Dataset {} already exists".format(dataset_id))
2727
except NotFound:
2828
print("Dataset {} is not found".format(dataset_id))

bigquery/samples/delete_dataset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def delete_dataset(client, dataset_id):
2727

2828
# Use the delete_contents parameter to delete a dataset and its contents
2929
# Use the not_found_ok parameter to not receive an error if the dataset has already been deleted.
30-
client.delete_dataset(dataset_id, delete_contents=True, not_found_ok=True)
30+
client.delete_dataset(
31+
dataset_id, delete_contents=True, not_found_ok=True
32+
) # Make an API request.
3133

3234
print("Deleted dataset '{}'.".format(dataset_id))
3335
# [END bigquery_delete_dataset]

bigquery/samples/delete_dataset_labels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def delete_dataset_labels(client, dataset_id):
2525
# TODO(developer): Set dataset_id to the ID of the dataset to fetch.
2626
# dataset_id = "your-project.your_dataset"
2727

28-
dataset = client.get_dataset(dataset_id)
28+
dataset = client.get_dataset(dataset_id) # Make an API request.
2929

3030
# To delete a label from a dataset, set its value to None
3131
dataset.labels["color"] = None
3232

33-
dataset = client.update_dataset(dataset, ["labels"])
33+
dataset = client.update_dataset(dataset, ["labels"]) # Make an API request.
3434
print("Labels deleted from {}".format(dataset_id))
3535
# [END bigquery_delete_label_dataset]
3636
return dataset

bigquery/samples/delete_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def delete_model(client, model_id):
2626
# TODO(developer): Set model_id to the ID of the model to fetch.
2727
# model_id = 'your-project.your_dataset.your_model'
2828

29-
client.delete_model(model_id)
29+
client.delete_model(model_id) # Make an API request.
3030

3131
print("Deleted model '{}'.".format(model_id))
3232
# [END bigquery_delete_model]

bigquery/samples/delete_routine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def delete_routine(client, routine_id):
2525
# TODO(developer): Set the fully-qualified ID for the routine.
2626
# routine_id = "my-project.my_dataset.my_routine"
2727

28-
client.delete_routine(routine_id)
28+
client.delete_routine(routine_id) # Make an API request.
2929

3030
print("Deleted routine {}.".format(routine_id))
3131
# [END bigquery_delete_routine]

bigquery/samples/delete_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def delete_table(client, table_id):
2727

2828
# If the table does not exist, delete_table raises
2929
# google.api_core.exceptions.NotFound unless not_found_ok is True
30-
client.delete_table(table_id, not_found_ok=True)
30+
client.delete_table(table_id, not_found_ok=True) # Make an API request.
3131
print("Deleted table '{}'.".format(table_id))
3232
# [END bigquery_delete_table]

bigquery/samples/get_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_dataset(client, dataset_id):
2525
# TODO(developer): Set dataset_id to the ID of the dataset to fetch.
2626
# dataset_id = 'your-project.your_dataset'
2727

28-
dataset = client.get_dataset(dataset_id)
28+
dataset = client.get_dataset(dataset_id) # Make an API request.
2929

3030
full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id)
3131
friendly_name = dataset.friendly_name

bigquery/samples/get_dataset_labels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_dataset_labels(client, dataset_id):
2525
# TODO(developer): Set dataset_id to the ID of the dataset to fetch.
2626
# dataset_id = "your-project.your_dataset"
2727

28-
dataset = client.get_dataset(dataset_id)
28+
dataset = client.get_dataset(dataset_id) # Make an API request.
2929

3030
# View dataset labels
3131
print("Dataset ID: {}".format(dataset_id))

bigquery/samples/get_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_model(client, model_id):
2626
# TODO(developer): Set model_id to the ID of the model to fetch.
2727
# model_id = 'your-project.your_dataset.your_model'
2828

29-
model = client.get_model(model_id)
29+
model = client.get_model(model_id) # Make an API request.
3030

3131
full_model_id = "{}.{}.{}".format(model.project, model.dataset_id, model.model_id)
3232
friendly_name = model.friendly_name

bigquery/samples/get_routine.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def get_routine(client, routine_id):
2525
# TODO(developer): Set the fully-qualified ID for the routine.
2626
# routine_id = "my-project.my_dataset.my_routine"
2727

28-
routine = client.get_routine(routine_id)
28+
routine = client.get_routine(routine_id) # Make an API request.
2929

30-
print("Routine `{}`:".format(routine.reference))
31-
print(" Type: '{}'".format(routine.type_))
32-
print(" Language: '{}'".format(routine.language))
33-
print(" Arguments:")
30+
print("Routine '{}':".format(routine.reference))
31+
print("\tType: '{}'".format(routine.type_))
32+
print("\tLanguage: '{}'".format(routine.language))
33+
print("\tArguments:")
3434

3535
for argument in routine.arguments:
36-
print(" Name: '{}'".format(argument.name))
37-
print(" Type: '{}'".format(argument.type_))
36+
print("\t\tName: '{}'".format(argument.name))
37+
print("\t\tType: '{}'".format(argument.data_type))
3838
# [END bigquery_get_routine]
3939
return routine

bigquery/samples/get_table.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ def get_table(client, table_id):
2525
# TODO(developer): Set table_id to the ID of the model to fetch.
2626
# table_id = 'your-project.your_dataset.your_table'
2727

28-
table = client.get_table(table_id)
28+
table = client.get_table(table_id) # Make an API request.
2929

30+
# View table properties
3031
print(
3132
"Got table '{}.{}.{}'.".format(table.project, table.dataset_id, table.table_id)
3233
)
33-
34-
# View table properties
3534
print("Table schema: {}".format(table.schema))
3635
print("Table description: {}".format(table.description))
3736
print("Table has {} rows".format(table.num_rows))

bigquery/samples/label_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def label_dataset(client, dataset_id):
2525
# TODO(developer): Set dataset_id to the ID of the dataset to fetch.
2626
# dataset_id = "your-project.your_dataset"
2727

28-
dataset = client.get_dataset(dataset_id)
28+
dataset = client.get_dataset(dataset_id) # Make an API request.
2929
dataset.labels = {"color": "green"}
30-
dataset = client.update_dataset(dataset, ["labels"])
30+
dataset = client.update_dataset(dataset, ["labels"]) # Make an API request.
3131

3232
print("Labels added to {}".format(dataset_id))
3333
# [END bigquery_label_dataset]

bigquery/samples/list_datasets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def list_datasets(client):
2222
# TODO(developer): Construct a BigQuery client object.
2323
# client = bigquery.Client()
2424

25-
datasets = list(client.list_datasets())
25+
datasets = list(client.list_datasets()) # Make an API request.
2626
project = client.project
2727

2828
if datasets:
2929
print("Datasets in project {}:".format(project))
30-
for dataset in datasets: # API request(s)
30+
for dataset in datasets:
3131
print("\t{}".format(dataset.dataset_id))
3232
else:
3333
print("{} project does not contain any datasets.".format(project))

bigquery/samples/list_datasets_by_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def list_datasets_by_label(client):
2323
# client = bigquery.Client()
2424

2525
label_filter = "labels.color:green"
26-
datasets = list(client.list_datasets(filter=label_filter))
26+
datasets = list(client.list_datasets(filter=label_filter)) # Make an API request.
2727

2828
if datasets:
2929
print("Datasets filtered by {}:".format(label_filter))

bigquery/samples/list_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def list_models(client, dataset_id):
2727
# the models you are listing.
2828
# dataset_id = 'your-project.your_dataset'
2929

30-
models = client.list_models(dataset_id)
30+
models = client.list_models(dataset_id) # Make an API request.
3131

3232
print("Models contained in '{}':".format(dataset_id))
3333
for model in models:

bigquery/samples/list_routines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def list_routines(client, dataset_id):
2626
# the routines you are listing.
2727
# dataset_id = 'your-project.your_dataset'
2828

29-
routines = client.list_routines(dataset_id)
29+
routines = client.list_routines(dataset_id) # Make an API request.
3030

3131
print("Routines contained in dataset {}:".format(dataset_id))
3232
for routine in routines:

bigquery/samples/list_tables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def list_tables(client, dataset_id):
2626
# the tables you are listing.
2727
# dataset_id = 'your-project.your_dataset'
2828

29-
tables = client.list_tables(dataset_id)
29+
tables = client.list_tables(dataset_id) # Make an API request.
3030

3131
print("Tables contained in '{}':".format(dataset_id))
3232
for table in tables:

bigquery/samples/load_table_dataframe.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515

1616
def load_table_dataframe(client, table_id):
17+
1718
# [START bigquery_load_table_dataframe]
1819
from google.cloud import bigquery
20+
1921
import pandas
2022

2123
# TODO(developer): Construct a BigQuery client object.
@@ -59,11 +61,14 @@ def load_table_dataframe(client, table_id):
5961
)
6062

6163
job = client.load_table_from_dataframe(
62-
dataframe, table_id, job_config=job_config, location="US"
63-
)
64-
job.result() # Waits for table load to complete.
64+
dataframe,
65+
table_id,
66+
job_config=job_config,
67+
location="US", # Must match the destination dataset location.
68+
) # Make an API request.
69+
job.result() # Waits for the job to complete.
6570

66-
table = client.get_table(table_id)
71+
table = client.get_table(table_id) # Make an API request.
6772
print(
6873
"Loaded {} rows and {} columns to {}".format(
6974
table.num_rows, len(table.schema), table_id

bigquery/samples/query_to_arrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def query_to_arrow(client):
4141
CROSS JOIN UNNEST(r.participants) as participant;
4242
"""
4343
query_job = client.query(sql)
44-
arrow_table = query_job.to_arrow()
44+
arrow_table = query_job.to_arrow() # Make an API request.
4545

4646
print(
4747
"Downloaded {} rows, {} columns.".format(

bigquery/samples/tests/test_browse_table_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ def test_browse_table_data(capsys, client, table_with_data_id):
2424
assert "Downloaded 10 rows from table {}".format(table_with_data_id) in out
2525
assert "Selected 2 columns from table {}".format(table_with_data_id) in out
2626
assert "Downloaded 10 rows from table {}".format(table_with_data_id) in out
27+
assert "word" in out
28+
assert "LVII" in out
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from .. import create_routine
17+
18+
19+
def test_create_routine(capsys, client, random_routine_id):
20+
21+
create_routine.create_routine(client, random_routine_id)
22+
out, err = capsys.readouterr()
23+
assert "Created routine {}".format(random_routine_id) in out

0 commit comments

Comments
 (0)