Created
June 5, 2018 19:13
-
-
Save daviwesley/206025d9dac90673119ba7ef70b45ce7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validar_cpf(cpf): | |
digitos = [int(c) for c in cpf if c.isdigit()] | |
if len(digitos) == 11: | |
a, b, c, d, e, f, g, h, i, j, k = digitos | |
numeros = [a, b, c, d, e, f, g, h, i] | |
r = range(10, 1, -1) | |
soma = sum([x * y for x, y in zip(numeros, r)]) | |
resto = soma % 11 | |
dv1 = (11 - resto if 11 - resto < 10 else 0) | |
numeros = [a, b, c, d, e, f, g, h, i, dv1] | |
r = range(11, 1, -1) | |
soma = sum([x*y for x, y in zip(numeros, r)]) | |
resto = soma % 11 | |
dv2 = (11 - resto if 11 - resto < 10 else 0) | |
return dv1 == j and dv2 == k | |
return False | |
def validar_cnpj(cnpj): | |
""" | |
Valida CNPJs, retornando apenas a string de números válida. | |
# CNPJs errados | |
>>> validar_cnpj('abcdefghijklmn') | |
False | |
>>> validar_cnpj('123') | |
False | |
>>> validar_cnpj('') | |
False | |
>>> validar_cnpj(None) | |
False | |
>>> validar_cnpj('12345678901234') | |
False | |
>>> validar_cnpj('11222333000100') | |
False | |
# CNPJs corretos | |
>>> validar_cnpj('11222333000181') | |
'11222333000181' | |
>>> validar_cnpj('11.222.333/0001-81') | |
'11222333000181' | |
>>> validar_cnpj(' 11 222 333 0001 81 ') | |
'11222333000181' | |
""" | |
cnpj = ''.join(re.findall('\d', str(cnpj))) | |
if (not cnpj) or (len(cnpj) < 14): | |
return False | |
inteiros = list(map(int, cnpj)) | |
novo = inteiros[:12] | |
prod = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] | |
while len(novo) < 14: | |
r = sum([x * y for (x, y) in zip(novo, prod)]) % 11 | |
if r > 1: | |
f = 11 - r | |
else: | |
f = 0 | |
novo.append(f) | |
prod.insert(0, 6) | |
if novo == inteiros: | |
return cnpj | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment