Skip to main content

ccpf

A CPF (brazilian register numbers for persons) library that can:

  • Validate CPFs
  • Generate random CPFs
  • Apply and remove masks from CPFs
  • Check if CPFs are masked

Install

Just do pip3 install ccpf and you are good to go.

How to use

After importing ccpf you can:

  • generate() - Generate a random valid unmasked CPF.
import ccpf
cpf = ccpf.generate()
assert ccpf.validate(cpf)
  • validate(cpf) - Validate if a string is a valid CPF. Works for masked and unmasked CPFs.
import ccpf
cpf = ccpf.generate()
assert ccpf.validate(cpf)
  • has_mask(cpf) - Return wheter or not the given CPF is masked. If CPF format is invalid, it will raise a CPFInvalidFormat exception.
import ccpf
cpf = ccpf.generate()
assert not ccpf.has_mask(cpf)
  • mask(cpf) - Return the masked version of the given CPF. If the CPF is already masked, just return it. If CPF format is invalid, it will raise a CPFInvalidFormat exception.
import ccpf
cpf = ccpf.generate()
assert not ccpf.has_mask(cpf)
masked = ccpf.mask(cpf)
assert ccpf.has_mask(masked)
masked2 = ccpf.mask(masked)
assert ccpf.has_mask(masked2)
  • unmask(cpf) - Return unmasked version of the given CPF. If the CPF is already unmasked, just return it. If CPF format is invalid, it will raise a CPFInvalidFormat exception.
import ccpf
cpf = ccpf.generate()
assert not ccpf.has_mask(cpf)
unmasked = ccpf.unmask(cpf)
assert not ccpf.has_mask(unmasked)
masked = ccpf.mask(unmasked)
assert ccpf.has_mask(masked)
unmasked2 = ccpf.unmask(masked)
assert not ccpf.has_mask(unmasked2)

CPFInvalidFormat

A CPF may have a valid format, but be invalid:

  • without mask but invalid:
invalid_cpf_with_right_format = '12345678901'
assert not ccpf.validate(invalid_cpf_with_right_format)
error = False
try:
    ccpf.unmask(invalid_cpf_with_right_format)
except ccpf.CPFInvalidFormat:
    error = True
finally:
    assert not error
  • with mask but invalid.
invalid_cpf_with_right_format = '123.456.789-01'
assert not ccpf.validate(invalid_cpf_with_right_format)
error = False
try:
    ccpf.unmask(invalid_cpf_with_right_format)
except ccpf.CPFInvalidFormat:
    error = True
finally:
    assert not error
  • CPFInvalidFormat will be raised when the format is invalid. It does not say whether the CPF itself is invalid or not.
cpf_with_wrong_format = 'thisisclearlynotinthecpfformat'
assert not ccpf.validate(cpf_with_wrong_format)
error = False
try:
    ccpf.unmask(cpf_with_wrong_format)
except ccpf.CPFInvalidFormat:
    error = True
finally:
    assert error

Run tests

Just execute tox by calling it:

tox

Metadata

Release files for ccpf 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ccpf 1.0.0
File Size Uploaded Provenance Actions
ccpf-1.0.0.tar.gz 60.7 kB Without provenance Details for ccpf-1.0.0.tar.gz

Release files / ccpf-1.0.0.tar.gz

Download URL ccpf-1.0.0.tar.gz
Size 60.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d804180633116c18f09d70fc4c460daee79bac288943a65bb56cec7ef12c976f
BLAKE2b-256 checksum
How to use checksums
8523d9c5aaf3560bc067e90c7f2bd4a08edb3150f8722acf937256968cd56c3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.8

Security for ccpf 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page