Utility function to format CNPJ (Brazilian employer ID)
Project description
Utility function/class to format CNPJ (Brazilian employer ID).
Python Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Installation
$ pip install cnpj-fmt
Import
# Using class-based resource
from cnpj_fmt import CnpjFormatter
# Or using function-based one
from cnpj_fmt import cnpj_fmt
Usage
Object-Oriented Usage
formatter = CnpjFormatter()
cnpj = '03603568000195'
print(formatter.format(cnpj)) # returns '03.603.568/0001-95'
# With options
print(formatter.format(
cnpj,
hidden=True,
hidden_key='#',
hidden_start=5,
hidden_end=13
)) # returns '03.603.###/####-##'
The options can be provided to the constructor or the format() method. If passed to the constructor, the options will be attached to the CnpjFormatter instance. When passed to the format() method, it only applies the options to that specific call.
cnpj = '03603568000195'
formatter = CnpjFormatter(hidden=True)
print(formatter.format(cnpj)) # '03.603.***/****-**'
print(formatter.format(cnpj, hidden=False)) # '03.603.568/0001-95' merges the options to the instance's
print(formatter.format(cnpj)) # '03.603.***/****-**' uses only the instance options
Functional programming
The helper function cnpj_fmt() is just a functional abstraction. Internally it creates an instance of CnpjFormatter and calls the format() method right away.
cnpj = '03603568000195'
print(cnpj_fmt(cnpj)) # returns '03.603.568/0001-95'
print(cnpj_fmt(cnpj, hidden=True)) # returns '03.603.***/****-**'
print(cnpj_fmt(cnpj, dot_key='', slash_key='|', dash_key='_')) # returns '03603568|0001_95'
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 |
5 |
Starting index for hidden range (0-13) |
hidden_end |
int | None |
13 |
Ending index for hidden range (0-13) |
dot_key |
str | None |
'.' |
String to replace dot characters |
slash_key |
str | None |
'/' |
String to replace slash character |
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 cnpj_fmt-1.0.0.tar.gz.
File metadata
- Download URL: cnpj_fmt-1.0.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75bd20af6b0e6f9780c159599a9b11a9beabce61571dc2010104ebfede2b4714
|
|
| MD5 |
2d64bcd9cf3ef185d064b65962a85e23
|
|
| BLAKE2b-256 |
333de743f8977ee718a57a69507d3651a6a0702d4ed1deb6e054f7d9bc89cb97
|
File details
Details for the file cnpj_fmt-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cnpj_fmt-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.5 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 |
d0076c8cb57d6a9f7d1bb79dd4f8bd2f6c6e658f27162a53890618d1d90ed5d4
|
|
| MD5 |
d5203a73346f1b2d796a926f5540d1dc
|
|
| BLAKE2b-256 |
4e3a46ee44ed670b8e856c3fb85ab92366df489e61b1ac933f1cdefa2ec6a000
|