A flexible, backend-agnostic library for data anonymization (Pandas, Polars, Streams).
Project description
Anonimize Library
anonimize is a flexible, agnostic library designed for data anonymization. It decouples anonymization logic from data processing, allowing it to work seamlessly with Pandas, Polars, or pure Python Streams/Iterators.
Installation
The core library is lightweight (only faker and numpy). You can install it with specific backends depending on your needs:
# Minimum install (Streams & Cursors only)
pip install anonimize
# With Pandas support
pip install "anonimize[pandas]"
# With Polars support
pip install "anonimize[polars]"
# Install everything
pip install "anonimize[all]"
Core Concepts
The library is built on a Strategy Pattern architecture:
- Configuration (Schema): A dictionary defining
{column_name: strategy_name}. - Strategy Registry: A central store for functions (e.g., hash, mask) that can be extended.
- Engine: A smart dispatcher that selects the best execution backend based on your input data.
- Backends:
- Pandas/Polars: Optimized for in-memory, vectorized operations.
- Stream: Optimized for row-by-row processing (e.g., DB cursors) with constant memory usage.
Usage
The primary entry point is the anonymize() function, which automatically detects your data type and dispatches to the correct backend.
1. Unified Interface
import anonimize as nz
import pandas as pd
import polars as pl
schema = {
"user_id": "hash",
"email": "mask",
"name": "faker_name"
}
# --- Auto-dispatch for Pandas ---
df_pd = pd.DataFrame(...)
result_pd = nz.anonymize(df_pd, schema)
# --- Auto-dispatch for Polars ---
df_pl = pl.DataFrame(...)
result_pl = nz.anonymize(df_pl, schema)
# --- Auto-dispatch for List of Dicts (Streams) ---
data = [{"user_id": 1, "email": "test@test.com"}, ...]
result_iter_dicts = nz.anonymize(data, schema)
2. Database Cursors (Tuples)
For database drivers that yield tuples (like psycopg2 or sqlite3), use the columns argument or the specific from_cursor function.
cursor.execute("SELECT id, name, email FROM users")
columns = ["id", "name", "email"]
# Returns a generator of dicts
secure_rows = nz.anonymize(cursor, schema, columns=columns)
for row in secure_rows:
print(row) # {'id': '...', 'name': '...', 'email': '...'}
3. Explicit Backend Selection (Advanced)
You can still strictly enforce a specific backend if desired:
from anonimize import from_pandas, from_polars, from_stream
# Will raise TypeError if input is not a Pandas DataFrame
nz.from_pandas(df, schema)
Architecture
graph TD
UserInput[Input Data + Schema] --> Main[anonymize()]
Main --> Dispatcher{Check Type}
Dispatcher -->|Polars DataFrame| PolarsExec[Polars Backend]
Dispatcher -->|Pandas DataFrame| PandasExec[Pandas Backend]
Dispatcher -->|Iterator/List| StreamExec[Stream Backend]
PandasExec & PolarsExec & StreamExec --> Registry[StrategyRegistry]
Registry --> Strategies[Anonymization Functions]
Strategies -->|Hash/Mask/Fake| Result[Anonymized Data]
Extensibility
Register custom strategies globally:
from anonimize import register_strategy
def my_custom_redact(value):
return "REDACTED" if value else None
register_strategy("redact", my_custom_redact)
# Now use it in your schema
schema = {"notes": "redact"}
Dependencies
- Core:
faker,numpy - Optional:
pandas,polars
If an optional dependency is missing (e.g., passing a Pandas DataFrame without pandas installed), anonymize() will raise a clear TypeError.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anonimize-0.1.0.tar.gz.
File metadata
- Download URL: anonimize-0.1.0.tar.gz
- Upload date:
- Size: 46.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d115f657184147e86b60152e0dee720db064326311e9b8dbd618e5419237920
|
|
| MD5 |
8eaf05a497eaaf20c853b0c7e809e355
|
|
| BLAKE2b-256 |
9d2f718a20c399c5e5fce389dcf9a138818e041794dc29c4e564399a8be589f0
|
Provenance
The following attestation bundles were made for anonimize-0.1.0.tar.gz:
Publisher:
python-publish.yml on AleLoredo/anonimize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
anonimize-0.1.0.tar.gz -
Subject digest:
8d115f657184147e86b60152e0dee720db064326311e9b8dbd618e5419237920 - Sigstore transparency entry: 857277268
- Sigstore integration time:
-
Permalink:
AleLoredo/anonimize@ab951ee411069149832cbefba825e9e6f7023c30 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/AleLoredo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ab951ee411069149832cbefba825e9e6f7023c30 -
Trigger Event:
release
-
Statement type:
File details
Details for the file anonimize-0.1.0-py3-none-any.whl.
File metadata
- Download URL: anonimize-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
722761d80ec6567cc3641f0ec353f34cac58ddad4e677aacd4108230eb9f825a
|
|
| MD5 |
13cd73bfffde1c1a833cfd83337127e2
|
|
| BLAKE2b-256 |
6b2a114b393b05728eaa53e3ed8261852014cad66d14e521cf9d02d828ab6652
|
Provenance
The following attestation bundles were made for anonimize-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on AleLoredo/anonimize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
anonimize-0.1.0-py3-none-any.whl -
Subject digest:
722761d80ec6567cc3641f0ec353f34cac58ddad4e677aacd4108230eb9f825a - Sigstore transparency entry: 857277272
- Sigstore integration time:
-
Permalink:
AleLoredo/anonimize@ab951ee411069149832cbefba825e9e6f7023c30 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/AleLoredo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ab951ee411069149832cbefba825e9e6f7023c30 -
Trigger Event:
release
-
Statement type: