rodrigosantosbr

[Py] Validate Brazilian CPF and CNPJ numbers

Jan 26th, 2019
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. import re
  2.  
  3. def is_valid_cpf(cpf):
  4.     """Check if a 11-numbers-only Brazilian CPF string is valid
  5.  
  6.    Parameters
  7.    ----------
  8.        cpf: string
  9.            11-numbers-only Brazilian CPF
  10.  
  11.    Returns
  12.    -------
  13.        True or False
  14.  
  15.    Raises
  16.    ------
  17.        None
  18.  
  19.    Examples
  20.    --------
  21.        >>> cpf1 = '06670116098'
  22.        >>> print(is_valid_cpf(cpf1))
  23.        True
  24.        >>> cpf2 = '06670116099'
  25.        >>> print(is_valid_cpf(cpf2))
  26.        False
  27.        >>> cpf3 = '0667011609A'
  28.        >>> print(is_valid_cpf(cpf3))
  29.        False
  30.        >>> cpf4 = '0667011'
  31.        >>> print(is_valid_cpf(cpf4))
  32.        False
  33.  
  34.    See Also
  35.    --------
  36.        https://djangosnippets.org/snippets/10601/
  37.    """
  38.  
  39.     cpf = str(cpf)
  40.     # Strip all whitespaces
  41.     cpf = re.sub(r'\s+', '', cpf)
  42.     # Remove punctuation
  43.     cpf = re.sub(r'[^\w\s]', '', cpf)
  44.  
  45.     # The method isdigit() checks whether the string consists of digits only.
  46.     if not cnpj.isdigit():
  47.         return False
  48.  
  49.     if len(cpf) != 11:
  50.         return False
  51.  
  52.     orig_dv = cpf[-2:]
  53.  
  54.     new_1dv = sum([i * int(cpf[idx]) for idx, i in enumerate(range(10, 1, -1))])
  55.     if (new_1dv % 11) >= 2:
  56.         new_1dv = 11 - (new_1dv % 11)
  57.     else:
  58.         new_1dv = 0
  59.     cpf = cpf[:-2] + str(new_1dv) + cpf[-1]
  60.     new_2dv = sum([i * int(cpf[idx]) for idx, i in enumerate(range(11, 1, -1))])
  61.     if (new_2dv % 11) >= 2:
  62.         new_2dv = 11 - (new_2dv % 11)
  63.     else:
  64.         new_2dv = 0
  65.     cpf = cpf[:-1] + str(new_2dv)
  66.     if cpf[-2:] != orig_dv:
  67.         return False
  68.     return True
  69.  
  70. def is_valid_cnpj(cnpj):
  71.     """Check if a 14-numbers-only Brazilian CNPJ string is valid
  72.  
  73.    Parameters
  74.    ----------
  75.        cpf: string
  76.            11-numbers-only Brazilian CPF
  77.  
  78.    Returns
  79.    -------
  80.        True or False
  81.  
  82.    Raises
  83.    ------
  84.        ValueError
  85.            If cnpj contains alphabet chars
  86.  
  87.    Examples
  88.    --------
  89.        >>> cnpj1 = '57375425000100'
  90.        >>> print(is_valid_cnpj(cnpj1))
  91.        True
  92.        >>> cnpj2 = '73787162000190'
  93.        >>> print(is_valid_cnpj(cnpj2))
  94.        False
  95.        >>> cnpj3 = '73787162000A90'
  96.        >>> print(is_valid_cnpj(cnpj3))
  97.        False
  98.        >>> cnpj4 = '73787162000'
  99.        >>> print(is_valid_cnpj(cnpj4))
  100.        False
  101.  
  102.    See Also
  103.    --------
  104.        https://djangosnippets.org/snippets/10601/
  105.    """
  106.  
  107.     cnpj = str(cnpj)
  108.     # Strip all whitespaces
  109.     cnpj = re.sub(r'\s+', '', cnpj)
  110.     # Remove punctuation
  111.     cnpj = re.sub(r'[^\w\s]', '', cnpj)
  112.  
  113.     # The method isdigit() checks whether the string consists of digits only.
  114.     if not cnpj.isdigit():
  115.         return False
  116.  
  117.     if len(cnpj) != 14:
  118.         return False
  119.  
  120.     orig_dv = cnpj[-2:]
  121.  
  122.     new_1dv = sum([i * int(cnpj[idx]) for idx, i in enumerate(list(range(5, 1, -1)) + list(range(9, 1, -1)))])
  123.     if (new_1dv % 11) >= 2:
  124.         new_1dv = 11 - (new_1dv % 11)
  125.     else:
  126.         new_1dv = 0
  127.     cnpj = cnpj[:-2] + str(new_1dv) + cnpj[-1]
  128.     new_2dv = sum([i * int(cnpj[idx]) for idx, i in enumerate(list(range(6, 1, -1)) + list(range(9, 1, -1)))])
  129.     if (new_2dv % 11) >= 2:
  130.         new_2dv = 11 - (new_2dv % 11)
  131.     else:
  132.         new_2dv = 0
  133.     cnpj = cnpj[:-1] + str(new_2dv)
  134.  
  135.     if cnpj[-2:] != orig_dv:
  136.         return False
  137.     return True
Advertisement
Add Comment
Please, Sign In to add comment