8000 Add a simplified inspect string example · jlmwise/python-docs-samples@bbd2937 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbd2937

Browse files
committed
Add a simplified inspect string example
1 parent 85bf5d5 commit bbd2937

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

dlp/inspect_content.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,64 @@
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+
try:
70+
if finding.quote:
71+
print("Quote: {}".format(finding.quote))
72+
except AttributeError:
73+
pass
74+
print("Info type: {}".format(finding.info_type.name))
75+
print("Likelihood: {}".format(finding.likelihood))
76+
else:
77+
print("No findings.")
78+
79+
80+
# [END dlp_inspect_string_basic]
81+
82+
2583
# [START dlp_inspect_string]
2684
def inspect_string(
2785
project,

dlp/inspect_content_test.py

Lines changed: 10 additions & 0 deletions
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 gary@example.com"
166176

0 commit comments

Comments
 (0)
0