Skip to content

Instantly share code, notes, and snippets.

@diegoponciano
Forked from juanplopes/cpf.py
Last active August 29, 2015 14:27
Show Gist options
  • Save diegoponciano/a54dca1e79a6045e5eb1 to your computer and use it in GitHub Desktop.
Save diegoponciano/a54dca1e79a6045e5eb1 to your computer and use it in GitHub Desktop.
def next_digit(value, base):
return value + str(sum(int(a)*b for a,b in zip(value, base))%11%10)
def make_valid(value, ap2, base):
return next_digit(next_digit(value, base), ap2+base)
def numbers_only(string):
return ''.join([s for s in string if s.isdigit()])
def is_valid_cpf(cpf):
cpf = numbers_only(cpf)
return make_valid(cpf[:9], [0], [1,2,3,4,5,6,7,8,9]) == cpf
def is_valid_cnpj(cnpj):
cnpj = numbers_only(cnpj)
return make_valid(cnpj[:12], [5], [6,7,8,9,2,3,4,5,6,7,8,9]) == cnpj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment