pycpfcnpj

repository·master·Indexed 41 days ago

https://github.com/matheuscas/pycpfcnpj

A lightweight Python module for validating and generating Brazilian registration numbers for individuals (CPF) and companies (CNPJ). It supports numeric, masked, and alphanumeric formats, providing a unified facade for automatic detection and validation, as well as tools for generating valid test numbers.

Tokens
2.5K
Snippets
13
Records
19
Agent score
50%

What's inside pycpfcnpj

  1. How the cpfcnpj facade works

    master
    The cpfcnpj module is a high-level interface (Facade) for the more specific cpf and cnpj modules. Instead of manually choosing which validator to use, you can call cpfcnpj.validate(), and the module will determine the correct validation logic based on the input provided.
  2. Validate CNPJ numbers using pycpfcnpj.cnpj

    master

    Use the pycpfcnpj.cnpj module when you need to ensure a number is specifically a valid CNPJ. This module will only accept CNPJ numbers.

    import pycpfcnpj.cnpj as cnpj
    
    valid_cnpj = '11444777000161'
    invalid_cnpj = '11444777000162'
    
    cnpj.validate(valid_cnpj)
    cnpj.validate(invalid_cnpj)
  3. Validate CPF numbers using pycpfcnpj.cpf

    master

    Use the pycpfcnpj.cpf module when you need to ensure a number is specifically a valid CPF. This module will only accept CPF numbers.

    import pycpfcnpj.cpf as cpf
    
    valid_cpf = '11144477735'
    invalid_cpf = '11144477736'
    
    cpf.validate(valid_cpf)
    cpf.validate(invalid_cpf)
  4. Validate both CPF and CNPJ using pycpfcnpj.cpfcnpj

    master

    Use the pycpfcnpj.cpfcnpj module when you want a single validation function that accepts either a CPF or a CNPJ number. The validate() method will determine the type and validate the number accordingly.

    import pycpfcnpj.cpfcnpj as cpfcnpj
    
    valid_cpf = '11144477735'
    invalid_cpf = '11144477736'
    
    cpfcnpj.validate(valid_cpf)
    cpfcnpj.validate(invalid_cpf)
    
    valid_cnpj = '11444777000161'
    invalid_cnpj = '11444777000162'
    
    cpfcnpj.validate(valid_cnpj)
    cpfcnpj.validate(invalid_cnpj)
  5. Validate CPF and CNPJ numbers using the cpfcnpj facade

    master

    The cpfcnpj module acts as a Facade that automatically detects whether a provided string is a CPF or a CNPJ and validates it. It supports both raw numeric strings and masked strings (with punctuation). As of version 1.9, it also supports alphanumeric CNPJs.

    from pycpfcnpj import cpfcnpj
    
    cpf_number = '11144477735'
    masked_cpf_number = '111.444.777-35'
    cnpj_number = '11444777000161'
    masked_cnpj_number = '11.444.777/0001-61'
    alphanumeric_cnpj = '12ABC34501DE35'
    
    print(cpfcnpj.validate(cpf_number))          # Returns True
    print(cpfcnpj.validate(masked_cpf_number))  # Returns True
    print(cpfcnpj.validate(cnpj_number))        # Returns True
    print(cpfcnpj.validate(masked_cnpj_number))# Returns True
    print(cpfcnpj.validate(alphanumeric_cnpj))  # Returns True
  6. Generate test CPF and CNPJ numbers

    master

    The gen module allows you to generate valid CPF and CNPJ numbers strictly for testing purposes. You can generate plain numeric strings or strings formatted with punctuation marks.

    from pycpfcnpj import gen
    
    # Generate plain numeric strings
    print(gen.cpf())                   # e.g., 49384063495
    print(gen.cnpj())                   # e.g., 20788274885880
    
    # Generate strings with punctuation
    print(gen.cpf_with_punctuation())    # e.g., 048.891.866-97
    print(gen.cnpj_with_punctuation())   # e.g., 63.212.638/0361-35