20
20
21
21
# [START dlp_omit_name_if_also_email]
22
22
def omit_name_if_also_email (
23
- project ,
24
- content_string ,
23
+ project , content_string ,
25
24
):
26
25
"""Marches PERSON_NAME and EMAIL_ADDRESS, but not both.
27
26
@@ -51,33 +50,34 @@ def omit_name_if_also_email(
51
50
# the total number of findings when there is a large overlap between different
52
51
# infoTypes.
53
52
inspect_config = {
54
- "info_types" :
55
- info_types_to_locate ,
56
- "rule_set" : [{
57
- "info_types" : [{
58
- "name" : "PERSON_NAME"
59
- }],
60
- "rules" : [{
61
- "exclusion_rule" : {
62
- "exclude_info_types" : {
63
- "info_types" : [{
64
- "name" : "EMAIL_ADDRESS"
65
- }]
66
- },
67
- "matching_type" : "MATCHING_TYPE_PARTIAL_MATCH"
68
- }
69
- }]
70
- }]
53
+ "info_types" : info_types_to_locate ,
54
+ "rule_set" : [
55
+ {
56
+ "info_types" : [{"name" : "PERSON_NAME" }],
57
+ "rules" : [
58
+ {
59
+ "exclusion_rule" : {
60
+ "exclude_info_types" : {
61
+ "info_types" : [{"name" : "EMAIL_ADDRESS" }]
62
+ },
63
+ "matching_type" : google .cloud .dlp_v2 .MatchingType .MATCHING_TYPE_PARTIAL_MATCH ,
64
+ }
65
+ }
66
+ ],
67
+ }
68
+ ],
71
69
}
72
70
73
71
# Construct the `item`.
74
72
item = {"value" : content_string }
75
73
76
74
# Convert the project id into a full resource id.
77
- parent = dlp . project_path ( project )
75
+ parent = f"projects/ { project } "
78
76
79
77
# Call the API.
80
- response = dlp .inspect_content (parent , inspect_config , item )
78
+ response = dlp .inspect_content (
79
+ request = {"parent" : parent , "inspect_config" : inspect_config , "item" : item }
80
+ )
81
81
82
82
return [f .info_type .name for f in response .result .findings ]
83
83
@@ -87,9 +87,7 @@ def omit_name_if_also_email(
87
87
88
88
# [START inspect_with_person_name_w_custom_hotword]
89
89
def inspect_with_person_name_w_custom_hotword (
90
- project ,
91
- content_string ,
92
- custom_hotword = "patient"
90
+ project , content_string , custom_hotword = "patient"
93
91
):
94
92
"""Uses the Data Loss Prevention API increase likelihood for matches on
95
93
PERSON_NAME if the user specified custom hotword is present. Only
@@ -114,7 +112,9 @@ def inspect_with_person_name_w_custom_hotword(
114
112
# window preceding the PII finding.
115
113
hotword_rule = {
116
114
"hotword_regex" : {"pattern" : custom_hotword },
117
- "likelihood_adjustment" : {"fixed_likelihood" : "VERY_LIKELY" },
115
+ "likelihood_adjustment" : {
116
+ "fixed_likelihood" : google .cloud .dlp_v2 .Likelihood .VERY_LIKELY
117
+ },
118
118
"proximity" : {"window_before" : 50 },
119
119
}
120
120
@@ -128,17 +128,19 @@ def inspect_with_person_name_w_custom_hotword(
128
128
# Construct the configuration dictionary with the custom regex info type.
129
129
inspect_config = {
130
130
"rule_set" : rule_set ,
131
- "min_likelihood" : " VERY_LIKELY" ,
131
+ "min_likelihood" : google . cloud . dlp_v2 . Likelihood . VERY_LIKELY ,
132
132
}
133
133
134
134
# Construct the `item`.
135
135
item = {"value" : content_string }
136
136
137
137
# Convert the project id into a full resource id.
138
- parent = dlp . project_path ( project )
138
+ parent = f"projects/ { project } "
139
139
140
140
# Call the API.
141
- response = dlp .inspect_content (parent , inspect_config , item )
141
+ response = dlp .inspect_content (
142
+ request = {"parent" : parent , "inspect_config" : inspect_config , "item" : item }
143
+ )
142
144
143
145
# Print out the results.
144
146
if response .result .findings :
@@ -153,13 +155,13 @@ def inspect_with_person_name_w_custom_hotword(
153
155
else :
154
156
print ("No findings." )
155
157
158
+
156
159
# [END inspect_with_person_name_w_custom_hotword]
157
160
158
161
159
162
# [START dlp_inspect_with_medical_record_number_custom_regex_detector]
160
163
def inspect_with_medical_record_number_custom_regex_detector (
161
- project ,
162
- content_string ,
164
+ project , content_string ,
163
165
):
164
166
"""Uses the Data Loss Prevention API to analyze string with medical record
165
167
number custom regex detector
@@ -183,7 +185,7 @@ def inspect_with_medical_record_number_custom_regex_detector(
183
185
{
184
186
"info_type" : {"name" : "C_MRN" },
185
187
"regex" : {"pattern" : "[1-9]{3}-[1-9]{1}-[1-9]{5}" },
186
- "likelihood" : " POSSIBLE" ,
188
+ "likelihood" : google . cloud . dlp_v2 . Likelihood . POSSIBLE ,
187
189
}
188
190
]
189
191
@@ -196,10 +198,12 @@ def inspect_with_medical_record_number_custom_regex_detector(
196
198
item = {"value" : content_string }
197
199
198
200
# Convert the project id into a full resource id.
199
- parent = dlp . project_path ( project )
201
+ parent = f"projects/ { project } "
200
202
201
203
# Call the API.
202
- response = dlp .inspect_content (parent , inspect_config , item )
204
+ response = dlp .inspect_content (
205
+ request = {"parent" : parent , "inspect_config" : inspect_config , "item" : item }
206
+ )
203
207
204
208
# Print out the results.
205
209
if response .result .findings :
@@ -214,13 +218,13 @@ def inspect_with_medical_record_number_custom_regex_detector(
214
218
else :
215
219
print ("No findings." )
216
220
221
+
217
222
# [END dlp_inspect_with_medical_record_number_custom_regex_detector]
218
223
219
224
220
225
# [START dlp_inspect_with_medical_record_number_w_custom_hotwords]
221
226
def inspect_with_medical_record_number_w_custom_hotwords (
222
- project ,
223
- content_string ,
227
+ project , content_string ,
224
228
):
225
229
"""Uses the Data Loss Prevention API to analyze string with medical record
226
230
number custom regex detector, with custom hotwords rules to boost finding
@@ -245,30 +249,23 @@ def inspect_with_medical_record_number_w_custom_hotwords(
245
249
{
246
250
"info_type" : {"name" : "C_MRN" },
247
251
"regex" : {"pattern" : "[1-9]{3}-[1-9]{1}-[1-9]{5}" },
248
- "likelihood" : " POSSIBLE" ,
252
+ "likelihood" : google . cloud . dlp_v2 . Likelihood . POSSIBLE ,
249
253
}
250
254
]
251
255
252
256
# Construct a rule set with hotwords "mrn" and "medical", with a likelohood
253
257
# boost to VERY_LIKELY when hotwords are present within the 10 character-
254
258
# window preceding the PII finding.
255
259
hotword_rule = {
256
- "hotword_regex" : {
257
- "pattern" : "(?i)(mrn|medical)(?-i)"
258
- },
260
+ "hotword_regex" : {"pattern" : "(?i)(mrn|medical)(?-i)" },
259
261
"likelihood_adjustment" : {
260
- "fixed_likelihood" : " VERY_LIKELY"
262
+ "fixed_likelihood" : google . cloud . dlp_v2 . Likelihood . VERY_LIKELY
261
263
},
262
- "proximity" : {
263
- "window_before" : 10
264
- }
264
+ "proximity" : {"window_before" : 10 },
265
265
}
266
266
267
267
rule_set = [
268
- {
269
- "info_types" : [{"name" : "C_MRN" }],
270
- "rules" : [{"hotword_rule" : hotword_rule }],
271
- }
268
+ {"info_types" : [{"name" : "C_MRN" }], "rules" : [{"hotword_rule" : hotword_rule }]}
272
269
]
273
270
274
271
# Construct the configuration dictionary with the custom regex info type.
@@ -281,10 +278,12 @@ def inspect_with_medical_record_number_w_custom_hotwords(
281
278
item = {"value" : content_string }
282
279
283
280
# Convert the project id into a full resource id.
284
- parent = dlp . project_path ( project )
281
+ parent = f"projects/ { project } "
285
282
286
283
# Call the API.
287
- response = dlp .inspect_content (parent , inspect_config , item )
284
+ response = dlp .inspect_content (
285
+ request = {"parent" : parent , "inspect_config" : inspect_config , "item" : item }
286
+ )
288
287
289
288
# Print out the results.
290
289
if response .result .findings :
@@ -299,4 +298,5 @@ def inspect_with_medical_record_number_w_custom_hotwords(
299
298
else :
300
299
print ("No findings." )
301
300
301
+
302
302
# [END dlp_inspect_with_medical_record_number_w_custom_hotwords]
0 commit comments