blob: 3f589cbd604bde208909137628dcfe4986a9a4e5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
from typing import List, Dict
from obfuscator.logger import get_logger
# Create the logger
logger = get_logger("Obfuscator")
def obfuscate(
data: List[Dict[str, str]], pii_fields: List[str]
) -> List[Dict[str, str]]:
"""
A function to obfuscate PII fields in a list of dictionaries, replacing
sensitive values with a string of asterisks.
"""
# If no data is provided, log a message and return an empty list
if not data:
logger.info("No valid data was provided to obfuscate")
return []
# Obfuscate the PII fields in each record using a list/dict comprehension
# This code is good but makes debugging a bit tricky. I may consider
# breaking it down into a for loop.
return [
{k: ("***" if k in pii_fields else v) for k, v in record.items()}
for record in data
]
|