Advertisement
Stealth_Kaze

Untitled

Jul 4th, 2022 (edited)
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. // Primeiro arquivo .py
  2.  
  3. import cnpj
  4.  
  5. cnpj1 = '04.252.011/0001-10'
  6.  
  7. if cnpj.valida(cnpj1):
  8.     print(f'{cnpj1} é válido')
  9. else:
  10.     print(f'{cnpj1} é inválido')
  11.  
  12. // Segunda parte do arquivo .py
  13.  
  14.  
  15. REGRESSIVOS = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
  16.  
  17.  
  18. def valida(cnpj):
  19.     cnpj = apenas_numeros(cnpj)
  20.  
  21.     try:
  22.         if eh_sequencia(cnpj):
  23.             return False
  24.     except:
  25.         return False
  26.  
  27.     try:
  28.         novo_cnpj = calcula_digito(cnpj=cnpj, digito=1)
  29.         novo_cnpj = calcula_digito(cnpj=novo_cnpj, digito=2)
  30.     except Exception as e:
  31.         return False
  32.  
  33.     if novo_cnpj == cnpj:
  34.         return True
  35.     else:
  36.         return False
  37.  
  38.  
  39. def calcula_digito(cnpj, digito):
  40.     if digito == 1:
  41.         regressivos = REGRESSIVOS[1:]
  42.         novo_cnpj = cnpj[:-2]
  43.     elif digito == 2:
  44.         regressivos = REGRESSIVOS
  45.         novo_cnpj = cnpj
  46.     else:
  47.         return None
  48.  
  49.     total = 0
  50.     for indice, regressivo in enumerate(regressivos):
  51.         total += int(cnpj[indice]) * regressivo
  52.  
  53.     digito = 11 - (total % 11)
  54.     digito = digito if digito <= 9 else 0
  55.  
  56.     return f'{novo_cnpj}{digito}'
  57.  
  58.  
  59. def eh_sequencia(cnpj):
  60.     sequencia = cnpj[0] * len(cnpj)
  61.  
  62.     if sequencia == cnpj:
  63.         return True
  64.     else:
  65.         return False
  66.  
  67.  
  68. def apenas_numeros(cnpj):
  69.     return re.sub(r'[^0-9]', '', cnpj)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement