Skip to main content

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:

  1. Configuration (Schema): A dictionary defining {column_name: strategy_name}.
  2. Strategy Registry: A central store for functions (e.g., hash, mask) that can be extended.
  3. Engine: A smart dispatcher that selects the best execution backend based on your input data.
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anonimize-0.1.0.tar.gz (46.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anonimize-0.1.0-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

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

Hashes for anonimize-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8d115f657184147e86b60152e0dee720db064326311e9b8dbd618e5419237920
MD5 8eaf05a497eaaf20c853b0c7e809e355
BLAKE2b-256 9d2f718a20c399c5e5fce389dcf9a138818e041794dc29c4e564399a8be589f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for anonimize-0.1.0.tar.gz:

Publisher: python-publish.yml on AleLoredo/anonimize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for anonimize-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 722761d80ec6567cc3641f0ec353f34cac58ddad4e677aacd4108230eb9f825a
MD5 13cd73bfffde1c1a833cfd83337127e2
BLAKE2b-256 6b2a114b393b05728eaa53e3ed8261852014cad66d14e521cf9d02d828ab6652

See more details on using hashes here.

Provenance

The following attestation bundles were made for anonimize-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on AleLoredo/anonimize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page