Skip to content

Instantly share code, notes, and snippets.

@eduardofcgo
Last active May 26, 2022 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardofcgo/17f90a94d661eed01c70525c1701bffe to your computer and use it in GitHub Desktop.
Save eduardofcgo/17f90a94d661eed01c70525c1701bffe to your computer and use it in GitHub Desktop.
validar nifs portugueses
import re
def _convert_to_int_list(nif_str):
return list(map(int, nif_str))
def _check_len(nif):
return len(nif) == 9
def _check_type(nif):
return nif[0] in {1, 2, 5, 6, 8, 9}
def _check_control(nif):
sum_ = 0
for pos, dig in enumerate(nif[:-1]):
sum_ += dig * (9 - pos)
control = sum_ % 11 and (11 - sum_ % 11) % 10
return control == nif[-1]
def check(nif_str):
try:
nif = _convert_to_int_list(nif_str)
return _check_len(nif) and _check_type(nif) and _check_control(nif)
except (ValueError, IndexError):
return False
_nine_digits_numbers_pattern = re.compile(r"[0-9\(\)]+")
def search(text):
nine_digits_numbers = _nine_digits_numbers_pattern.findall(text)
valid_nifs = filter(check, nine_digits_numbers)
return list(valid_nifs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment