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

Commit

Permalink
Add a simplified inspect string example to DLP code samples [(#4069)](G…
Browse files Browse the repository at this point in the history
…oogleCloudPlatform/python-docs-samples#4069)

* Add a simplified inspect string example

* Remove unnecessary try-catch block - all findings in this examnple should have quotes.
  • Loading branch information
jlmwise authored Jun 12, 2020
1 parent af4ecad commit fa8478a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
54 changes: 54 additions & 0 deletions samples/snippets/inspect_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,60 @@
import os


# [START dlp_inspect_string_basic]
def inspect_string_basic(
project,
content_string,
info_types=["PHONE_NUMBER"],
):
"""Uses the Data Loss Prevention API to analyze strings for protected data.
Args:
project: The Google Cloud project id to use as a parent resource.
content_string: The string to inspect.
info_types: A list of strings representing info types to look for.
A full list of info type categories can be fetched from the API.
Returns:
None; the response from the API is printed to the terminal.
"""

# Import the client library.
import google.cloud.dlp

# Instantiate a client.
dlp = google.cloud.dlp_v2.DlpServiceClient()

# Prepare info_types by converting the list of strings into a list of
# dictionaries (protos are also accepted).
info_types = [{"name": info_type} for info_type in info_types]

# Construct the configuration dictionary.
inspect_config = {
"info_types": info_types,
"include_quote": True,
}

# Construct the `item`.
item = {"value": content_string}

# Convert the project id into a full resource id.
parent = dlp.project_path(project)

# Call the API.
response = dlp.inspect_content(parent, inspect_config, item)

# Print out the results.
if response.result.findings:
for finding in response.result.findings:
print("Quote: {}".format(finding.quote))
print("Info type: {}".format(finding.info_type.name))
print("Likelihood: {}".format(finding.likelihood))
else:
print("No findings.")


# [END dlp_inspect_string_basic]


# [START dlp_inspect_string]
def inspect_string(
project,
Expand Down
10 changes: 10 additions & 0 deletions samples/snippets/inspect_content_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ def bigquery_project():
bigquery_client.delete_dataset(dataset_ref, delete_contents=True)


def test_inspect_string_basic(capsys):
test_string = "String with a phone number: 234-555-6789"

inspect_content.inspect_string_basic(GCLOUD_PROJECT, test_string)

out, _ = capsys.readouterr()
assert "Info type: PHONE_NUMBER" in out
assert "Quote: 234-555-6789" in out


def test_inspect_string(capsys):
test_string = "My name is Gary Smith and my email is gary@example.com"

Expand Down

0 comments on commit fa8478a

Please sign in to comment.