Skip to content

Commit 14ae1f2

Browse files
Mattix23gcf-owl-bot[bot]tswast
authored
docs: revise label table code samples (#1451)
* docs: revise label table code samples * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added TODO to clean up snippets.py Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Tim Swast <[email protected]>
1 parent ed1de4f commit 14ae1f2

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

docs/snippets.py

+3
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ def test_manage_table_labels(client, to_delete):
250250
table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
251251
table = client.create_table(table)
252252

253+
# TODO(Mattix23): After code sample from https://github.com/googleapis/python-bigquery/pull/1451
254+
# is updated from cloud.google.com delete this.
255+
253256
# [START bigquery_label_table]
254257
# from google.cloud import bigquery
255258
# client = bigquery.Client()

samples/snippets/label_table.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2022 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+
def label_table(table_id: str) -> None:
17+
orig_table_id = table_id
18+
# [START bigquery_label_table]
19+
from google.cloud import bigquery
20+
21+
client = bigquery.Client()
22+
23+
# TODO(dev): Change table_id to the full name of the table you want to create.
24+
table_id = "your-project.your_dataset.your_table_name"
25+
26+
# [END bigquery_label_table]
27+
table_id = orig_table_id
28+
# [START bigquery_label_table]
29+
table = client.get_table(table_id) # API request
30+
31+
labels = {"color": "green"}
32+
table.labels = labels
33+
34+
table = client.update_table(table, ["labels"]) # API request
35+
36+
print(f"Added {table.labels} to {table_id}.")
37+
# [END bigquery_label_table]

samples/snippets/label_table_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2022 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+
import typing
16+
17+
import label_table
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_label_table(
24+
capsys: "pytest.CaptureFixture[str]",
25+
table_id: str,
26+
) -> None:
27+
28+
label_table.label_table(table_id)
29+
30+
out, _ = capsys.readouterr()
31+
assert "color" in out
32+
assert table_id in out

0 commit comments

Comments
 (0)