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

Commit

Permalink
Add code sample and tests for redaction [(#4037)](GoogleCloudPlatform…
Browse files Browse the repository at this point in the history
…/python-docs-samples#4037)

Add A DLP code sample for redacting text.

Code will be linked to this documentation: https://cloud.google.com/dlp/docs/deidentify-sensitive-data
  • Loading branch information
jlmwise authored Jun 11, 2020
1 parent 2ef334d commit 9d74640
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
58 changes: 58 additions & 0 deletions samples/snippets/deid.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,64 @@ def deidentify_with_mask(

# [END dlp_deidentify_masking]

# [START dlp_deidentify_redact]
def deidentify_with_redact(
project,
input_str,
info_types,
):
"""Uses the Data Loss Prevention API to deidentify sensitive data in a
string by redacting matched input values.
Args:
project: The Google Cloud project id to use as a parent resource.
input_str: The string to deidentify (will be treated as text).
info_types: A list of strings representing info types to look for.
Returns:
None; the response from the API is printed to the terminal.
"""
import google.cloud.dlp

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

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

# Construct inspect configuration dictionary
inspect_config = {
"info_types": [{"name": info_type} for info_type in info_types]
}

# Construct deidentify configuration dictionary
deidentify_config = {
"info_type_transformations": {
"transformations": [
{
"primitive_transformation": {
"redact_config": {}
}
}
]
}
}

# Construct item
item = {"value": input_str}

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

# Print out the results.
print(response.item.value)


# [END dlp_deidentify_redact]

# [START dlp_deidentify_replace]
def deidentify_with_replace(
project,
Expand Down
8 changes: 8 additions & 0 deletions samples/snippets/deid_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def test_deidentify_with_mask_masking_number_specified(capsys):
assert "My SSN is *******27" in out


def test_deidentify_with_redact(capsys):
deid.deidentify_with_redact(
GCLOUD_PROJECT, HARMFUL_STRING + "!", ["US_SOCIAL_SECURITY_NUMBER"]
)
out, _ = capsys.readouterr()
assert "My SSN is !" in out


def test_deidentify_with_replace(capsys):
deid.deidentify_with_replace(
GCLOUD_PROJECT, HARMFUL_STRING, ["US_SOCIAL_SECURITY_NUMBER"],
Expand Down

0 comments on commit 9d74640

Please sign in to comment.