Utility function to format CNPJ (Brazilian personal ID)
Project description
Utility function/class to format CPF (Brazilian ID document).
Python Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Installation
$ pip install cpf-fmt
Import
# Using class-based resource
from cpf_fmt import CpfFormatter
# Or using function-based one
from cpf_fmt import cpf_fmt
Usage
Object-Oriented Usage
formatter = CpfFormatter()
cpf = '47844241055'
print(formatter.format(cpf)) # returns '478.442.410-55'
# With options
print(formatter.format(
cpf,
hidden=True,
hidden_key='#',
hidden_start=3,
hidden_end=10
)) # returns '478.###.###-##'
The options can be provided to the constructor or the format() method. If passed to the constructor, the options will be attached to the CpfFormatter instance. When passed to the format() method, it only applies the options to that specific call.
cpf = '12345678910'
formatter = CpfFormatter(hidden=True)
print(formatter.format(cpf)) # '123.***.***-**'
print(formatter.format(cpf, hidden=False)) # '123.456.789-10' merges the options to the instance's
print(formatter.format(cpf)) # '123.***.***-**' uses only the instance options
Functional programming
The helper function cpf_fmt() is just a functional abstraction. Internally it creates an instance of CpfFormatter and calls the format() method right away.
cpf = '47844241055'
print(cpf_fmt(cpf)) # returns '478.442.410-55'
print(cpf_fmt(cpf, hidden=True)) # returns '478.***.***-**'
print(cpf_fmt(cpf, dot_key='', dash_key='_')) # returns '478442410_55'
Formatting Options
| Parameter | Type | Default | Description |
|---|---|---|---|
escape |
bool | None |
False |
Whether to HTML escape the result |
hidden |
bool | None |
False |
Whether to hide digits with a mask |
hidden_key |
str | None |
'*' |
Character to replace hidden digits |
hidden_start |
int | None |
3 |
Starting index for hidden range (0-10) |
hidden_end |
int | None |
10 |
Ending index for hidden range (0-10) |
dot_key |
str | None |
'.' |
String to replace dot characters |
dash_key |
str | None |
'-' |
String to replace dash character |
on_fail |
Callable | None |
lambda value, error=None: value |
Fallback function for invalid input |
Contribution & Support
We welcome contributions! Please see our Contributing Guidelines for details. But if you find this project helpful, please consider:
- ⭐ Starring the repository
- 🤝 Contributing to the codebase
- 💡 Suggesting new features
- 🐛 Reporting bugs
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG for a list of changes and version history.
Made with ❤️ by Lacus Solutions
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 cpf_fmt-1.0.0.tar.gz.
File metadata
- Download URL: cpf_fmt-1.0.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52baf8dd82cb99979a35983dabcc20dceec05140b1b69fdf16ee05c3c724c3b2
|
|
| MD5 |
c2b5593a21f5984dfb6930013fabef69
|
|
| BLAKE2b-256 |
edfe67477a85d6347d6718a12c640c21167dc44dee636d151039ff830840a0e8
|
File details
Details for the file cpf_fmt-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cpf_fmt-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1078a2e668c020dac0753c9be09528c5ddaf7cfcd0d7383aea3b476f2eb79b3a
|
|
| MD5 |
1eb89c8f2701b5d6d4a66ec0fd6d5f58
|
|
| BLAKE2b-256 |
8f187415171f83360b37ef6414dbdf65b76cb6b9b8a3a942f3a4d9a168481459
|