Skip to main content

Collection of utilities for Brazilian data and formats

Project description

pybr

Collection of utilities for handling Brazilian data, documents and formats.

Overview

pybr is a collection of utilities designed to handle common Brazilian data formats and standards. It provides a clean, simple, and reliable interface for validating, formatting, and cleaning various types of data specific to Brazil.

This library is aimed at developers who need to work with Brazilian data and want to ensure correctness and proper formatting according to local standards. While it currently includes robust support for document numbers, the long-term goal is to cover a wider range of data types, such as dates, currency, and more.

Features

  • Brazilian Documents: Tools for validating, formatting, and cleaning taxpayer registry numbers.
    • CPF: Support for individual taxpayer numbers.
    • CNPJ: Support for company taxpayer numbers, including 2026 alphanumeric format.
  • Extensible: Designed with a base structure to easily support other Brazilian data types in the future (e.g., dates, phone numbers).
  • Type-hinted: Fully type-hinted for better editor support and code quality.
  • No Dependencies: Lightweight and dependency-free.

Installation

You can install pybr from PyPI:

pip install pybr

Usage

Here are a few examples of how you can use pybr.

Example: CPF

The CPF class provides methods to handle CPF numbers.

from pybr import CPF

# --- Validation ---
# Note: Replace with a valid CPF for actual testing
cpf_valid = "123.456.789-00" 
cpf_invalid = "111.111.111-11"

print(f"Is {cpf_valid} valid? {CPF.is_valid(cpf_valid)}")
# Is 123.456.789-00 valid? True

print(f"Is {cpf_invalid} valid? {CPF.is_valid(cpf_invalid)}")
# Is 111.111.111-11 valid? False

# By default, repeated digit sequences are invalid. You can allow them:
print(f"Is {cpf_invalid} valid (allowing repeated)? {CPF.is_valid(cpf_invalid, allow_repeated=True)}")
# Is 111.111.111-11 valid (allowing repeated)? True (but checksum is still checked)


# --- Formatting ---
unformatted_cpf = "12345678900"
formatted_cpf = CPF.format(unformatted_cpf)
print(f"Formatted CPF: {formatted_cpf}")
# Formatted CPF: 123.456.789-00


# --- Cleaning ---
dirty_cpf = "123.456.789///00"
clean_cpf = CPF.clean(dirty_cpf)
print(f"Cleaned CPF: {clean_cpf}")
# Cleaned CPF: 12345678900


# --- Enforcement ---
# The `validate` method raises a ValueError for invalid CPFs.
try:
    CPF.validate(cpf_invalid)
except ValueError as e:
    print(e)
    # "Invalid CPF"

Example: CNPJ

The CNPJ class provides methods to handle CNPJ numbers.

from pybr import CNPJ

# --- Validation ---
# Note: Replace with a valid CNPJ for actual testing
cnpj_valid = "06.990.590/0001-23" 
cnpj_invalid = "11.111.111/1111-11"

print(f"Is {cnpj_valid} valid? {CNPJ.is_valid(cnpj_valid)}")
# Is 00.000.000/0001-91 valid? True

print(f"Is {cnpj_invalid} valid? {CNPJ.is_valid(cnpj_invalid)}")
# Is 11.111.111/1111-11 valid? False


# --- Formatting ---
unformatted_cnpj = "06990590000123"
formatted_cnpj = CNPJ.format(unformatted_cnpj)
print(f"Formatted CNPJ: {formatted_cnpj}")
# Formatted CNPJ: 06.990.590/0001-23


# --- Cleaning ---
dirty_cnpj = "06.990.590/0001-23"
clean_cnpj = CNPJ.clean(dirty_cnpj)
print(f"Cleaned CNPJ: {clean_cnpj}")
# Cleaned CNPJ: 06990590000123


# --- Enforcement ---
# The `validate` method raises a ValueError for invalid CNPJs.
try:
    CNPJ.validate(cnpj_invalid)
except ValueError as e:
    print(e)
    # "Invalid CNPJ"

Contributing

Contributions are welcome! If you have a feature request, bug report, or want to add support for another Brazilian data type (like dates, phone numbers, etc.), please feel free to:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/my-new-feature).
  3. Make your changes and add tests.
  4. Ensure the test suite passes (pytest).
  5. Open a pull request.

License

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

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

pybr-1.1.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

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

pybr-1.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pybr-1.1.0.tar.gz.

File metadata

  • Download URL: pybr-1.1.0.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pybr-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d5151a4c3392850c9911a5420d0b81f9120f26d4c4f025b9ea0e02ccdc4b52eb
MD5 027bf8b9a07f52faebeda2361b1d7110
BLAKE2b-256 0e6d483bcd8840b8abf04672169ff1c074401f7ca396cc21139a35b0f24bd7af

See more details on using hashes here.

File details

Details for the file pybr-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pybr-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pybr-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e14fa0be59ef4559502533b3c3c40ceb8c182a24badd85f61add3c2b9539625
MD5 04ccfa866256605c9d2578c69000aada
BLAKE2b-256 0275337ada0130c44c4ed2585b3cc50d84cd96ef050d2898ccece3aa1f34556c

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