Deployment, MLOps, and Security › AI Security and Privacy › Day 346
Hands-on lab — Day 346: Data Privacy and PII Handling
- ← Back to the Day 346 lesson
- Open the hands-on files on GitHub — clone or download them from the public labs repository
- Local path in your clone:
labs/sections/deployment-mlops-and-security/day-346-data-privacy-and-pii-handling/
Commands
Setup
pip install -r requirements/requirements.txt Run
python examples/data_privacy.py Test
bash tests/run_tests.sh File tree
examples/data_privacy.py expected-output/examples-run.txt expected-output/FIELDS.md expected-output/measured-values.txt expected-output/run-output.txt expected-output/starter-run.txt expected-output/test-output.txt expected-output/test-run.txt metadata.yml README.md requirements/requirements.txt security.md starter/data_privacy.py tests/run_tests.sh tests/test_data_privacy.py troubleshooting.md
Lab README
Day 346 Lab: Data Privacy and PII Handling
Lesson
- Lesson title: Data Privacy and PII Handling
- Day number: 346 of 365
- Lesson article: https://ai-roadmap-365.github.io/day-346-data-privacy-and-pii-handling
- Lab files: everything you need is in this directory — follow “How to run” below.
- Browse the course locally: from the repository root, this lab also appears in the course website at
/labs/day-346-data-privacy-and-pii-handlingwhen the site is running.
Purpose
Build a Production PII Token Vault and Differential Privacy Engine in Python implementing entity pseudonymization, reversible detokenization, right-to-forget key shredding, and calibrated Laplace noise addition.
Learning objectives
- Tokenize sensitive SSNs, emails, and credit cards into synthetic surrogates.
- Maintain reversible mapping tables and detokenize model responses.
- Implement right-to-be-forgotten deletion workflows.
- Apply the Laplace Mechanism for Differential Privacy.
Prerequisites
- Python 3.10+ installed
- pytest installed
Supported operating systems
- macOS, Linux, Windows WSL2
Hardware requirements
- Standard CPU, 512MB RAM
Required software
- Python 3.10+, pytest
Free and open-source options
- Python Standard Library, Pytest
Installation
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements/requirements.txt
File structure
starter/data_privacy.py: Starter implementation skeletonexamples/data_privacy.py: Verified reference implementationtests/test_data_privacy.py: Test suiteexpected-output/: Captured execution logs
How to run
python3 examples/data_privacy.py
What the commands do
- Executes PII tokenization tests, detokenization verification, key deletion checks, and Laplace noise bounds testing.
Expected output
Test email is <EMAIL_1>
Validation steps
Run test runner:
bash tests/run_tests.sh
Tests
The test suite validates:
- PII entity tokenization (SSN, Email, Credit Card)
- Reversible response detokenization
- Right-to-forget key shredding and unlearning
- Laplace mechanism differential privacy noise addition
- Zero division error handling on invalid epsilon
Cleanup
rm -rf .pytest_cache __pycache__ .venv
Troubleshooting
Ensure epsilon is strictly positive (epsilon > 0).
Security notes
Runs completely offline on local CPU.
Extension exercises
Implement phone number and address tokenization rules.
Navigation
Day number: 346 of 365
Expected output
FIELDS.md
# Measured Values
- `pii_records_tokenized`: 25
- `epsilon_budget`: 0.5
examples-run.txt
Test email is <EMAIL_1>
measured-values.txt
pii_records_tokenized=25
epsilon_budget=0.5
run-output.txt
Test email is <EMAIL_1>
starter-run.txt
An untouched starter fails the suite. Captured from a real run:
self = <examples.data_privacy.PIITokenVault object at 0x107108cd0>
text = 'Email same@test.com twice: same@test.com'
def tokenize_text(self, text: str) -> str:
> raise NotImplementedError('TASK 2: implement tokenize_text.')
E NotImplementedError: TASK 2: implement tokenize_text.
examples/data_privacy.py:30: NotImplementedError
=========================== short test summary info ============================
FAILED tests/test_data_privacy.py::test_pii_tokenization_and_detokenization
FAILED tests/test_data_privacy.py::test_right_to_be_forgotten_key_shredding
FAILED tests/test_data_privacy.py::test_differential_privacy_laplace_noise - ...
FAILED tests/test_data_privacy.py::test_invalid_epsilon_raises_error - NotImp...
FAILED tests/test_data_privacy.py::test_multiple_identical_entities_share_surrogate
============================== 5 failed in 0.03s ===============================
Implement each TASK in starter/, copy it over examples/, and the same
suite goes green.
test-output.txt
============================= test session starts ==============================
platform darwin -- Python 3.14.0, pytest-9.1.1, pluggy-1.6.0 -- <repo>/.venv-tools/bin/python3.14
cachedir: .pytest_cache
rootdir: <repo>/labs/sections/deployment-mlops-and-security/day-346-data-privacy-and-pii-handling
plugins: cov-7.1.0, anyio-4.14.2
collecting ... collected 5 items
tests/test_data_privacy.py::test_pii_tokenization_and_detokenization PASSED [ 20%]
tests/test_data_privacy.py::test_right_to_be_forgotten_key_shredding PASSED [ 40%]
tests/test_data_privacy.py::test_differential_privacy_laplace_noise PASSED [ 60%]
tests/test_data_privacy.py::test_invalid_epsilon_raises_error PASSED [ 80%]
tests/test_data_privacy.py::test_multiple_identical_entities_share_surrogate PASSED [100%]
============================== 5 passed in 0.01s ===============================
test-run.txt
============================= test session starts ==============================
platform darwin -- Python 3.14.0, pytest-9.1.1, pluggy-1.6.0 -- <repo>/.venv-tools/bin/python3.14
cachedir: .pytest_cache
rootdir: <repo>/labs/sections/deployment-mlops-and-security/day-346-data-privacy-and-pii-handling
plugins: cov-7.1.0, anyio-4.14.2
collecting ... collected 5 items
tests/test_data_privacy.py::test_pii_tokenization_and_detokenization PASSED [ 20%]
tests/test_data_privacy.py::test_right_to_be_forgotten_key_shredding PASSED [ 40%]
tests/test_data_privacy.py::test_differential_privacy_laplace_noise PASSED [ 60%]
tests/test_data_privacy.py::test_invalid_epsilon_raises_error PASSED [ 80%]
tests/test_data_privacy.py::test_multiple_identical_entities_share_surrogate PASSED [100%]
============================== 5 passed in 0.01s ===============================
Source files
examples/data_privacy.py (2895 bytes)
import re
import math
import random
from typing import Dict, Any, Tuple, List
class DifferentialPrivacyEngine:
@staticmethod
def laplace_mechanism(true_value: float, sensitivity: float, epsilon: float) -> float:
if epsilon <= 0:
raise ValueError("Epsilon privacy budget must be strictly positive.")
scale = sensitivity / epsilon
u = random.random() - 0.5
noise = -scale * math.copysign(1.0, u) * math.log(1.0 - 2.0 * abs(u))
return round(true_value + noise, 4)
class PIITokenVault:
def __init__(self):
self.forward_map: Dict[str, str] = {}
self.reverse_map: Dict[str, str] = {}
self.entity_counters: Dict[str, int] = {"PERSON": 0, "SSN": 0, "EMAIL": 0, "CREDIT_CARD": 0}
self.ssn_pattern = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
self.email_pattern = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b")
self.cc_pattern = re.compile(r"\b(?:\d{4}-){3}\d{4}\b")
def tokenize_text(self, text: str) -> str:
tokenized = text
for match in self.ssn_pattern.findall(text):
if match not in self.forward_map:
self.entity_counters["SSN"] += 1
token = f"<SSN_{self.entity_counters['SSN']}>"
self.forward_map[match] = token
self.reverse_map[token] = match
tokenized = tokenized.replace(match, self.forward_map[match])
for match in self.email_pattern.findall(text):
if match not in self.forward_map:
self.entity_counters["EMAIL"] += 1
token = f"<EMAIL_{self.entity_counters['EMAIL']}>"
self.forward_map[match] = token
self.reverse_map[token] = match
tokenized = tokenized.replace(match, self.forward_map[match])
for match in self.cc_pattern.findall(text):
if match not in self.forward_map:
self.entity_counters["CREDIT_CARD"] += 1
token = f"<CREDIT_CARD_{self.entity_counters['CREDIT_CARD']}>"
self.forward_map[match] = token
self.reverse_map[token] = match
tokenized = tokenized.replace(match, self.forward_map[match])
return tokenized
def detokenize_text(self, text: str) -> str:
detokenized = text
for token, raw_val in self.reverse_map.items():
detokenized = detokenized.replace(token, raw_val)
return detokenized
def forget_user_pii(self, raw_pii_value: str) -> bool:
if raw_pii_value in self.forward_map:
token = self.forward_map[raw_pii_value]
del self.forward_map[raw_pii_value]
del self.reverse_map[token]
return True
return False
if __name__ == "__main__":
v = PIITokenVault()
print(v.tokenize_text("Test email is test@domain.com"))
metadata.yml (399 bytes)
lesson_id: D346
day: 346
kind: python
languages:
- python
setup_commands:
- pip install -r requirements/requirements.txt
run_commands:
- python examples/data_privacy.py
test_commands:
- bash tests/run_tests.sh
cleanup_commands:
- rm -rf __pycache__ .pytest_cache
requires_network: false
requires_api_key: false
estimated_minutes: 45
last_executed: '2026-08-30'
executed_on: macOS 15.0 Apple Silicon
requirements/requirements.txt (14 bytes)
pytest>=7.0.0
starter/data_privacy.py (1529 bytes)
"""STARTER -- implement each TASK below, then run the tests.
Every function that raises NotImplementedError is yours to write. The
imports, constants and data structures are given; the logic is not.
Run `bash tests/run_tests.sh` to see which tasks are still outstanding.
"""
import re
import math
import random
from typing import Dict, Any, Tuple, List
class DifferentialPrivacyEngine:
@staticmethod
def laplace_mechanism(true_value: float, sensitivity: float, epsilon: float) -> float:
raise NotImplementedError('TASK 1: implement laplace_mechanism.')
class PIITokenVault:
def __init__(self):
self.forward_map: Dict[str, str] = {}
self.reverse_map: Dict[str, str] = {}
self.entity_counters: Dict[str, int] = {'PERSON': 0, 'SSN': 0, 'EMAIL': 0, 'CREDIT_CARD': 0}
self.ssn_pattern = re.compile('\\b\\d{3}-\\d{2}-\\d{4}\\b')
self.email_pattern = re.compile('\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b')
self.cc_pattern = re.compile('\\b(?:\\d{4}-){3}\\d{4}\\b')
def tokenize_text(self, text: str) -> str:
raise NotImplementedError('TASK 2: implement tokenize_text.')
def detokenize_text(self, text: str) -> str:
raise NotImplementedError('TASK 3: implement detokenize_text.')
def forget_user_pii(self, raw_pii_value: str) -> bool:
raise NotImplementedError('TASK 4: implement forget_user_pii.')
if __name__ == '__main__':
v = PIITokenVault()
print(v.tokenize_text('Test email is test@domain.com'))
tests/run_tests.sh (253 bytes)
#!/usr/bin/env bash
set -e
export PYTHONPATH=examples:.
if [ -n "$PYTEST" ]; then
$PYTEST tests/test_*.py -v
elif command -v pytest &> /dev/null; then
pytest tests/test_*.py -v
else
python3 -m unittest discover -s tests -p 'test_*.py' -v
fi
tests/test_data_privacy.py (2057 bytes)
import pytest
from examples.data_privacy import PIITokenVault, DifferentialPrivacyEngine
def test_pii_tokenization_and_detokenization():
vault = PIITokenVault()
raw_prompt = "Contact user@example.com with SSN 123-45-6789 and Card 1111-2222-3333-4444."
tokenized = vault.tokenize_text(raw_prompt)
assert "user@example.com" not in tokenized
assert "123-45-6789" not in tokenized
assert "1111-2222-3333-4444" not in tokenized
assert "<EMAIL_1>" in tokenized
assert "<SSN_1>" in tokenized
assert "<CREDIT_CARD_1>" in tokenized
# Detokenize
restored = vault.detokenize_text(tokenized)
assert restored == raw_prompt
def test_right_to_be_forgotten_key_shredding():
vault = PIITokenVault()
raw = "User email: secret@corp.org"
tokenized = vault.tokenize_text(raw)
# Delete secret@corp.org
deleted = vault.forget_user_pii("secret@corp.org")
assert deleted is True
# Detokenizing now leaves surrogate tag intact
after_forget = vault.detokenize_text(tokenized)
assert "secret@corp.org" not in after_forget
assert "<EMAIL_1>" in after_forget
def test_differential_privacy_laplace_noise():
# Epsilon=1.0, sensitivity=10.0
val = 1000.0
noisy = DifferentialPrivacyEngine.laplace_mechanism(val, sensitivity=10.0, epsilon=1.0)
assert isinstance(noisy, float)
# Laplace noise is centered at 0 with high probability inside [-100, 100]
assert 800.0 < noisy < 1200.0
def test_invalid_epsilon_raises_error():
with pytest.raises(ValueError):
DifferentialPrivacyEngine.laplace_mechanism(100.0, sensitivity=1.0, epsilon=0.0)
with pytest.raises(ValueError):
DifferentialPrivacyEngine.laplace_mechanism(100.0, sensitivity=1.0, epsilon=-0.5)
def test_multiple_identical_entities_share_surrogate():
vault = PIITokenVault()
text = "Email same@test.com twice: same@test.com"
tokenized = vault.tokenize_text(text)
assert tokenized == "Email <EMAIL_1> twice: <EMAIL_1>"
assert len(vault.forward_map) == 1