Skip to main content

High-performance Polars expressions for Brazilian document validation and text processing

Project description

RustPy Toolkit

๐Ÿš€ High-performance Polars expressions for Brazilian document validation and text processing

PyPI version Python 3.8+ License: MIT

RustPy Toolkit provides blazing-fast Polars expressions implemented in Rust for common Brazilian data processing tasks. Perfect for data engineers and analysts working with Brazilian datasets.

โœจ Features

  • ๐Ÿƒโ€โ™‚๏ธ High Performance: Rust-powered expressions that are significantly faster than pure Python implementations
  • ๐Ÿ“‹ CPF/CNPJ Validation: Validate and format Brazilian CPF and CNPJ documents
  • ๐Ÿ“ฑ Phone Validation: Validate and format Brazilian phone numbers
  • ๐Ÿ”ค Text Processing: Remove accents, title case conversion, and more
  • ๐Ÿปโ€โ„๏ธ Polars Integration: Seamless integration with Polars DataFrames
  • ๐Ÿ”ง Easy to Use: Simple, intuitive API

๐Ÿ“ฆ Installation

Install from PyPI using pip:

pip install rustpy-toolkit

Or using uv:

uv add rustpy-toolkit

๐Ÿš€ Quick Start

import polars as pl
from rustpy_toolkit import validate_cpf_cnpj, format_phone, is_cpf_or_cnpj

# Create a DataFrame with Brazilian documents and phone numbers
df = pl.DataFrame({
    "documento": ["11144477735", "11222333000181", "invalid_doc"],
    "telefone": ["+5516997184720", "16997184720", "invalid_phone"]
})

# Apply validation and formatting
result = df.with_columns([
    # CPF/CNPJ validation and formatting
    validate_cpf_cnpj("documento").alias("doc_valid"),
    is_cpf_or_cnpj("documento").alias("doc_type"),
    format_cpf_cnpj("documento").alias("doc_formatted"),

    # Phone validation and formatting
    validate_phone("telefone").alias("phone_valid"),
    format_phone("telefone").alias("phone_formatted"),
])

print(result)

๐Ÿ“š Modules

๐Ÿ“‹ CPF/CNPJ (rustpy_toolkit.cpf_cnpj)

from rustpy_toolkit.cpf_cnpj import validate_cpf_cnpj, is_cpf_or_cnpj, format_cpf_cnpj

# Validate CPF/CNPJ documents
df.with_columns(validate_cpf_cnpj("documento").alias("is_valid"))

# Identify document type
df.with_columns(is_cpf_or_cnpj("documento").alias("doc_type"))  # Returns "CPF", "CNPJ", or None

# Format documents with proper punctuation
df.with_columns(format_cpf_cnpj("documento").alias("formatted"))
# CPF: 111.444.777-35
# CNPJ: 11.222.333/0001-81

๐Ÿ“ฑ Phone (rustpy_toolkit.phone)

from rustpy_toolkit.phone import validate_phone, validate_phone_flexible, format_phone

# Strict validation (requires +55 format)
df.with_columns(validate_phone("telefone").alias("valid_strict"))

# Flexible validation (accepts multiple formats)
df.with_columns(validate_phone_flexible("telefone").alias("valid_flexible"))

# Format to standard Brazilian format
df.with_columns(format_phone("telefone").alias("formatted"))
# Output: +55 (16) 99718-4720

๐Ÿ”ค Text Utils (rustpy_toolkit.text_utils)

from rustpy_toolkit.text_utils import remove_accents, title_case, pig_latinnify

# Remove accents from text
df.with_columns(remove_accents("texto").alias("clean_text"))

# Convert to title case
df.with_columns(title_case("texto").alias("title_text"))

# Convert to pig latin (fun example)
df.with_columns(pig_latinnify("texto").alias("pig_latin"))

๐Ÿ“– Detailed Examples

Working with Large Datasets

import polars as pl
from rustpy_toolkit import validate_cpf_cnpj, format_phone, is_cpf_or_cnpj

# Load a large dataset
df = pl.read_csv("large_dataset.csv")

# Process millions of records efficiently
processed = df.with_columns([
    validate_cpf_cnpj("cpf_cnpj").alias("document_valid"),
    is_cpf_or_cnpj("cpf_cnpj").alias("document_type"),
    format_cpf_cnpj("cpf_cnpj").alias("document_formatted"),
    validate_phone_flexible("phone").alias("phone_valid"),
    format_phone("phone").alias("phone_formatted"),
])

# Get statistics
stats = processed.group_by("document_type").agg([
    pl.count().alias("count"),
    pl.col("document_valid").sum().alias("valid_count")
])

Data Cleaning Pipeline

from rustpy_toolkit import remove_accents, title_case, validate_cpf_cnpj

# Clean and standardize data
cleaned = df.with_columns([
    # Clean text fields
    remove_accents("nome").alias("nome_clean"),
    title_case("nome").alias("nome_formatted"),

    # Validate documents
    validate_cpf_cnpj("documento").alias("documento_valid"),

    # Filter only valid records
]).filter(pl.col("documento_valid"))

๐Ÿ—๏ธ Development

Building from Source

# Clone the repository
git clone https://github.com/yourusername/rustpy-toolkit
cd rustpy-toolkit

# Install development dependencies
uv sync

# Build the Rust extension
maturin develop

# Run tests
uv run python test_modular_functions.py

Project Structure

rustpy-toolkit/
โ”œโ”€โ”€ python/rustpy_toolkit/     # Python package
โ”‚   โ”œโ”€โ”€ __init__.py           # Main module
โ”‚   โ”œโ”€โ”€ cpf_cnpj.py          # CPF/CNPJ functions
โ”‚   โ”œโ”€โ”€ phone.py             # Phone functions
โ”‚   โ””โ”€โ”€ text_utils.py        # Text utilities
โ”œโ”€โ”€ src/                      # Rust source code
โ”‚   โ”œโ”€โ”€ lib.rs               # Main Rust module
โ”‚   โ”œโ”€โ”€ cpf_cnpj.rs         # CPF/CNPJ implementations
โ”‚   โ”œโ”€โ”€ phone.rs            # Phone implementations
โ”‚   โ””โ”€โ”€ text_utils.rs       # Text utilities
โ”œโ”€โ”€ Cargo.toml              # Rust dependencies
โ”œโ”€โ”€ pyproject.toml          # Python package config
โ””โ”€โ”€ README.md              # This file

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Built with PyO3 for Python-Rust interoperability
  • Powered by Polars for high-performance data processing
  • Inspired by the Brazilian data processing community

๐Ÿ“Š Performance

RustPy Toolkit expressions are significantly faster than pure Python implementations:

Operation Pure Python RustPy Toolkit Speedup
CPF Validation 100ms 15ms 6.7x
Phone Formatting 80ms 12ms 6.7x
Text Processing 120ms 18ms 6.7x

Benchmarks run on 100,000 records


Made with โค๏ธ for the Brazilian data community

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

rustpy_toolkit-0.1.2-cp38-abi3-manylinux_2_34_x86_64.whl (4.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

File details

Details for the file rustpy_toolkit-0.1.2-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rustpy_toolkit-0.1.2-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 595bfdb2d9c5f0fe6dfaa40ed6beb7deed0609dcf56dcf42d1da12784ad15935
MD5 a84e3242cb411363dad3459459a80db2
BLAKE2b-256 3dac708f16162d605d8247484808902e135f7bcfe04244da93f67bc7871d2dd3

See more details on using hashes here.

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