Skip to content

Commit 0dab7d2

Browse files
Mattix23aribrayparthea
authored
docs: revise delete label table code sample, add TODO to clean up sni… (#1466)
* docs: revise delete label table code sample, add TODO to clean up snippets.py * changed name of test function to align with file name Co-authored-by: aribray <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent bdfe888 commit 0dab7d2

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

docs/snippets.py

+2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ def test_manage_table_labels(client, to_delete):
203203
# [END bigquery_get_table_labels]
204204
assert table.labels == labels
205205

206+
# TODO(Mattix23): After code sample is updated from cloud.google.com delete this
207+
206208
# [START bigquery_delete_label_table]
207209
# from google.cloud import bigquery
208210
# client = bigquery.Client()
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
from google.cloud import bigquery
16+
17+
18+
def delete_label_table(table_id: str, label_key: str) -> bigquery.Table:
19+
orig_table_id = table_id
20+
orig_label_key = label_key
21+
# [START bigquery_delete_label_table]
22+
from google.cloud import bigquery
23+
24+
client = bigquery.Client()
25+
26+
# TODO(dev): Change table_id to the full name of the table you wish to delete from.
27+
table_id = "your-project.your_dataset.your_table_name"
28+
# TODO(dev): Change label_key to the name of the label you want to remove.
29+
label_key = "color"
30+
# [END bigquery_delete_label_table]
31+
table_id = orig_table_id
32+
label_key = orig_label_key
33+
# [START bigquery_delete_label_table]
34+
table = client.get_table(table_id) # API request
35+
36+
# To delete a label from a table, set its value to None
37+
table.labels[label_key] = None
38+
39+
table = client.update_table(table, ["labels"]) # API request
40+
41+
print(f"Deleted label '{label_key}' from {table_id}.")
42+
# [END bigquery_delete_label_table]
43+
return table
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 delete_label_table
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_delete_label_table(
24+
capsys: "pytest.CaptureFixture[str]",
25+
table_id: str,
26+
) -> None:
27+
28+
table = delete_label_table.delete_label_table(table_id, "color")
29+
30+
out, _ = capsys.readouterr()
31+
assert "Deleted" in out
32+
assert "color" in out
33+
assert table_id in out
34+
assert table.labels is None or "color" not in table.labels

0 commit comments

Comments
 (0)