Skip to content

Commit

Permalink
Add DLP sample code for inspecting with custom regex detector (#4031)
Browse files Browse the repository at this point in the history
* code sample and test for medical record number custom regex detector

* fix linter error

* Using f-strings instead of string.format

Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>

Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: gcf-merge-on-green[bot] <60162190+gcf-merge-on-green[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 10, 2020
1 parent 29da228 commit f8aeba9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
60 changes: 60 additions & 0 deletions dlp/custom_infotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,63 @@ def omit_name_if_also_email(


# [END dlp_omit_name_if_also_email]

# [START dlp_inspect_with_medical_record_number_custom_regex_detector]
def inspect_with_medical_record_number_custom_regex_detector(
project,
content_string,
):
"""Uses the Data Loss Prevention API to analyze string with medical record
number custom regex detector
Args:
project: The Google Cloud project id to use as a parent resource.
content_string: The string to inspect.
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()

# Construct a custom regex detector info type called "C_MRN",
# with ###-#-##### pattern, where each # represents a digit from 1 to 9.
# The detector has a detection likelihood of POSSIBLE.
custom_info_types = [
{
"info_type": {"name": "C_MRN"},
"regex": {"pattern": "[1-9]{3}-[1-9]{1}-[1-9]{5}"},
"likelihood": "POSSIBLE",
}
]

# Construct the configuration dictionary with the custom regex info type.
inspect_config = {
"custom_info_types": custom_info_types,
}

# 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:
try:
if finding.quote:
print(f"Quote: {finding.quote}")
except AttributeError:
pass
print(f"Info type: {finding.info_type.name}")
print(f"Likelihood: {finding.likelihood}")
else:
print("No findings.")

# [END dlp_inspect_with_medical_record_number_custom_regex_detector]
8 changes: 8 additions & 0 deletions dlp/custom_infotype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ def test_omit_name_if_also_email(capsys):
# Ensure we found only EMAIL_ADDRESS, and not PERSON_NAME.
assert len(info_types) == 1
assert info_types[0] == "EMAIL_ADDRESS"


def test_inspect_with_medical_record_number_custom_regex_detector(capsys):
custom_infotype.inspect_with_medical_record_number_custom_regex_detector(
GCLOUD_PROJECT, "Patients MRN 444-5-22222")

out, _ = capsys.readouterr()
assert "Info type: C_MRN" in out

0 comments on commit f8aeba9

Please sign in to comment.