Skip to content
This repository was archived by the owner on Dec 10, 2023. It is now read-only.

Commit fa8478a

Browse files
authored
Add a simplified inspect string example to DLP code samples [(#4069)](GoogleCloudPlatform/python-docs-samples#4069)
* Add a simplified inspect string example * Remove unnecessary try-catch block - all findings in this examnple should have quotes.
1 parent af4ecad commit fa8478a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

samples/snippets/inspect_content.py

+54
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,60 @@
2222
import os
2323

2424

25+
# [START dlp_inspect_string_basic]
26+
def inspect_string_basic(
27+
project,
28+
content_string,
29+
info_types=["PHONE_NUMBER"],
30+
):
31+
"""Uses the Data Loss Prevention API to analyze strings for protected data.
32+
Args:
33+
project: The Google Cloud project id to use as a parent resource.
34+
content_string: The string to inspect.
35+
info_types: A list of strings representing info types to look for.
36+
A full list of info type categories can be fetched from the API.
37+
Returns:
38+
None; the response from the API is printed to the terminal.
39+
"""
40+
41+
# Import the client library.
42+
import google.cloud.dlp
43+
44+
# Instantiate a client.
45+
dlp = google.cloud.dlp_v2.DlpServiceClient()
46+
47+
# Prepare info_types by converting the list of strings into a list of
48+
# dictionaries (protos are also accepted).
49+
info_types = [{"name": info_type} for info_type in info_types]
50+
51+
# Construct the configuration dictionary.
52+
inspect_config = {
53+
"info_types": info_types,
54+
"include_quote": True,
55+
}
56+
57+
# Construct the `item`.
58+
item = {"value": content_string}
59+
60+
# Convert the project id into a full resource id.
61+
parent = dlp.project_path(project)
62+
63+
# Call the API.
64+
response = dlp.inspect_content(parent, inspect_config, item)
65+
66+
# Print out the results.
67+
if response.result.findings:
68+
for finding in response.result.findings:
69+
print("Quote: {}".format(finding.quote))
70+
print("Info type: {}".format(finding.info_type.name))
71+
print("Likelihood: {}".format(finding.likelihood))
72+
else:
73+
print("No findings.")
74+
75+
76+
# [END dlp_inspect_string_basic]
77+
78+
2579
# [START dlp_inspect_string]
2680
def inspect_string(
2781
project,

samples/snippets/inspect_content_test.py

+10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ def bigquery_project():
161161
bigquery_client.delete_dataset(dataset_ref, delete_contents=True)
162162

163163

164+
def test_inspect_string_basic(capsys):
165+
test_string = "String with a phone number: 234-555-6789"
166+
167+
inspect_content.inspect_string_basic(GCLOUD_PROJECT, test_string)
168+
169+
out, _ = capsys.readouterr()
170+
assert "Info type: PHONE_NUMBER" in out
171+
assert "Quote: 234-555-6789" in out
172+
173+
164174
def test_inspect_string(capsys):
165175
test_string = "My name is Gary Smith and my email is [email protected]"
166176

0 commit comments

Comments
 (0)